Skip to content

Commit

Permalink
Manage no expense state for the group list screen
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Jun 19, 2024
1 parent 353447e commit f5a5908
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
5 changes: 4 additions & 1 deletion Splito/Localization/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@
},
"No email address" : {
"extractionState" : "manual"
},
"no expense" : {

},
"No expenses here yet." : {

Expand Down Expand Up @@ -392,7 +395,7 @@
"Take Picture" : {

},
"Tap the plus button from home screen to add an expense with any group." : {
"Tap the plus button to add an expense with any group." : {

},
"This code will be active for 2 days." : {
Expand Down
2 changes: 1 addition & 1 deletion Splito/UI/Home/Groups/Group/GroupHomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private struct NoExpenseView: View {
.font(.subTitle4(17))
.foregroundStyle(primaryText)

Text("Tap the plus button from home screen to add an expense with any group.")
Text("Tap the plus button to add an expense with any group.")
.font(.body1(18))
.foregroundStyle(secondaryText)
.multilineTextAlignment(.center)
Expand Down
2 changes: 1 addition & 1 deletion Splito/UI/Home/Groups/GroupListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private struct GroupListCellView: View {
let isBorrowed = group.oweAmount < 0
VStack(alignment: .trailing, spacing: 4) {
if group.oweAmount == 0 {
Text("settled up")
Text(group.hasExpenses ? "settled up" : "no expense")
.font(.body1(12))
.foregroundStyle(secondaryText)
} else {
Expand Down
25 changes: 18 additions & 7 deletions Splito/UI/Home/Groups/GroupListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ class GroupListViewModel: BaseViewModel, ObservableObject {
return Fail(error: ServiceError.dataNotFound).eraseToAnyPublisher()
}

return fetchGroupMembers(groupId: groupId)
.combineLatest(fetchExpenses(group: group))
.map { (members, amountTuple) in
let (expense, owingAmounts) = amountTuple
return GroupInformation(group: group, oweAmount: expense, memberOweAmount: owingAmounts, members: members)
return fetchExpenses(group: group)
.combineLatest(fetchGroupMembers(groupId: groupId))
.map { (expenseResult, members) -> GroupInformation in
let (expenseTotal, owingAmounts, hasExpenses) = expenseResult
return GroupInformation(group: group, oweAmount: expenseTotal, memberOweAmount: owingAmounts, members: members, hasExpenses: hasExpenses)
}
.eraseToAnyPublisher()
}
Expand All @@ -127,14 +127,24 @@ class GroupListViewModel: BaseViewModel, ObservableObject {
groupRepository.fetchMembersBy(groupId: groupId)
}

private func fetchExpenses(group: Groups) -> AnyPublisher<(Double, [String: Double]), ServiceError> {
private func fetchExpenses(group: Groups) -> AnyPublisher<(Double, [String: Double], Bool), ServiceError> {
expenseRepository.fetchExpensesBy(groupId: group.id ?? "")
.flatMap { [weak self] expenses -> AnyPublisher<(Double, [String: Double]), ServiceError> in
.flatMap { [weak self] expenses -> AnyPublisher<(Double, [String: Double], Bool), ServiceError> in
guard let self else { return Fail(error: .dataNotFound).eraseToAnyPublisher() }
if group.isDebtSimplified {
return self.calculateExpensesSimply(expenses: expenses)
.map { (total, owingAmounts) in
let hasExpenses = !expenses.isEmpty
return (total, owingAmounts, hasExpenses)
}
.eraseToAnyPublisher()
} else {
return self.calculateExpenses(expenses: expenses)
.map { (total, owingAmounts) in
let hasExpenses = !expenses.isEmpty
return (total, owingAmounts, hasExpenses)
}
.eraseToAnyPublisher()
}
}
.eraseToAnyPublisher()
Expand Down Expand Up @@ -278,6 +288,7 @@ struct GroupInformation {
let oweAmount: Double
let memberOweAmount: [String: Double]
let members: [AppUser]
let hasExpenses: Bool
}

// MARK: - Group States
Expand Down

0 comments on commit f5a5908

Please sign in to comment.