Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion GoMoney/Scences/AddExpense/AddExpenseForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,13 @@ class AddExpenseForm: UIView {

dateField.text = DateFormatter.dmy().string(from: transaction.occuredOn)
categoryField.text = transaction.tag?.name
amountField.text = String(transaction.amount)

let amountString = String(Int(transaction.amount))
.replacingOccurrences(of: ",", with: "")
.replacingOccurrences(of: ".", with: "")
let formated = amountString.splitFromEnd(by: 3).joined(separator: ",")
amountField.text = formated

noteField.text = transaction.note
}

Expand Down
35 changes: 30 additions & 5 deletions GoMoney/Scences/Home/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,19 @@ extension HomeViewController: UITableViewDataSource, UITableViewDelegate {
return swipe
}

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

self.navigationController?.pushViewController(vc, animated: true)
self.present(vc, animated: true)

completionHandler(true)
completionHandler(true)
}
}

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

private func applyTransaction(transaction: Expense, newTrans: Expense) {
viewModel.applyTransaction(transaction: transaction, newTrans: newTrans) { [weak self] err in
DispatchQueue.main.async {
if let err = err {
self?.alert(
title: "Error",
message: err.localizedDescription,
actionTitle: "Cancel"
)
} else {
self?.loadData()
self?.snackBar(
message: "Transaction updated successfully!",
actionText: "OK"
)
}
}
}
}

private func toggleEmptyView() {
let empty = viewModel.transactions?.count == 0
let contents = [backImage, chartView, recentExpenseLabel, tableView, floatingButton]
Expand Down
10 changes: 10 additions & 0 deletions GoMoney/ViewModel/Home/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,14 @@ class HomeViewModel {
}
completion?(nil)
}

func applyTransaction(transaction: Expense, newTrans: Expense, completion: @escaping (Error?) -> Void) {
DataService.shared.updateExpense(
oldTrans: transaction,
newTrans: newTrans,
completion: { err in
completion(err)
}
)
}
}