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() + } + } + } }