Skip to content

Commit

Permalink
fix issue #8 - add table view refreshing to watchlist and favourites …
Browse files Browse the repository at this point in the history
…view controller - udacity/ios-nd-networking#8
  • Loading branch information
Brent Mifsud committed Jun 2, 2019
1 parent 9a2a947 commit d4cf397
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
39 changes: 31 additions & 8 deletions TheMovieManager/Controller/FavoritesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
36 changes: 29 additions & 7 deletions TheMovieManager/Controller/WatchlistViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit d4cf397

Please sign in to comment.