<commit_before>//
//  ScrollView.swift
//  Few
//
//  Created by <NAME> on 14-03-15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit

private class FewScrollView: UIScrollView, UIScrollViewDelegate {
    private var didScroll: CGRect -> ()
    
    private init(frame: CGRect, didScroll: CGRect -> ()) {
        self.didScroll = didScroll
        
        super.init(frame: frame)
        
        delegate = self
    }
    
    required init(coder: NSCoder) {
        fatalError("FewScrollView should not be used in a xib.")
    }
    
    private func scrollViewDidScroll(scrollView: UIScrollView) {
        didScroll(CGRect(origin: contentOffset, size: frame.size))
    }
}

private class ScrollViewElement: Element {
    private let didScroll: CGRect -> ()
    
    private init(_ didScroll: CGRect -> ()) {
        self.didScroll = didScroll
    }
    
    // MARK: Element
    
    private override func createView() -> ViewType {
        let scrollView = FewScrollView(frame: frame, didScroll: didScroll)
        scrollView.alpha = alpha
        scrollView.hidden = hidden
        return scrollView
    }
    
    private override func addRealizedChildView(childView: ViewType, selfView: ViewType) {
        let scrollVew = selfView as FewScrollView
        scrollVew.addSubview(childView)
    }
    
    private override func realize() -> RealizedElement {
        let realizedElement = super.realize()
        
        let scrollView = realizedElement.view as FewScrollView
        let documentView = scrollView.subviews.first! as UIView
        
        let top = CGPointMake(0, documentView.bounds.size.height);
        scrollView.contentOffset = top
        
        return realizedElement
    }
    
    private override func applyDiff(old: Element, realizedSelf: RealizedElement?) {
        super.applyDiff(old, realizedSelf: realizedSelf)
        
        let scrollView = realizedSelf?.view as FewScrollView
        scrollView.didScroll = didScroll
    }
}

private class ScrollViewContent: Element {
    private let layoutChildren: [Element]
    
    private init(layoutChildren: [Element]) {
        self.layoutChildren = layoutChildren
    }
    
    private override func assembleLayoutNode() -> Node {
        let childNodes = layoutChildren.map { $0.assembleLayoutNode() }
        return Node(size: frame.size, children: childNodes, direction: direction, margin: margin, padding: padding, wrap: wrap, justification: justification, selfAlignment: selfAlignment, childAlignment: childAlignment, flex: flex)
    }
    
    private override func applyLayout(layout: Layout) {
        frame = layout.frame
        
        for (child, layout) in Zip2(layoutChildren, layout.children) {
            child.applyLayout(layout)
        }
    }
}

public typealias ScrollView = ScrollView_<CGRect>

public class ScrollView_<LOL>: Component<CGRect> {
    public var elements: [Element]
    
    public init(_ elements: [Element]) {
        self.elements = elements.reverse()
        super.init(initialState: CGRectZero, render: ScrollView_.render)<commit_msg>Make the ScrollView actually work<commit_after>//
//  ScrollView.swift
//  Few
//
//  Created by <NAME> on 14-03-15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit

private class FewScrollView: UIScrollView, UIScrollViewDelegate {
    private var didScroll: CGRect -> ()
    
    private init(frame: CGRect, didScroll: CGRect -> ()) {
        self.didScroll = didScroll
        
        super.init(frame: frame)
        
        delegate = self
    }
    
    required init(coder: NSCoder) {
        fatalError("FewScrollView should not be used in a xib.")
    }
    
    private func scrollViewDidScroll(scrollView: UIScrollView) {
        let visibleRect = CGRect(x: contentOffset.x, y: contentOffset.y, width: contentOffset.x + bounds.size.width, height: contentOffset.y + bounds.size.height)
        didScroll(visibleRect)
    }
}

private class ScrollViewElement: Element {
    private let didScroll: CGRect -> ()
    
    private init(_ didScroll: CGRect -> ()) {
        self.didScroll = didScroll
    }
    
    // MARK: Element
    
    private override func createView() -> ViewType {
        return FewScrollView(frame: frame, didScroll: didScroll)
    }
    
    private override func addRealizedChildView(childView: ViewType, selfView: ViewType) {
        let scrollView = selfView as FewScrollView
        scrollView.subviews.first?.removeFromSuperview()
        scrollView.addSubview(childView)
    }
    
    private override func realize() -> RealizedElement {
        let realizedElement = super.realize()
        
        let scrollView = realizedElement.view as FewScrollView
        if let element = children.first {
            scrollView.contentSize = element.frame.size
        }
        
        return realizedElement
    }
    
    private override func applyDiff(old: Element, realizedSelf: RealizedElement?) {
        super.applyDiff(old, realizedSelf: realizedSelf)
        
        let scrollView = realizedSelf?.view as FewScrollView
        scrollView.didScroll = didScroll
    }
}

private class ScrollViewContent: Element {
    private let layoutChildren: [Element]
    
    private init(layoutChildren: [Element]) {
        self.layoutChildren = layoutChildren
    }
    
    private override func assembleLayoutNode() -> Node {
        let childNodes = layoutChildren.map { $0.assembleLayoutNode() }
        return Node(size: frame.size, children: childNodes, direction: direction, margin: margin, padding: padding, wrap: wrap, justification: justification, selfAlignment: selfAlignment, childAlignment: childAlignment, flex: flex)
    }
    
    private override func applyLayout(layout: Layout) {
        frame = layout.frame
        
        for (child, layout) in Zip2(layoutChildren, layout.children) {
            child.applyLayout(layout)
        }
    }
}

public typealias ScrollView = ScrollView_<CGRect>

public class ScrollView_<LOL>: Component<CGRect> {
    public var elements: [Element]
    
    public init(_ elements: [Element]) {
        self.elements = elements
        super.init(initialState: CGRectZero, render: ScrollView_.render)
{'fraction_non_alphanumeric': 0.08866677414154442, 'fraction_numerical': 0.004030307915524746, 'mean_word_length': 2.6710059171597633, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, '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': '4298213', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1686, 'n_words': 449}