Skip to content

Instantly share code, notes, and snippets.

@anuragajwani
Created May 7, 2017 20:36
Show Gist options
  • Save anuragajwani/70da00c6b6a3d2a05faaf43be908094e to your computer and use it in GitHub Desktop.
Save anuragajwani/70da00c6b6a3d2a05faaf43be908094e to your computer and use it in GitHub Desktop.
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