diff --git a/Box-Office-Movies/Box-Office-Movies/Resources/Poster.storyboard b/Box-Office-Movies/Box-Office-Movies/Resources/Poster.storyboard index f7a331a..8ecb97b 100644 --- a/Box-Office-Movies/Box-Office-Movies/Resources/Poster.storyboard +++ b/Box-Office-Movies/Box-Office-Movies/Resources/Poster.storyboard @@ -18,6 +18,9 @@ + + + @@ -26,7 +29,9 @@ + + diff --git a/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesInteractor.swift b/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesInteractor.swift index 257cf29..418894c 100644 --- a/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesInteractor.swift +++ b/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesInteractor.swift @@ -10,9 +10,9 @@ import Box_Office_Movies_Core import UIKit protocol NowPlayingMoviesDataStore { - var movies: [Movie] { get } - var favoriteMovies: [Movie] { get } - var filteredMovies: [Movie] { get } + var movies: [Movie]? { get } + var favoriteMovies: [Movie]? { get } + var filteredMovies: [Movie]? { get } var state: State { get } } @@ -34,11 +34,11 @@ class NowPlayingMoviesInteractor: NowPlayingMoviesDataStore { var page = 1 var paginatedMovieLists = [PaginatedMovieList]() - var movies = [Movie]() - var favoriteMovies = [Movie]() - var filteredMovies = [Movie]() + var movies: [Movie]? + var favoriteMovies: [Movie]? + var filteredMovies: [Movie]? - var currentMovies: [Movie] { + var currentMovies: [Movie]? { switch state { case .allMovies: return movies @@ -59,7 +59,7 @@ extension NowPlayingMoviesInteractor: NowPlayingMoviesBusinessLogic { func fetchNowPlayingMovies(request: NowPlayingMovies.FetchNowPlayingMovies.Request) { state = .allMovies - if movies.isEmpty { + if movies == nil { fetchNowPlayingMovies { [weak self] error in let response = NowPlayingMovies.FetchNowPlayingMovies.Response(movies: self?.movies, error: error) self?.presenter?.presentNowPlayingMovies(response: response) @@ -90,7 +90,7 @@ extension NowPlayingMoviesInteractor: NowPlayingMoviesBusinessLogic { let isFiltering = request.isSearchControllerActive && !request.searchText.isEmpty if isFiltering { - filteredMovies = currentMovies.filter { movie -> Bool in + filteredMovies = currentMovies?.filter { movie -> Bool in return movie.title.lowercased().contains(request.searchText.lowercased()) == true } } else { @@ -104,8 +104,8 @@ extension NowPlayingMoviesInteractor: NowPlayingMoviesBusinessLogic { func refreshMovies(request: NowPlayingMovies.RefreshMovies.Request) { page = 1 paginatedMovieLists.removeAll() - movies.removeAll() - filteredMovies.removeAll() + movies?.removeAll() + filteredMovies?.removeAll() state = .allMovies fetchNowPlayingMovies { [weak self] error in @@ -133,10 +133,10 @@ extension NowPlayingMoviesInteractor { self?.paginatedMovieLists.append(paginatedMovieList) self?.page += 1 } - self?.movies.removeAll() - self?.paginatedMovieLists.forEach({ (paginatedMovieList) in - self?.movies.append(contentsOf: paginatedMovieList.movies) - }) + self?.movies = [] + self?.paginatedMovieLists.forEach { (paginatedMovieList) in + self?.movies?.append(contentsOf: paginatedMovieList.movies) + } completionHandler?(error) } } @@ -154,11 +154,13 @@ extension NowPlayingMoviesInteractor { } func removeMovieFromFavorites(request: NowPlayingMovies.RemoveMovieFromFavorites.Request) { - guard favoriteMovies.indices.contains(request.indexPathForMovieToRemove.row) else { + guard + favoriteMovies?.indices.contains(request.indexPathForMovieToRemove.row) == true, + let favoriteMovieToRemove = favoriteMovies?.remove(at: request.indexPathForMovieToRemove.row) + else { return } - let favoriteMovieToRemove = favoriteMovies.remove(at: request.indexPathForMovieToRemove.row) _ = ManagerProvider.shared.favoritesManager.removeMovieFromFavorites(favoriteMovieToRemove) let response = NowPlayingMovies.RemoveMovieFromFavorites.Response(movies: favoriteMovies, indexPathForMovieToRemove: request.indexPathForMovieToRemove, editButtonItem: request.editButtonItem) diff --git a/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesModels.swift b/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesModels.swift index 35217a0..dcb6517 100644 --- a/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesModels.swift +++ b/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesModels.swift @@ -90,7 +90,7 @@ enum NowPlayingMovies { struct Response { let state: State let searchText: String? - let movies: [Movie] + let movies: [Movie]? } struct ViewModel { diff --git a/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesPresenter.swift b/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesPresenter.swift index 1b344a7..7328d9b 100644 --- a/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesPresenter.swift +++ b/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesPresenter.swift @@ -79,7 +79,7 @@ extension NowPlayingMoviesPresenter: NowPlayingMoviesPresentationLogic { func presentTableViewBackgroundView(response: NowPlayingMovies.LoadTableViewBackgroundView.Response) { let backgroundView: UIView? = { guard - response.movies.isEmpty, + response.movies?.isEmpty == true, let emptyBackgroundView = EmptyBackgroundView.fromNib(named: Constants.NibName.emptyBackgroundView) as? EmptyBackgroundView else { return nil diff --git a/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesRouter.swift b/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesRouter.swift index a0ae818..90d294d 100644 --- a/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesRouter.swift +++ b/Box-Office-Movies/Box-Office-Movies/Scenes/NowPlayingMovies/NowPlayingMoviesRouter.swift @@ -38,7 +38,7 @@ extension NowPlayingMoviesRouter: NowPlayingMoviesRoutingLogic { private extension NowPlayingMoviesRouter { func passDataToMovieDetails(source: NowPlayingMoviesDataStore, destination: inout MovieDetailsDataStore) { - let movies: [Movie] = { + let optionalMovies: [Movie]? = { if viewController?.searchController.isActive == true { return source.filteredMovies } else { @@ -53,6 +53,7 @@ private extension NowPlayingMoviesRouter { guard let indexForSelectedRow = viewController?.nowPlayingMoviesTableView.indexPathForSelectedRow?.row, + let movies = optionalMovies, movies.indices.contains(indexForSelectedRow) else { return