-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1e5cce
commit 9e5b4cc
Showing
8 changed files
with
101 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 0 additions & 108 deletions
108
Splito/UI/Home/Expense/Detail Selection/Notes/ExpenseAddNoteViewModel.swift
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
Splito/UI/Home/Expense/Notes/ExpenseAddNoteViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// ExpenseAddNoteViewModel.swift | ||
// Splito | ||
// | ||
// Created by Nirali Sonani on 27/11/24. | ||
// | ||
|
||
import Data | ||
import BaseStyle | ||
import Foundation | ||
|
||
class ExpenseAddNoteViewModel: BaseViewModel, ObservableObject { | ||
|
||
@Inject private var expenseRepository: ExpenseRepository | ||
|
||
@Published var expenseNote: String | ||
@Published private(set) var showLoader: Bool = false | ||
|
||
private let group: Groups? | ||
private let expense: Expense? | ||
private let handleSaveNoteTap: ((String) -> Void)? | ||
|
||
init(group: Groups?, expense: Expense?, expenseNote: String, handleSaveNoteTap: ((String) -> Void)? = nil) { | ||
self.group = group | ||
self.expense = expense | ||
self.expenseNote = expenseNote | ||
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(expenseNote) | ||
return true | ||
} | ||
|
||
guard let expense, expense.note != expenseNote else { return true } | ||
return await updateExpenseNote() | ||
} | ||
|
||
private func updateExpenseNote() async -> Bool { | ||
guard let group, let expense else { return false } | ||
|
||
do { | ||
showLoader = true | ||
var updatedExpense = expense | ||
updatedExpense.note = expenseNote | ||
|
||
try await expenseRepository.updateExpense(group: group, expense: updatedExpense, oldExpense: expense, type: .expenseUpdated) | ||
NotificationCenter.default.post(name: .updateExpense, object: updatedExpense) | ||
|
||
showLoader = false | ||
LogD("ExpenseAddNoteViewModel: \(#function) Expense note updated successfully.") | ||
return true | ||
} catch { | ||
LogE("ExpenseAddNoteViewModel: \(#function) Failed to update expense note: \(error).") | ||
showToastForError() | ||
return false | ||
} | ||
} | ||
} |