Skip to content

Instantly share code, notes, and snippets.

@ayanonagon
Last active May 26, 2016 11:47
Show Gist options
  • Save ayanonagon/4b368fbf854bdf44b1d6 to your computer and use it in GitHub Desktop.
Save ayanonagon/4b368fbf854bdf44b1d6 to your computer and use it in GitHub Desktop.
import UIKit
protocol Coordinator {
func start()
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
private lazy var applicationCoordinator: ApplicationCoordinator = {
return ApplicationCoordinator(window: self.window!)
}()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
applicationCoordinator.start()
return true
}
}
class ApplicationCoordinator: Coordinator {
let window: UIWindow
let rootViewController: UITabBarController
let wordsNavigationController: UINavigationController
let phrasesNavigationController: UINavigationController
let wordsCoordinator: WordsCoordinator
let phrasesCoordinator: PhrasesCoordinator
init(window: UIWindow) {
self.window = window
self.rootViewController = UITabBarController()
self.wordsNavigationController = UINavigationController()
self.phrasesNavigationController = UINavigationController()
self.rootViewController.setViewControllers([wordsNavigationController, phrasesNavigationController], animated: false)
self.wordsCoordinator = WordsCoordinator(presenter: wordsNavigationController)
self.phrasesCoordinator = PhrasesCoordinator(presenter: phrasesNavigationController)
}
func start() {
window.rootViewController = rootViewController
window.tintColor = Color.oranje
wordsCoordinator.start()
phrasesCoordinator.start()
window.makeKeyAndVisible()
}
}
class WordsCoordinator: Coordinator {
let presenter: UINavigationController
private let listViewController: ListViewController<Word>
private let dataSource: WordsDataSource
init(presenter: UINavigationController) {
self.presenter = presenter
self.presenter.navigationBar.translucent = false
self.dataSource = WordsDataSource()
self.listViewController = ListViewController<Word>()
self.listViewController.title = "Words"
self.listViewController.items = dataSource.words
self.listViewController.configureCell = { cell, item in
cell.item = item
}
self.listViewController.didSelectItem = { item in
presenter.pushViewController(WordViewController(word: item), animated: true)
}
}
func start() {
presenter.pushViewController(listViewController, animated: false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment