Skip to content

Commit

Permalink
fix issue #11 - add swipe to delete functionality - udacity/ios-nd-ne…
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent Mifsud committed Jun 2, 2019
1 parent 885c5ad commit 074a9b8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
17 changes: 17 additions & 0 deletions TheMovieManager/Controller/FavoritesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}

}
18 changes: 17 additions & 1 deletion TheMovieManager/Controller/WatchlistViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
}

0 comments on commit 074a9b8

Please sign in to comment.