From 9f4a24d6e71591339078d0005ab608e142ffe4ea Mon Sep 17 00:00:00 2001 From: Brent Mifsud Date: Sat, 1 Jun 2019 18:47:54 -0400 Subject: [PATCH] fix issue #11 - add swipe to delete functionality - https://github.com/udacity/ios-nd-networking/issues/11 --- .../Controller/FavoritesViewController.swift | 17 +++++++++++++++++ .../Controller/WatchlistViewController.swift | 18 +++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/TheMovieManager/Controller/FavoritesViewController.swift b/TheMovieManager/Controller/FavoritesViewController.swift index 0f304aa..dc92146 100644 --- a/TheMovieManager/Controller/FavoritesViewController.swift +++ b/TheMovieManager/Controller/FavoritesViewController.swift @@ -75,5 +75,22 @@ extension FavoritesViewController: UITableViewDataSource, UITableViewDelegate { performSegue(withIdentifier: "showDetail", sender: nil) tableView.deselectRow(at: indexPath, animated: true) } + + func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { + return true + } + + func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { + guard editingStyle == .delete else { return } + + let selectedMovie = MovieModel.favorites[indexPath.row] + + TMDBClient.markFavorite(movieId: selectedMovie.id, favorite: false) { (success, error) in + if success { + MovieModel.favorites.remove(at: indexPath.row) + tableView.reloadData() + } + } + } } diff --git a/TheMovieManager/Controller/WatchlistViewController.swift b/TheMovieManager/Controller/WatchlistViewController.swift index c4abf00..d627411 100644 --- a/TheMovieManager/Controller/WatchlistViewController.swift +++ b/TheMovieManager/Controller/WatchlistViewController.swift @@ -74,5 +74,21 @@ extension WatchlistViewController: UITableViewDataSource, UITableViewDelegate { performSegue(withIdentifier: "showDetail", sender: nil) tableView.deselectRow(at: indexPath, animated: true) } - + + func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { + return true + } + + func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { + guard editingStyle == .delete else { return } + + let selectedMovie = MovieModel.watchlist[indexPath.row] + + TMDBClient.markWatchlist(movieId: selectedMovie.id, watchlist: false) { (success, error) in + if success { + MovieModel.watchlist.remove(at: indexPath.row) + tableView.reloadData() + } + } + } }