Skip to content

Commit 7c638b2

Browse files
update: home swipe to edit (#68)
1 parent 7b4af25 commit 7c638b2

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

GoMoney/Scences/AddExpense/AddExpenseForm.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,13 @@ class AddExpenseForm: UIView {
198198

199199
dateField.text = DateFormatter.dmy().string(from: transaction.occuredOn)
200200
categoryField.text = transaction.tag?.name
201-
amountField.text = String(transaction.amount)
201+
202+
let amountString = String(Int(transaction.amount))
203+
.replacingOccurrences(of: ",", with: "")
204+
.replacingOccurrences(of: ".", with: "")
205+
let formated = amountString.splitFromEnd(by: 3).joined(separator: ",")
206+
amountField.text = formated
207+
202208
noteField.text = transaction.note
203209
}
204210

GoMoney/Scences/Home/HomeViewController.swift

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,19 @@ extension HomeViewController: UITableViewDataSource, UITableViewDelegate {
211211
return swipe
212212
}
213213

214-
func tableView(_: UITableView, leadingSwipeActionsConfigurationForRowAt _: IndexPath) -> UISwipeActionsConfiguration? {
214+
func tableView(_: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
215215
let edit = UIContextualAction(style: .normal, title: "Edit") { _, _, completionHandler in
216-
// TODO: Go to edit
217-
let vc = GMMainViewController()
216+
if let transaction = self.viewModel.transactions?[indexPath.row] {
217+
let vc = EditViewController()
218+
vc.transaction = transaction
219+
vc.onApply = { [weak self] newTrans in
220+
self?.applyTransaction(transaction: transaction, newTrans: newTrans)
221+
}
218222

219-
self.navigationController?.pushViewController(vc, animated: true)
223+
self.present(vc, animated: true)
220224

221-
completionHandler(true)
225+
completionHandler(true)
226+
}
222227
}
223228

224229
edit.image = UIImage(systemName: "highlighter")
@@ -229,6 +234,26 @@ extension HomeViewController: UITableViewDataSource, UITableViewDelegate {
229234
return swipe
230235
}
231236

237+
private func applyTransaction(transaction: Expense, newTrans: Expense) {
238+
viewModel.applyTransaction(transaction: transaction, newTrans: newTrans) { [weak self] err in
239+
DispatchQueue.main.async {
240+
if let err = err {
241+
self?.alert(
242+
title: "Error",
243+
message: err.localizedDescription,
244+
actionTitle: "Cancel"
245+
)
246+
} else {
247+
self?.loadData()
248+
self?.snackBar(
249+
message: "Transaction updated successfully!",
250+
actionText: "OK"
251+
)
252+
}
253+
}
254+
}
255+
}
256+
232257
private func toggleEmptyView() {
233258
let empty = viewModel.transactions?.count == 0
234259
let contents = [backImage, chartView, recentExpenseLabel, tableView, floatingButton]

GoMoney/ViewModel/Home/HomeViewModel.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,14 @@ class HomeViewModel {
6868
}
6969
completion?(nil)
7070
}
71+
72+
func applyTransaction(transaction: Expense, newTrans: Expense, completion: @escaping (Error?) -> Void) {
73+
DataService.shared.updateExpense(
74+
oldTrans: transaction,
75+
newTrans: newTrans,
76+
completion: { err in
77+
completion(err)
78+
}
79+
)
80+
}
7181
}

0 commit comments

Comments
 (0)