Skip to content

Instantly share code, notes, and snippets.

@arthur-here
Last active July 24, 2017 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthur-here/e6b036e6aaf4bd3df45cce954d0e4926 to your computer and use it in GitHub Desktop.
Save arthur-here/e6b036e6aaf4bd3df45cce954d0e4926 to your computer and use it in GitHub Desktop.
/// Type that defines possible coordination results of the `LanguageListCoordinator`.
///
/// - language: Language was choosen.
/// - cancel: Cancel button was tapped.
enum LanguageListCoordinationResult {
case language(String)
case cancel
}
class LanguageListCoordinator: BaseCoordinator<LanguageListCoordinationResult> {
private let rootViewController: UIViewController
init(rootViewController: UIViewController) {
self.rootViewController = rootViewController
}
override func start() -> Observable<CoordinationResult> {
// Initialize a View Controller from the storyboard and put it into the UINavigationController stack
let viewController = LanguageListViewController.initFromStoryboard(name: "Main")
let navigationController = UINavigationController(rootViewController: viewController)
// Initialize a View Model and inject it into the View Controller
let viewModel = LanguageListViewModel()
viewController.viewModel = viewModel
// Map the outputs of the View Model to the LanguageListCoordinationResult type
let cancel = viewModel.didCancel.map { _ in CoordinationResult.cancel }
let language = viewModel.didSelectLanguage.map { CoordinationResult.language($0) }
// Present View Controller onto the provided rootViewController
rootViewController.present(navigationController, animated: true)
// Merge the mapped outputs of the view model, taking only the first emitted event and dismissing the View Controller on that event
return Observable.merge(cancel, language)
.take(1)
.do(onNext: { [weak self] _ in self?.rootViewController.dismiss(animated: true) })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment