Skip to content

Commit

Permalink
fix issue #10 - add network indicator when network requests are runni…
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent Mifsud committed Jun 2, 2019
1 parent 6f89097 commit 401deae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
7 changes: 6 additions & 1 deletion TheMovieManager/Controller/FavoritesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class FavoritesViewController: UIViewController {
detailVC.movie = MovieModel.favorites[selectedIndex]
}
}

}

extension FavoritesViewController: UITableViewDataSource, UITableViewDelegate {
Expand All @@ -53,6 +52,8 @@ extension FavoritesViewController: UITableViewDataSource, UITableViewDelegate {

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MovieTableViewCell")!

isDownloading(true)

let movie = MovieModel.favorites[indexPath.row]

Expand All @@ -62,8 +63,12 @@ extension FavoritesViewController: UITableViewDataSource, UITableViewDelegate {
if let posterPath = movie.posterPath {
TMDBClient.downloadPosterImage(posterPath: posterPath) { (data, error) in
guard let data = data else { return }

unowned let favoritesVC = self

cell.imageView?.image = UIImage(data: data)
cell.setNeedsLayout()
favoritesVC.isDownloading(false)
}
}

Expand Down
10 changes: 9 additions & 1 deletion TheMovieManager/Controller/SearchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ extension SearchViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MovieTableViewCell")!

let movie = movies[indexPath.row]
isDownloading(true)

let movie = movies[indexPath.row]

cell.textLabel?.text = "\(movie.title)"

Expand All @@ -87,8 +89,12 @@ extension SearchViewController: UITableViewDataSource, UITableViewDelegate {
if let posterPath = movie.posterPath {
TMDBClient.downloadPosterImage(posterPath: posterPath) { (data, error) in
guard let data = data else { return }

unowned let searchVC = self

cell.imageView?.image = UIImage(data: data)
cell.setNeedsLayout()
searchVC.isDownloading(false)
}
}

Expand All @@ -108,6 +114,7 @@ extension SearchViewController: UITableViewDataSource, UITableViewDelegate {
guard indexPath.row == movies.count-1 else { return }

if let query = searchBar.text {
isDownloading(true)
TMDBClient.searchForMovie(query: query, page: currentPageNumber + 1) { (movieResults, error) in
if let movieResults = movieResults {
weak var searchVC = self
Expand All @@ -117,6 +124,7 @@ extension SearchViewController: UITableViewDataSource, UITableViewDelegate {
searchVC?.maxPageCount = movieResults.totalPages
}
}
isDownloading(false)
}
}

Expand Down
4 changes: 4 additions & 0 deletions TheMovieManager/Controller/UIViewController+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ extension UIViewController {
}
}
}

func isDownloading(_ downloading: Bool) {
UIApplication.shared.isNetworkActivityIndicatorVisible = downloading
}
}
8 changes: 7 additions & 1 deletion TheMovieManager/Controller/WatchlistViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,22 @@ extension WatchlistViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MovieTableViewCell")!

let movie = MovieModel.watchlist[indexPath.row]
isDownloading(true)

let movie = MovieModel.watchlist[indexPath.row]

cell.textLabel?.text = movie.title
cell.imageView?.image = UIImage(named: "MoviePlaceholder")

if let posterPath = movie.posterPath {
TMDBClient.downloadPosterImage(posterPath: posterPath) { (data, error) in
guard let data = data else { return }

unowned let watchlistVC = self

cell.imageView?.image = UIImage(data: data)
cell.setNeedsLayout()
watchlistVC.isDownloading(false)
}
}

Expand Down

0 comments on commit 401deae

Please sign in to comment.