From 9b25974252c10b0ea8f2717570a8d8d0a5ea754a Mon Sep 17 00:00:00 2001 From: NiraliSonani Date: Tue, 17 Dec 2024 12:16:56 +0530 Subject: [PATCH] Added checks to stop wrong amount & fixed split option alert --- Splito/Localization/Localizable.xcstrings | 5 ++++- .../UI/Home/Expense/AddExpenseViewModel.swift | 17 +++++++++-------- .../Payer/ChoosePayerView.swift | 2 +- .../Payer/ChoosePayerViewModel.swift | 4 ++-- .../Detail Selection/SelectGroupViewModel.swift | 7 ++++++- .../ExpenseSplitOptionsViewModel.swift | 4 +++- Splito/UI/Home/Groups/GroupListViewModel.swift | 3 ++- 7 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Splito/Localization/Localizable.xcstrings b/Splito/Localization/Localizable.xcstrings index f6dfacca8..c3972da3d 100644 --- a/Splito/Localization/Localizable.xcstrings +++ b/Splito/Localization/Localizable.xcstrings @@ -625,7 +625,10 @@ "Please fill all data to add expense." : { "extractionState" : "manual" }, - "Please select group to get payer list." : { + "Please select a group before choosing the split option." : { + "extractionState" : "manual" + }, + "Please select a group to get payer list." : { "extractionState" : "manual" }, "Privacy Policy" : { diff --git a/Splito/UI/Home/Expense/AddExpenseViewModel.swift b/Splito/UI/Home/Expense/AddExpenseViewModel.swift index 1fdb0b733..852b1db58 100644 --- a/Splito/UI/Home/Expense/AddExpenseViewModel.swift +++ b/Splito/UI/Home/Expense/AddExpenseViewModel.swift @@ -72,7 +72,7 @@ class AddExpenseViewModel: BaseViewModel, ObservableObject { // MARK: - Data Loading private func fetchGroup(groupId: String) async { - guard let id = preference.user?.id else { return } + guard let userId = preference.user?.id else { return } do { viewState = .loading @@ -82,7 +82,7 @@ class AddExpenseViewModel: BaseViewModel, ObservableObject { groupMembers = group.members selectedMembers = group.members } - selectedPayers = [id: expenseAmount] + selectedPayers = [userId: expenseAmount] viewState = .initial LogD("AddExpenseViewModel: \(#function) Group fetched successfully.") } catch { @@ -259,11 +259,11 @@ extension AddExpenseViewModel { func handlePayerBtnAction() { guard selectedGroup != nil else { - self.showToastFor(toast: ToastPrompt(type: .warning, title: "Whoops!", message: "Please select group to get payer list.")) + self.showToastFor(toast: ToastPrompt(type: .warning, title: "Whoops!", message: "Please select a group to get payer list.")) return } - if let user = preference.user, selectedPayers == [:] || selectedPayers[user.id] == 0 { - selectedPayers = [user.id: expenseAmount] + if let userId = preference.user?.id, selectedPayers == [:] || selectedPayers[userId] == 0 { + selectedPayers = [userId: expenseAmount] } showPayerSelection = true } @@ -275,7 +275,7 @@ extension AddExpenseViewModel { func handleSplitTypeBtnAction() { guard selectedGroup != nil else { showToastFor(toast: ToastPrompt(type: .warning, title: "Whoops!", - message: "Please select group to get payer list.")) + message: "Please select a group before choosing the split option.")) return } @@ -320,8 +320,9 @@ extension AddExpenseViewModel { } func handleSaveAction() async -> Bool { - if let user = preference.user, selectedPayers == [:] || selectedPayers[user.id] == 0 { - selectedPayers = [user.id: expenseAmount] + if let userId = preference.user?.id, selectedPayers == [:] || selectedPayers[userId] == 0 || + (selectedPayers.count == 1 && selectedPayers[userId] != expenseAmount) { + selectedPayers = [userId: expenseAmount] } if expenseName == "" || expenseAmount == 0 || selectedGroup == nil || selectedPayers == [:] || selectedPayers.values.reduce(0, +) != expenseAmount { diff --git a/Splito/UI/Home/Expense/Detail Selection/Payer/ChoosePayerView.swift b/Splito/UI/Home/Expense/Detail Selection/Payer/ChoosePayerView.swift index 56c9bb83a..5dcfc707c 100644 --- a/Splito/UI/Home/Expense/Detail Selection/Payer/ChoosePayerView.swift +++ b/Splito/UI/Home/Expense/Detail Selection/Payer/ChoosePayerView.swift @@ -44,7 +44,7 @@ struct ChoosePayerView: View { isSelected: (viewModel.selectedPayers.count > 1) ? false : viewModel.selectedPayers.keys.contains(user.id) ) .onTapGestureForced { - viewModel.handlePayerSelection(user: user) + viewModel.handlePayerSelection(userId: user.id) } } diff --git a/Splito/UI/Home/Expense/Detail Selection/Payer/ChoosePayerViewModel.swift b/Splito/UI/Home/Expense/Detail Selection/Payer/ChoosePayerViewModel.swift index b43bd4479..1df35bb58 100644 --- a/Splito/UI/Home/Expense/Detail Selection/Payer/ChoosePayerViewModel.swift +++ b/Splito/UI/Home/Expense/Detail Selection/Payer/ChoosePayerViewModel.swift @@ -56,8 +56,8 @@ class ChoosePayerViewModel: BaseViewModel, ObservableObject { } // MARK: - User Actions - func handlePayerSelection(user: AppUser) { - selectedPayers = [user.id: amount] + func handlePayerSelection(userId: String) { + selectedPayers = [userId: amount] } func handleMultiplePayerTap() { diff --git a/Splito/UI/Home/Expense/Detail Selection/SelectGroupViewModel.swift b/Splito/UI/Home/Expense/Detail Selection/SelectGroupViewModel.swift index 7dd4ab642..f1f9b9785 100644 --- a/Splito/UI/Home/Expense/Detail Selection/SelectGroupViewModel.swift +++ b/Splito/UI/Home/Expense/Detail Selection/SelectGroupViewModel.swift @@ -34,8 +34,13 @@ class SelectGroupViewModel: BaseViewModel, ObservableObject { // MARK: - Data Loading private func fetchGroups() async { + guard let userId = preference.user?.id else { + currentViewState = .noGroups + return + } + do { - let (groups, _) = try await groupRepository.fetchGroupsBy(userId: preference.user?.id ?? "") + let (groups, _) = try await groupRepository.fetchGroupsBy(userId: userId) currentViewState = groups.isEmpty ? .noGroups : .hasGroups(groups: groups) LogD("SelectGroupViewModel: \(#function) Groups fetched successfully.") } catch { diff --git a/Splito/UI/Home/Expense/Expense Split Option/ExpenseSplitOptionsViewModel.swift b/Splito/UI/Home/Expense/Expense Split Option/ExpenseSplitOptionsViewModel.swift index 169f107b1..ef756a719 100644 --- a/Splito/UI/Home/Expense/Expense Split Option/ExpenseSplitOptionsViewModel.swift +++ b/Splito/UI/Home/Expense/Expense Split Option/ExpenseSplitOptionsViewModel.swift @@ -146,8 +146,10 @@ class ExpenseSplitOptionsViewModel: BaseViewModel, ObservableObject { } func handleAllBtnAction() { + guard let userId = preference.user?.id else { return } + if isAllSelected { - selectedMembers = [preference.user?.id ?? ""] + selectedMembers = [userId] } else { selectedMembers = members } diff --git a/Splito/UI/Home/Groups/GroupListViewModel.swift b/Splito/UI/Home/Groups/GroupListViewModel.swift index f23a2d2b3..bb3661355 100644 --- a/Splito/UI/Home/Groups/GroupListViewModel.swift +++ b/Splito/UI/Home/Groups/GroupListViewModel.swift @@ -379,7 +379,8 @@ extension GroupListViewModel { } private func processGroup(group: Groups, isNewGroup: Bool) async { - let userId = preference.user?.id ?? "" + guard let userId = preference.user?.id else { return } + let memberBalance = getMembersBalance(group: group, memberId: userId) let memberOwingAmount = calculateExpensesSimplified(userId: userId, memberBalances: group.balances)