@@ -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]
0 commit comments