diff --git a/Splito/UI/Home/Expense/AddExpenseViewModel.swift b/Splito/UI/Home/Expense/AddExpenseViewModel.swift index 6725b5560..69a330fdc 100644 --- a/Splito/UI/Home/Expense/AddExpenseViewModel.swift +++ b/Splito/UI/Home/Expense/AddExpenseViewModel.swift @@ -459,7 +459,7 @@ extension AddExpenseViewModel { let imageData = resizedImage?.jpegData(compressionQuality: 0.2) return imageData } - + private func hasExpenseChanged(_ expense: Expense, oldExpense: Expense) -> Bool { return oldExpense.amount != expense.amount || oldExpense.paidBy != expense.paidBy || oldExpense.splitTo != expense.splitTo || oldExpense.splitType != expense.splitType || diff --git a/Splito/UI/Home/Expense/Note/AddNoteViewModel.swift b/Splito/UI/Home/Expense/Note/AddNoteViewModel.swift index 18270b66a..b527d6392 100644 --- a/Splito/UI/Home/Expense/Note/AddNoteViewModel.swift +++ b/Splito/UI/Home/Expense/Note/AddNoteViewModel.swift @@ -10,19 +10,19 @@ import BaseStyle import Foundation class AddNoteViewModel: BaseViewModel, ObservableObject { - + @Inject private var userRepository: UserRepository @Inject private var expenseRepository: ExpenseRepository @Inject private var transactionRepository: TransactionRepository - + @Published var note: String @Published private(set) var showLoader: Bool = false - + private let group: Groups? private let expense: Expense? private let payment: Transactions? private let handleSaveNoteTap: ((String) -> Void)? - + init(group: Groups?, expense: Expense? = nil, payment: Transactions? = nil, note: String, handleSaveNoteTap: ((String) -> Void)? = nil) { self.group = group self.expense = expense @@ -31,38 +31,38 @@ class AddNoteViewModel: BaseViewModel, ObservableObject { self.handleSaveNoteTap = handleSaveNoteTap super.init() } - + // MARK: - User Actions func showSaveFailedError() { self.showToastFor(toast: ToastPrompt(type: .error, title: "Oops", message: "Failed to save note.")) } - + func handleSaveNoteAction() async -> Bool { if let handleSaveNoteTap { handleSaveNoteTap(note) return true } - + if let expense, expense.note != note { return await updateExpenseNote() } else if let payment, payment.note != note { return await updatePaymentNote() } - + return true } - + private func updateExpenseNote() async -> Bool { guard let group, let expense else { return false } - + do { showLoader = true var updatedExpense = expense updatedExpense.note = note - + updatedExpense = try await expenseRepository.updateExpense(group: group, expense: updatedExpense, oldExpense: expense, type: .expenseUpdated) NotificationCenter.default.post(name: .updateExpense, object: updatedExpense) - + showLoader = false LogD("AddNoteViewModel: \(#function) Expense note updated successfully.") return true @@ -73,10 +73,10 @@ class AddNoteViewModel: BaseViewModel, ObservableObject { return false } } - + private func updatePaymentNote() async -> Bool { guard let group, let payment else { return false } - + do { showLoader = true let members = try await fetchMembers(payerId: payment.payerId, receiverId: payment.receiverId) @@ -84,14 +84,14 @@ class AddNoteViewModel: BaseViewModel, ObservableObject { showLoader = false return false } - + var updatedPayment = payment updatedPayment.note = note updatedPayment = try await transactionRepository.updateTransaction(group: group, transaction: updatedPayment, oldTransaction: payment, members: members, type: .transactionUpdated) NotificationCenter.default.post(name: .updateTransaction, object: updatedPayment) - + showLoader = false LogD("AddNoteViewModel: \(#function) Payment note updated successfully.") return true @@ -102,11 +102,11 @@ class AddNoteViewModel: BaseViewModel, ObservableObject { return false } } - + private func fetchMembers(payerId: String, receiverId: String) async throws -> (payer: AppUser, receiver: AppUser)? { let payer = try await userRepository.fetchUserBy(userID: payerId) let receiver = try await userRepository.fetchUserBy(userID: receiverId) - + if let payer, let receiver { return (payer, receiver) } diff --git a/Splito/UI/Home/Groups/Create Group/CreateGroupViewModel.swift b/Splito/UI/Home/Groups/Create Group/CreateGroupViewModel.swift index 018cf5e97..3baa59742 100644 --- a/Splito/UI/Home/Groups/Create Group/CreateGroupViewModel.swift +++ b/Splito/UI/Home/Groups/Create Group/CreateGroupViewModel.swift @@ -142,7 +142,7 @@ class CreateGroupViewModel: BaseViewModel, ObservableObject { return false } } - + private func getImageData() -> Data? { let resizedImage = profileImage?.aspectFittedToHeight(200) let imageData = resizedImage?.jpegData(compressionQuality: 0.2) diff --git a/Splito/UI/Home/Groups/Group/Group Options/Settle up/Payment/GroupPaymentViewModel.swift b/Splito/UI/Home/Groups/Group/Group Options/Settle up/Payment/GroupPaymentViewModel.swift index c8b88e5cd..04402368b 100644 --- a/Splito/UI/Home/Groups/Group/Group Options/Settle up/Payment/GroupPaymentViewModel.swift +++ b/Splito/UI/Home/Groups/Group/Group Options/Settle up/Payment/GroupPaymentViewModel.swift @@ -257,7 +257,7 @@ class GroupPaymentViewModel: BaseViewModel, ObservableObject { self.transaction = try await transactionRepository.addTransaction(group: group, transaction: transaction, members: (payer, receiver), imageData: getImageData()) - NotificationCenter.default.post(name: .addTransaction, object: transaction) + NotificationCenter.default.post(name: .addTransaction, object: self.transaction) await updateGroupMemberBalance(updateType: .Add) showLoader = false @@ -306,7 +306,7 @@ class GroupPaymentViewModel: BaseViewModel, ObservableObject { let imageData = resizedImage?.jpegData(compressionQuality: 0.2) return imageData } - + private func hasTransactionChanged(_ transaction: Transactions, oldTransaction: Transactions) -> Bool { return oldTransaction.payerId != transaction.payerId || oldTransaction.receiverId != transaction.receiverId || oldTransaction.amount != transaction.amount || oldTransaction.isActive != transaction.isActive diff --git a/Splito/UI/Home/Groups/Group/Group Options/Transactions/Transaction Detail/GroupTransactionDetailViewModel.swift b/Splito/UI/Home/Groups/Group/Group Options/Transactions/Transaction Detail/GroupTransactionDetailViewModel.swift index 319e984ed..757b56458 100644 --- a/Splito/UI/Home/Groups/Group/Group Options/Transactions/Transaction Detail/GroupTransactionDetailViewModel.swift +++ b/Splito/UI/Home/Groups/Group/Group Options/Transactions/Transaction Detail/GroupTransactionDetailViewModel.swift @@ -260,6 +260,7 @@ class GroupTransactionDetailViewModel: BaseViewModel, ObservableObject { @objc private func getUpdatedTransaction(notification: Notification) { guard let updatedTransaction = notification.object as? Transactions else { return } transaction = updatedTransaction + paymentNote = updatedTransaction.note ?? "" } // MARK: - Error Handling