Created
June 7, 2020 18:39
-
-
Save DhavalDobariya86/cb1915eb14e40206fa1d741896e764e7 to your computer and use it in GitHub Desktop.
MVVM-C Coordinator
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 CountryListCoordinator: Coordinator { | |
private enum LocalizedStrings { | |
static let okTitle = "Ok" | |
static let errorTitle = "Error" | |
static let errorMessage = "Could not search country for the keyword." | |
} | |
var finished: (() -> Void)? | |
var childCoordinators: [Coordinator]? | |
private var childNavigationController: UINavigationController? | |
private weak var viewController: CountryListViewController? | |
private let requestManager: RequestManager | |
init(requestManager: RequestManager) { | |
self.requestManager = requestManager | |
} | |
func start() -> UIViewController { | |
let viewController: CountryListViewController = UIStoryboard.mainStoryBoard().instantiateViewController() | |
self.viewController = viewController | |
let countryListViewModel = CountryListViewModel(requestManager: requestManager) | |
countryListViewModel.loadingErrorDelegate = self | |
viewController.configure(viewModel: countryListViewModel) | |
viewController.delegate = self | |
return viewController | |
} | |
} | |
extension CountryListCoordinator: CountryListViewControllerDelegate { | |
func didSelect(country: Country, in viewController: UIViewController) { | |
let coordinator = CountryDetailCoordinator(country: country) | |
let detailViewController = coordinator.start() | |
let childNavigationController = UINavigationController(rootViewController: detailViewController) | |
self.childNavigationController = childNavigationController | |
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, | |
target: self, | |
action: #selector(doneButtonClicked)) | |
detailViewController.navigationItem.leftBarButtonItem = doneButton | |
viewController.present(childNavigationController, animated: true, completion: nil) | |
} | |
@objc private func doneButtonClicked(_ sender: UIBarButtonItem) { | |
childNavigationController?.dismiss(animated: true, completion: nil) | |
} | |
} | |
extension CountryListCoordinator: CountryListViewModelLoadingErrorDelegate { | |
func didFailLoading(error: Error) { | |
guard let viewController = viewController else { | |
assertionFailure("viewController of CountryListCoordinator can not be nil.\(#function)") | |
return | |
} | |
let alert = UIAlertController(title: LocalizedStrings.errorTitle, | |
message: LocalizedStrings.errorMessage, | |
preferredStyle: .alert) | |
let okAction = UIAlertAction(title: LocalizedStrings.okTitle, | |
style: .default, | |
handler: nil) | |
alert.addAction(okAction) | |
viewController.present(alert, animated: true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment