<commit_before>//
//  Weact.swift
//  Weact
//
//  Created by <NAME> on 30/03/2017.
//  Copyright © 2017 Octopepper. All rights reserved.
//

// TODO 
//compoenent  fixed size ? override var intrinsicContentSize: CGSize { return CGSize(width: 79, height: 32) }

import UIKit

class WeactEngine {
    
    var renderBlock = {}
    
    convenience init() {
        self.init(renderer:UIKitRenderer())
    }
    
    let renderer:Renderer
    private init(renderer:Renderer) {
        self.renderer = renderer
        
        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue:"WeactStateChanged"), object: nil, queue: nil) { n in
            self.renderBlock()
        }
        
//        Timer.scheduledTimer(withTimeInterval: 10, repeats: true) { _ in
//            self.renderBlock()
//        }
    }
    
    func render<C: Component>(component:C, in view: UIView) {
        renderBlock = {
            // Clean
            for sv in view.subviews {
                sv.removeFromSuperview()
            }
            
            // Rerender
//            let renderedView = self.render(component: component)
            
            
            ///
//            let rootView = UIView()
            let node = component.render()
            self.renderer.render(node, in: view)
            
            
            
            //fill first node in container view
            if let firstNodeView = view.subviews.first {
                firstNodeView.fillContainer()
            }
            

            
            ///
            
//            renderedView.translatesAutoresizingMaskIntoConstraints = false
//            view.addSubview(renderedView)
//            renderedView.fillContainer()
        }
        renderBlock()
    }
    
    func render<C: Component>(component:C) -> UIView {
        let rootView = UIView()
        let newV = component.render()
        renderer.render(newV, in: rootView)
        return rootView
    }
}

class NodeView: UIView, IsNodeView {
    convenience init() {
        self.init(frame: CGRect.zero)
        backgroundColor = .white
        renderNode()
    }
    
    func render() -> Node {
        return View([])
    }
    
    func layoutPass() { }
    
    func didRender() { }
}

extension IsNodeView where Self: UIView {
    
    func renderNode() {
        let renderer = UIKitRenderer()
        renderer.render(nodeView: self)
    }
}

extension IsPropsView where Self: UIView {
    
    func renderNode() {
        let renderer = UIKitRenderer()
        renderer.render(nodeView: self)
    }
}

protocol IsNodeView {
    func render() -> Node
    func layoutPass()
    func didRender()
}


protocol IsPropsView {
    associatedtype Props
    var props: Props { get }
    func render(props: Props) -> Node
    func layoutPass()
    func didRender()
}


class PropsView<Props>: UIView, IsPropsView {
    
    var props: Props
    
    init(props: Props) {
        self.props = props
        super.init(frame: CGRect.zero)
        
        backgroundColor = .white
        renderNode()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func render(props: Props) -> Node {
        return View([])
    }
    
    func layoutPass() { }
    
    func didRender() { }
}


extension IsPropsView where Self: UIView {
    
    func didRender() {}
    
    func layoutPass() { }
}


// Helper to render a given compoennet in a UIView.
// By using this wrap, you can use componenets incrementally by migrating your UIView subclasses in your App.
class ComponentView<T:Component> :UIView {
    
    let component: T!
    
    let engine = WeactEngine()
    
    init(component: T) {
        self.component = component
        super.init(frame: CGRect.zero)
        engine.render(component:component, in: self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    override var intrinsicContentSize: CGSize {
//        return component.size()
//    }
}
<commit_msg>Adds ComponentVC to wrap components in a typical UIKit App<commit_after>//
//  Weact.swift
//  Weact
//
//  Created by <NAME> on 30/03/2017.
//  Copyright © 2017 Octopepper. All rights reserved.
//

// TODO 
//compoenent  fixed size ? override var intrinsicContentSize: CGSize { return CGSize(width: 79, height: 32) }

import UIKit

class WeactEngine {
    
    var renderBlock = {}
    
    convenience init() {
        self.init(renderer:UIKitRenderer())
    }
    
    let renderer:Renderer
    private init(renderer:Renderer) {
        self.renderer = renderer
        
        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue:"WeactStateChanged"), object: nil, queue: nil) { n in
            self.renderBlock()
        }
        
//        Timer.scheduledTimer(withTimeInterval: 10, repeats: true) { _ in
//            self.renderBlock()
//        }
    }
    
    func render<C: Component>(component:C, in view: UIView) {
        renderBlock = {
            // Clean
            for sv in view.subviews {
                sv.removeFromSuperview()
            }
            
            // Rerender
//            let renderedView = self.render(component: component)
            
            
            ///
//            let rootView = UIView()
            let node = component.render()
            self.renderer.render(node, in: view)
            
            
            
            //fill first node in container view
            if let firstNodeView = view.subviews.first {
                firstNodeView.fillContainer()
            }
            

            
            ///
            
//            renderedView.translatesAutoresizingMaskIntoConstraints = false
//            view.addSubview(renderedView)
//            renderedView.fillContainer()
        }
        renderBlock()
    }
    
    func render<C: Component>(component:C) -> UIView {
        let rootView = UIView()
        let newV = component.render()
        renderer.render(newV, in: rootView)
        return rootView
    }
}

class NodeView: UIView, IsNodeView {
    convenience init() {
        self.init(frame: CGRect.zero)
        backgroundColor = .white
        renderNode()
    }
    
    func render() -> Node {
        return View([])
    }
    
    func layoutPass() { }
    
    func didRender() { }
}

extension IsNodeView where Self: UIView {
    
    func renderNode() {
        let renderer = UIKitRenderer()
        renderer.render(nodeView: self)
    }
}

extension IsPropsView where Self: UIView {
    
    func renderNode() {
        let renderer = UIKitRenderer()
        renderer.render(nodeView: self)
    }
}

protocol IsNodeView {
    func render() -> Node
    func layoutPass()
    func didRender()
}


protocol IsPropsView {
    associatedtype Props
    var props: Props { get }
    func render(props: Props) -> Node
    func layoutPass()
    func didRender()
}


class PropsView<Props>: UIView, IsPropsView {
    
    var props: Props
    
    init(props: Props) {
        self.props = props
        super.init(frame: CGRect.zero)
        
        backgroundColor = .white
        renderNode()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func render(props: Props) -> Node {
        return View([])
    }
    
    func layoutPass() { }
    
    func didRender() { }
}


extension IsPropsView where Self: UIView {
    
    func didRender() {}
    
    func layoutPass() { }
}


// Helper to render a given compoennet in a UIView.
// By using this wrap, you can use componenets incrementally by migrating your UIView subclasses in your App.
class ComponentView<T:Component> :UIView {
    
    let component: T!
    
    let engine = WeactEngine()
    
    init(component: T) {
        self.component = component
        super.init(frame: CGRect.zero)
        engine.render(component:component, in: self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ComponentVC<T:Component>: UIViewController {
    
    let component: T!
    
    init(component: T) {
        self.component = component
        super.init(nibName: nil, bundle: nil)
    }
    
    var componentsView: ComponentView<T>!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(forName: NSNotification.Name("INJECTION_BUNDLE_NOTIFICATION"), object: nil, queue: nil) {_ in
            self.componentsView = ComponentView(component: self.component)
            self.view = self.componentsView
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func loadView() {
        componentsView = ComponentView(component: component)
        view = componentsView
    }
}
{'fraction_non_alphanumeric': 0.09907705993550539, 'fraction_numerical': 0.004003113532747692, 'mean_word_length': 1.8471035137701803, 'pattern_counts': {'":': 0, '<': 15, '<?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': '7083550', 'n_tokens_mistral': 2633, 'n_tokens_neox': 2572, 'n_words': 629}