Skip to content

Commit

Permalink
Merge pull request #12 from bilalb/feature/improve-movie-list-logic
Browse files Browse the repository at this point in the history
Feature/improve movie list logic
  • Loading branch information
bilalb authored Aug 8, 2019
2 parents 6c6e359 + e7c2cf5 commit e58016b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<activityIndicatorView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" animating="YES" style="gray" translatesAutoresizingMaskIntoConstraints="NO" id="aVr-3G-Btv">
<rect key="frame" x="197" y="465" width="20" height="20"/>
</activityIndicatorView>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="Ia3-f6-4fO">
<rect key="frame" x="16" y="104" width="382" height="742"/>
<gestureRecognizers/>
Expand All @@ -26,7 +29,9 @@
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="S7R-XN-Sp2" firstAttribute="trailing" secondItem="Ia3-f6-4fO" secondAttribute="trailing" constant="16" id="MUR-nk-ULY"/>
<constraint firstItem="aVr-3G-Btv" firstAttribute="centerX" secondItem="S7R-XN-Sp2" secondAttribute="centerX" id="NOd-pb-fic"/>
<constraint firstItem="Ia3-f6-4fO" firstAttribute="top" secondItem="S7R-XN-Sp2" secondAttribute="top" constant="16" id="QOd-8F-ltD"/>
<constraint firstItem="aVr-3G-Btv" firstAttribute="centerY" secondItem="S7R-XN-Sp2" secondAttribute="centerY" id="pnv-YM-4R7"/>
<constraint firstItem="S7R-XN-Sp2" firstAttribute="bottom" secondItem="Ia3-f6-4fO" secondAttribute="bottom" constant="16" id="qF6-Bv-v68"/>
<constraint firstItem="Ia3-f6-4fO" firstAttribute="leading" secondItem="S7R-XN-Sp2" secondAttribute="leading" constant="16" id="rQb-fy-HmK"/>
</constraints>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ enum NowPlayingMovies {
struct Response {
let state: State
let searchText: String?
let movies: [Movie]
let movies: [Movie]?
}

struct ViewModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -53,6 +53,7 @@ private extension NowPlayingMoviesRouter {

guard
let indexForSelectedRow = viewController?.nowPlayingMoviesTableView.indexPathForSelectedRow?.row,
let movies = optionalMovies,
movies.indices.contains(indexForSelectedRow)
else {
return
Expand Down

0 comments on commit e58016b

Please sign in to comment.