-
-
Save arthur-here/e6b036e6aaf4bd3df45cce954d0e4926 to your computer and use it in GitHub Desktop.
This file contains 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
/// 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