Skip to content

Commit

Permalink
Fix: wrong calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Dec 16, 2024
1 parent 4465880 commit e029bc9
Showing 1 changed file with 4 additions and 21 deletions.
25 changes: 4 additions & 21 deletions Splito/UI/Home/Expense/AddExpenseViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ class AddExpenseViewModel: BaseViewModel, ObservableObject {
await fetchExpenseDetailsWithMembers(expenseId: expenseId)
} else if let groupId {
await fetchGroup(groupId: groupId)
await fetchDefaultUser()
}
}
}

// MARK: - Data Loading
private func fetchGroup(groupId: String) async {
guard let id = preference.user?.id else { return }

do {
viewState = .loading
let group = try await groupRepository.fetchGroupBy(id: groupId)
Expand All @@ -81,6 +82,7 @@ class AddExpenseViewModel: BaseViewModel, ObservableObject {
groupMembers = group.members
selectedMembers = group.members
}
selectedPayers = [id: expenseAmount]
viewState = .initial
LogD("AddExpenseViewModel: \(#function) Group fetched successfully.")
} catch {
Expand All @@ -90,25 +92,6 @@ class AddExpenseViewModel: BaseViewModel, ObservableObject {
}
}

private func fetchDefaultUser() async {
guard let id = preference.user?.id else { return }
do {
viewState = .loading
let user = try await userRepository.fetchUserBy(userID: id)
guard let user else {
viewState = .initial
return
}
selectedPayers = [user.id: expenseAmount]
viewState = .initial
LogD("AddExpenseViewModel: \(#function) Default user fetched successfully.")
} catch {
viewState = .initial
LogE("AddExpenseViewModel: \(#function) Failed to fetch default user: \(error).")
showToastForError()
}
}

private func fetchExpenseDetailsWithMembers(expenseId: String) async {
guard let groupId else { return }
do {
Expand Down Expand Up @@ -341,7 +324,7 @@ extension AddExpenseViewModel {
selectedPayers = [user.id: expenseAmount]
}

if expenseName == "" || expenseAmount == 0 || selectedGroup == nil || selectedPayers == [:] {
if expenseName == "" || expenseAmount == 0 || selectedGroup == nil || selectedPayers == [:] || selectedPayers.values.reduce(0, +) != expenseAmount {
showToastFor(toast: ToastPrompt(type: .warning, title: "Warning",
message: "Please fill all data to add expense."))
return false
Expand Down

0 comments on commit e029bc9

Please sign in to comment.