Skip to content

Commit

Permalink
fix issue #13 - lists wont show more than 20 movies - udacity/ios-nd-…
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent Mifsud committed Jun 2, 2019
1 parent bc0231c commit fb367e1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
34 changes: 30 additions & 4 deletions TheMovieManager/Controller/SearchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class SearchViewController: UIViewController {
var selectedIndex = 0

var currentSearchTask: URLSessionTask?

var currentPageNumber: Int = 0

var maxPageCount: Int = 0

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
Expand All @@ -32,10 +36,13 @@ extension SearchViewController: UISearchBarDelegate {

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
currentSearchTask?.cancel()
currentSearchTask = TMDBClient.searchForMovie(query: searchText) { (movies, error) in
if let movies = movies {
self.movies = movies
self.tableView.reloadData()
currentSearchTask = TMDBClient.searchForMovie(query: searchText) { (movieResults, error) in
if let movieResults = movieResults {
weak var searchVC = self
searchVC?.movies = movieResults.results
searchVC?.currentPageNumber = movieResults.page
searchVC?.maxPageCount = movieResults.totalPages
searchVC?.tableView.reloadData()
}
}
}
Expand Down Expand Up @@ -93,5 +100,24 @@ extension SearchViewController: UITableViewDataSource, UITableViewDelegate {
performSegue(withIdentifier: "showDetail", sender: nil)
tableView.deselectRow(at: indexPath, animated: true)
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard currentPageNumber > 0 else { return }
guard maxPageCount > 0 else { return }
guard currentPageNumber + 1 <= maxPageCount else { return }
guard indexPath.row == movies.count-1 else { return }

if let query = searchBar.text {
TMDBClient.searchForMovie(query: query, page: currentPageNumber + 1) { (movieResults, error) in
if let movieResults = movieResults {
weak var searchVC = self
searchVC?.movies.append(contentsOf: movieResults.results)
searchVC?.tableView.reloadData()
searchVC?.currentPageNumber = movieResults.page
searchVC?.maxPageCount = movieResults.totalPages
}
}
}
}

}
12 changes: 6 additions & 6 deletions TheMovieManager/Model/TMDB Client/TMDBClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TMDBClient {
case markWatchlist
case markFavorite
case getPosterImage(String)
case search(String)
case search(String, Int)

var stringValue: String {
switch self {
Expand All @@ -50,7 +50,7 @@ class TMDBClient {
case .markWatchlist: return Endpoints.base + "/account/\(Auth.accountId)/watchlist" + Endpoints.apiKeyParam + "&session_id=\(Auth.sessionId)"
case .markFavorite: return Endpoints.base + "/account/\(Auth.accountId)/favorite" + Endpoints.apiKeyParam + "&session_id=\(Auth.sessionId)"
case .getPosterImage(let posterPath): return Endpoints.basePoster + posterPath
case .search(let movieQuery): return Endpoints.base + "/search/movie" + Endpoints.apiKeyParam + "&query=\(movieQuery.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")"
case .search(let movieQuery, let page): return Endpoints.base + "/search/movie" + Endpoints.apiKeyParam + "&query=\(movieQuery.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")" + "&page=\(page)"
}
}

Expand Down Expand Up @@ -136,12 +136,12 @@ class TMDBClient {
}
}

class func searchForMovie(query: String, completion: @escaping ([Movie]?, Error?) -> Void) -> URLSessionTask {
let task = taskForGetRequest(url: Endpoints.search(query).url, responseType: MovieResults.self) { (response, error) in
@discardableResult class func searchForMovie(query: String, page: Int = 1, completion: @escaping (MovieResults?, Error?) -> Void) -> URLSessionTask {
let task = taskForGetRequest(url: Endpoints.search(query, page).url, responseType: MovieResults.self) { (response, error) in
if let response = response {
completion(response.results, nil)
completion(response, nil)
} else {
completion([], error)
completion(nil, error)
}
}
return task
Expand Down

0 comments on commit fb367e1

Please sign in to comment.