Created
May 7, 2017 20:36
-
-
Save anuragajwani/70da00c6b6a3d2a05faaf43be908094e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
final class MoviesViewCoordinator { | |
// MARK: - Instance dependencies | |
private let navigationController: UINavigationController | |
// MARK: - Instance state | |
private var viewController: MoviesViewController! | |
// MARK: - Initializers | |
init(navigationController: UINavigationController) { | |
self.navigationController = navigationController | |
} | |
// MARK: - Coordinator functions | |
func start() { | |
self.viewController = MoviesViewController() | |
self.fetchMovies { [unowned self] movies in | |
let viewModel = self.convert(movies: movies) | |
self.viewController.set(viewModel: viewModel) | |
} | |
self.navigationController.pushViewController(self.viewController, animated: false) | |
} | |
// MARK: - Helper functions | |
private func fetchMovies(onSuccess: ([Movie]) -> Void) { | |
let exampleMovie = Movie(title: "Movie title", year: "2017", genre: ["Action", "Adventure"]) | |
onSuccess([exampleMovie]) | |
} | |
private func convert(movies: [Movie]) -> MoviesViewModel { | |
let movieCellViewModels = movies.map { movie in | |
MovieCellViewModel( | |
textLabel: "\(movie.title) (\(movie.year))", | |
detailTextLabel: movie.genre.joined(separator: ",") | |
) | |
} | |
return MoviesViewModel(movies: movieCellViewModels) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment