Skip to content

Commit

Permalink
Added checks to stop wrong amount & fixed split option alert
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Dec 17, 2024
1 parent e029bc9 commit 9b25974
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
5 changes: 4 additions & 1 deletion Splito/Localization/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -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" : {
Expand Down
17 changes: 9 additions & 8 deletions Splito/UI/Home/Expense/AddExpenseViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion Splito/UI/Home/Groups/GroupListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 9b25974

Please sign in to comment.