<commit_before># ApplicationCoordinator
Based on the post about Application Coordinators [khanlou.com](http://khanlou.com/2015/10/coordinators-redux/) and Application Controller pattern description [martinfowler.com](http://martinfowler.com/eaaCatalog/applicationController.html).

My presentation and problem’s explanation: [speakerdeck.com](https://speakerdeck.com/andreypanov/introducing-application-coordinator)

Example provides very basic structure with 6 controllers and 5 coordinators with mock data and logic.
![](/str.jpg)

I used a protocol for coordinators in this example:
```swift
protocol Coordinatable: class {
    func start()
}
```
All flow controllers have a protocols (we need to configure blocks and handle callbacks in coordinators):
```swift
protocol ItemsFlowOutput: FlowControllerOutput {
    var authNeed: (() -> ())? { get set }
    var onItemSelect: (ItemList -> ())? { get set }
    var onCreateButtonTap: (() -> ())? { get set }
}
```
In this example I use factories for creating  coordinators and controllers (we can mock them in tests).
```swift
protocol CoordinatorFactory {
    func createItemCoordinator(navController navController: UINavigationController?) -> Coordinator
    func createItemCoordinator() -> Coordinator
    
    func createItemCreationCoordinatorBox() ->
        (coordinator: Coordinator,
        output: ItemCreateCoordinatorOutput,
        controllerForPresent: UIViewController?)
}
```
The base coordinator stores dependancies of child coordinators
```swift
class BaseCoordinator {
    
    var childCoordinators: [Coordinatable] = []
    
    func addDependancy(coordinator: Coordinatable) {
        childCoordinators.append(coordinator)
    }
    
    func removeDependancy(coordinator: Coordinatable) {
        guard childCoordinators.isEmpty == false else { return }
        
        for (index, element) in childCoordinators.enumerate() {
            if ObjectIdentifier(element) == ObjectIdentifier(coordinator) {
                childCoordinators.removeAtIndex(index)
            }
        }
    }
}
```
AppDelegate store lazy reference for the Application Coordinator
```swift
private lazy var applicationCoordinator: ApplicationCoordinator = {
        return ApplicationCoordinator(tabbarFlowOutput: self.window!.rootViewController as! TabbarFlowOutput,
                                      coordinatorFactory: CoordinatorFactoryImp())
    }()

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {<commit_msg>readme update<commit_after># ApplicationCoordinator
Based on the post about Application Coordinators [khanlou.com](http://khanlou.com/2015/10/coordinators-redux/) and Application Controller pattern description [martinfowler.com](http://martinfowler.com/eaaCatalog/applicationController.html).

My presentation and problem’s explanation: [speakerdeck.com](https://speakerdeck.com/andreypanov/introducing-application-coordinator)

Example provides very basic structure with 6 controllers and 5 coordinators with mock data and logic.
![](/str.jpg)

I used a protocol for coordinators in this example:
```swift
protocol Coordinatable: class {
    func start()
}
```
All flow controllers have a protocols (we need to configure blocks and handle callbacks in coordinators):
```swift
protocol ItemsListFlowOutput: FlowControllerOutput {
    var authNeed: (() -> ())? { get set }
    var onItemSelect: (ItemList -> ())? { get set }
    var onCreateButtonTap: (() -> ())? { get set }
}
```
In this example I use factories for creating  coordinators and controllers (we can mock them in tests).
```swift
protocol CoordinatorFactory {
    func createItemCoordinator(navController navController: UINavigationController?) -> Coordinator
    func createItemCoordinator() -> Coordinator
    
    func createItemCreationCoordinatorBox() ->
        (coordinator: Coordinator,
        output: ItemCreateCoordinatorOutput,
        controllerForPresent: UIViewController?)
}
```
The base coordinator stores dependancies of child coordinators
```swift
class BaseCoordinator {
    
    var childCoordinators: [Coordinatable] = []
    
    func addDependancy(coordinator: Coordinatable) {
        childCoordinators.append(coordinator)
    }
    
    func removeDependancy(coordinator: Coordinatable) {
        guard childCoordinators.isEmpty == false else { return }
        
        for (index, element) in childCoordinators.enumerate() {
            if ObjectIdentifier(element) == ObjectIdentifier(coordinator) {
                childCoordinators.removeAtIndex(index)
            }
        }
    }
}
```
AppDelegate store lazy reference for the Application Coordinator
```swift
private lazy var applicationCoordinator: Coordinator = {
        let tabbarFlowOutput = self.window!.rootViewController as! TabbarFlowOutput
        return ApplicationCoordinator(tabbarFlowOutput: tabbarFlowOutput,
                                      coordinatorFactory: CoordinatorFactoryImp())
    }()

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
{'fraction_non_alphanumeric': 0.08885864281547735, 'fraction_numerical': 0.003111024693758507, 'mean_word_length': 3.9941747572815536, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2287277', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1292, 'n_words': 378}