Skip to content

Commit

Permalink
Note getting note after adding in payment detail
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Nov 28, 2024
1 parent 103db69 commit c1e4f03
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Splito/UI/Home/Expense/AddExpenseViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
36 changes: 18 additions & 18 deletions Splito/UI/Home/Expense/Note/AddNoteViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -73,25 +73,25 @@ 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)
guard let members else {
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
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c1e4f03

Please sign in to comment.