From d4cf3975f08b6a831378d40f51b42134a5a2f6cf Mon Sep 17 00:00:00 2001 From: Brent Mifsud Date: Sat, 1 Jun 2019 20:00:00 -0400 Subject: [PATCH] fix issue #8 - add table view refreshing to watchlist and favourites view controller - https://github.com/udacity/ios-nd-networking/issues/8 --- .../Controller/FavoritesViewController.swift | 39 +++++++++++++++---- .../Controller/WatchlistViewController.swift | 36 +++++++++++++---- 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/TheMovieManager/Controller/FavoritesViewController.swift b/TheMovieManager/Controller/FavoritesViewController.swift index 8328a72..f97e494 100644 --- a/TheMovieManager/Controller/FavoritesViewController.swift +++ b/TheMovieManager/Controller/FavoritesViewController.swift @@ -12,18 +12,23 @@ class FavoritesViewController: UIViewController { @IBOutlet weak var tableView: UITableView! - var selectedIndex = 0 + lazy var refresh: UIRefreshControl = { + let refreshControl = UIRefreshControl() + refreshControl.tintColor = .black + refreshControl.addTarget(self, action: #selector(refreshFavorites), for: .valueChanged) + + return refreshControl + }() + + + var selectedIndex = 0 override func viewDidLoad() { super.viewDidLoad() - _ = TMDBClient.getFavorites() { movies, error in - MovieModel.favorites = movies - DispatchQueue.main.async { - self.tableView.reloadData() - } - } - + tableView.refreshControl = refresh + + refreshFavorites() } override func viewWillAppear(_ animated: Bool) { @@ -38,6 +43,24 @@ class FavoritesViewController: UIViewController { detailVC.movie = MovieModel.favorites[selectedIndex] } } + + @objc func refreshFavorites() { + TMDBClient.getFavorites() { movies, error in + MovieModel.favorites = movies + + unowned let favoriteVC = self + + DispatchQueue.main.async { + favoriteVC.tableView.reloadData() + } + + let deadline = DispatchTime.now() + .milliseconds(500) + DispatchQueue.main.asyncAfter(deadline: deadline, execute: { + favoriteVC.refresh.endRefreshing() + }) + } + } + } extension FavoritesViewController: UITableViewDataSource, UITableViewDelegate { diff --git a/TheMovieManager/Controller/WatchlistViewController.swift b/TheMovieManager/Controller/WatchlistViewController.swift index fe9b669..a8997ea 100644 --- a/TheMovieManager/Controller/WatchlistViewController.swift +++ b/TheMovieManager/Controller/WatchlistViewController.swift @@ -11,18 +11,23 @@ import UIKit class WatchlistViewController: UIViewController { @IBOutlet weak var tableView: UITableView! + + lazy var refresh: UIRefreshControl = { + let refreshControl = UIRefreshControl() + refreshControl.tintColor = .black + refreshControl.addTarget(self, action: #selector(refreshWatchlist), for: .valueChanged) + + return refreshControl + }() var selectedIndex = 0 override func viewDidLoad() { super.viewDidLoad() + + tableView.refreshControl = refresh - _ = TMDBClient.getWatchlist() { movies, error in - MovieModel.watchlist = movies - DispatchQueue.main.async { - self.tableView.reloadData() - } - } + refreshWatchlist() } override func viewWillAppear(_ animated: Bool) { @@ -37,7 +42,24 @@ class WatchlistViewController: UIViewController { detailVC.movie = MovieModel.watchlist[selectedIndex] } } - + + @objc func refreshWatchlist() { + TMDBClient.getWatchlist() { movies, error in + MovieModel.watchlist = movies + + unowned let watchlistVC = self + + DispatchQueue.main.async { + watchlistVC.tableView.reloadData() + } + + let deadline = DispatchTime.now() + .milliseconds(500) + DispatchQueue.main.asyncAfter(deadline: deadline, execute: { + watchlistVC.refresh.endRefreshing() + }) + } + } + } extension WatchlistViewController: UITableViewDataSource, UITableViewDelegate {