<commit_before>//
//  UIKitComponentManager.swift
//  PortalView
//
//  Created by <NAME> on 2/14/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit

public final class UIKitComponentManager<MessageType>: Presenter, Renderer {
    
    public var isDebugModeEnabled: Bool = false
    
    public let mailbox = Mailbox<MessageType>()
    
    fileprivate let layoutEngine: LayoutEngine
    
    fileprivate var window: WindowManager<MessageType, UIKitComponentRenderer<MessageType>>
    
    public init(window: UIWindow, layoutEngine: LayoutEngine = YogaLayoutEngine()) {
        self.window = WindowManager(window: window)
        self.layoutEngine = layoutEngine
    }
    
    public func present(component: Component<MessageType>, with root: RootComponent<MessageType>, modally: Bool) {
        switch (window.rootController, root, modally) {
        
        case (.some(.single(let presenter)), _, true):
            presentModally(component: component, root: root, onTopOf: presenter)
        
        case (.some(.navigationController(let presenter, _)), _, true):
            presentModally(component: component, root: root, onTopOf: presenter)
        
        case (.some(.navigationController(let navigationController, _)), .stack(let navigationBar), false):
            let containedController = controller(for: component)
            navigationController.push(controller: containedController, with: navigationBar, animated: true)
            
        default:
            let rootController = controller(for: component, root: root)
            window.rootController = rootController
            rootController.mailbox.forward(to: mailbox)
        }
    }
    
    public func render(component: Component<MessageType>) -> Mailbox<MessageType> {
        switch window.rootController {
            
        case .some(.single(let controller)):
            controller.component = component
            controller.render()
            controller.mailbox.forward(to: mailbox)
            return controller.mailbox
            
        case .some(.navigationController(_, let topController)):
            topController.component = component
            topController.render()
            topController.mailbox.forward(to: mailbox)
            return topController.mailbox
            
        default:
            let rootController = controller(for: component)
            window.rootController = .single(rootController)
            rootController.mailbox.forward(to: mailbox)
            return rootController.mailbox
            
        }
    }<commit_msg>Avoid double message dispatch.<commit_after>//
//  UIKitComponentManager.swift
//  PortalView
//
//  Created by <NAME> on 2/14/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit

public final class UIKitComponentManager<MessageType>: Presenter, Renderer {
    
    public var isDebugModeEnabled: Bool = false
    
    public let mailbox = Mailbox<MessageType>()
    
    fileprivate let layoutEngine: LayoutEngine
    
    fileprivate var window: WindowManager<MessageType, UIKitComponentRenderer<MessageType>>
    
    public init(window: UIWindow, layoutEngine: LayoutEngine = YogaLayoutEngine()) {
        self.window = WindowManager(window: window)
        self.layoutEngine = layoutEngine
    }
    
    public func present(component: Component<MessageType>, with root: RootComponent<MessageType>, modally: Bool) {
        switch (window.rootController, root, modally) {
        
        case (.some(.single(let presenter)), _, true):
            presentModally(component: component, root: root, onTopOf: presenter)
        
        case (.some(.navigationController(let presenter, _)), _, true):
            presentModally(component: component, root: root, onTopOf: presenter)
        
        case (.some(.navigationController(let navigationController, _)), .stack(let navigationBar), false):
            let containedController = controller(for: component)
            navigationController.push(controller: containedController, with: navigationBar, animated: true)
            
        default:
            let rootController = controller(for: component, root: root)
            window.rootController = rootController
            rootController.mailbox.forward(to: mailbox)
        }
    }
    
    public func render(component: Component<MessageType>) -> Mailbox<MessageType> {
        switch window.rootController {
            
        case .some(.single(let controller)):
            controller.component = component
            controller.render()
            return controller.mailbox
            
        case .some(.navigationController(_, let topController)):
            topController.component = component
            topController.render()
            return topController.mailbox
            
        default:
            let rootController = controller(for: component)
            window.rootController = .single(rootController)
            rootController.mailbox.forward(to: mailbox)
            return rootController.mailbox
            
        }
    }
{'fraction_non_alphanumeric': 0.08831628638867034, 'fraction_numerical': 0.0035405192761605035, 'mean_word_length': 2.4804928131416837, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2637248', 'n_tokens_mistral': 1254, 'n_tokens_neox': 1216, 'n_words': 301}