Skip to content

Commit

Permalink
Show payments made and payments received data in group totals group s…
Browse files Browse the repository at this point in the history
…pending summary sheet
  • Loading branch information
cp-nirali-s committed Jun 20, 2024
1 parent 4749e71 commit fd0aab5
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,19 @@ private struct GroupTotalSummaryView: View {
}

private var totalShare: Double {
viewModel.getTotalShareAmount()
return viewModel.getTotalShareAmount()
}

private var totalChangeInBalance: Double {
return viewModel.getTotalChangeInBalance()
}

private var paymentsMade: Double {
return 0 // Add calculation for payments made
return viewModel.getPaymentsMade()
}

private var paymentsReceived: Double {
return 0 // Add calculation for payments received
return viewModel.getPaymentsReceived()
}

var body: some View {
Expand All @@ -124,7 +124,7 @@ private struct GroupTotalSummaryView: View {
GroupSummaryAmountView(text: "Payments made", amount: paymentsMade)
GroupSummaryAmountView(text: "Payments received", amount: paymentsReceived)
GroupSummaryAmountView(text: "Total change in balance", amount: totalChangeInBalance,
fontColor: (totalChangeInBalance < 0 ? amountBorrowedColor : amountLentColor))
fontColor: (totalChangeInBalance < 0 ? amountBorrowedColor : amountLentColor))
}
.padding(.horizontal, 4)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class GroupTotalsViewModel: BaseViewModel, ObservableObject {
@Inject var preference: SplitoPreference
@Inject var groupRepository: GroupRepository
@Inject var expenseRepository: ExpenseRepository
@Inject var transactionRepository: TransactionRepository

@Published var viewState: ViewState = .initial
@Published var selectedTab: GroupTotalsTabType = .thisMonth
Expand All @@ -38,6 +39,15 @@ class GroupTotalsViewModel: BaseViewModel, ObservableObject {
@Published private var expenses: [Expense] = []
@Published var filteredExpenses: [Expense] = []

@Published private(set) var filteredTransactions: [Transactions] = []
@Published private var transactions: [Transactions] = []

static private let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "MMMM yyyy"
return formatter
}()

private let groupId: String

init(groupId: String) {
Expand All @@ -46,40 +56,54 @@ class GroupTotalsViewModel: BaseViewModel, ObservableObject {
self.fetchGroupAndExpenses()
}

// MARK: - Data Loading
private func fetchGroupAndExpenses() {
viewState = .loading
groupRepository.fetchGroupBy(id: groupId)
.sink { [weak self] completion in
if case .failure(let error) = completion {
self?.viewState = .initial
self?.showToastFor(error)
self?.handleServiceError(error)
}
} receiveValue: { [weak self] group in
guard let self, let group else { return }
self.group = group
self.fetchExpenses(group: group)
self.fetchTransactions()
self.viewState = .initial
}.store(in: &cancelable)
}

private func fetchExpenses(group: Groups) {
expenseRepository.fetchExpensesBy(groupId: groupId)
.sink { [weak self] completion in
if case .failure(let error) = completion {
self?.viewState = .initial
self?.showToastFor(error)
self?.handleServiceError(error)
}
} receiveValue: { [weak self] expenses in
guard let self else { return }
self.expenses = expenses
self.filteredExpensesForSelectedTab()
self.viewState = .initial
}.store(in: &cancelable)
}

func fetchTransactions() {
transactionRepository.fetchTransactionsBy(groupId: groupId).sink { [weak self] completion in
if case .failure(let error) = completion {
self?.handleServiceError(error)
}
} receiveValue: { [weak self] transactions in
guard let self else { return }
self.transactions = transactions
self.filteredTransactionsForSelectedTab()
}.store(in: &cancelable)
}

// MARK: - User Actions
func handleTabItemSelection(_ selection: GroupTotalsTabType) {
withAnimation(.easeInOut(duration: 0.3), {
selectedTab = selection
filteredExpensesForSelectedTab()
filteredTransactionsForSelectedTab()
})
}

Expand Down Expand Up @@ -118,6 +142,41 @@ class GroupTotalsViewModel: BaseViewModel, ObservableObject {
}
}

private func filteredTransactionsForSelectedTab() {
switch selectedTab {
case .thisMonth:
let calendar = Calendar.current
let currentMonth = calendar.component(.month, from: Date())
let currentYear = calendar.component(.year, from: Date())

filteredTransactions = transactions.filter {
let transactionDate = $0.date.dateValue()
let transactionMonth = calendar.component(.month, from: transactionDate)
let transactionYear = calendar.component(.year, from: transactionDate)
return transactionMonth == currentMonth && transactionYear == currentYear
}
case .lastMonth:
let calendar = Calendar.current
let currentDate = Date()

guard let lastMonthDate = calendar.date(byAdding: .month, value: -1, to: currentDate) else {
return
}

let lastMonth = calendar.component(.month, from: lastMonthDate)
let lastMonthYear = calendar.component(.year, from: lastMonthDate)

filteredTransactions = transactions.filter {
let transactionDate = $0.date.dateValue()
let transactionMonth = calendar.component(.month, from: transactionDate)
let transactionYear = calendar.component(.year, from: transactionDate)
return transactionMonth == lastMonth && transactionYear == lastMonthYear
}
case .allTime:
filteredTransactions = transactions
}
}

func getTotalShareAmount() -> Double {
guard let user = preference.user else { return 0 }

Expand Down Expand Up @@ -161,6 +220,26 @@ class GroupTotalsViewModel: BaseViewModel, ObservableObject {
}
return amountOweByMember[user.id] ?? 0
}

func getPaymentsMade() -> Double {
guard let user = preference.user else { return 0 }
let paymentsMade = filteredTransactions
.filter { $0.payerId == user.id }
return paymentsMade.reduce(0) { $0 + $1.amount }
}

func getPaymentsReceived() -> Double {
guard let user = preference.user else { return 0 }
let paymentsReceived = filteredTransactions
.filter { $0.receiverId == user.id }
return paymentsReceived.reduce(0) { $0 + $1.amount }
}

// MARK: - Error Handling
private func handleServiceError(_ error: ServiceError) {
viewState = .initial
showToastFor(error)
}
}

// MARK: - View States
Expand Down
4 changes: 2 additions & 2 deletions Splito/UI/Home/Groups/Group/GroupHomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ struct GroupOptionsListView: View {
HStack(spacing: 16) {
GroupOptionsButtonView(text: "Settle up", isForSettleUp: isSettleUpEnable, onTap: onSettleUpTap)

GroupOptionsButtonView(text: "Totals", onTap: onTotalsTap)

GroupOptionsButtonView(text: "Transactions", onTap: onTransactionsTap)

GroupOptionsButtonView(text: "Balances", onTap: onBalanceTap)

GroupOptionsButtonView(text: "Totals", onTap: onTotalsTap)
}
.padding(.bottom, 4)
.padding(.vertical, 6)
Expand Down

0 comments on commit fd0aab5

Please sign in to comment.