<commit_before>//
// This file is part of Akane
//
// Created by JC on 06/12/15.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code
//

import Foundation
import Bond

public class ViewModelWrapper<T: Observation where T.Element: ComponentViewModel> {
    public typealias ObservationType = T
    public typealias ViewModelType = T.Element

    let viewModel: ObservationType
    let disposeBag: DisposeBag
    unowned let lifecycle: Lifecycle

    init(viewModel: ObservationType, lifecycle: Lifecycle, disposeBag: DisposeBag) {
        self.viewModel = viewModel
        self.lifecycle = lifecycle
        self.disposeBag = disposeBag
    }

    public func bindTo<T:UIView where T:ComponentView>(view: T?) {
        if let view = view {
            self.bindTo(view)
        }
    }

    public func bindTo<T:UIView where T:ComponentView>(view: T) {
        self.bind(view)
    }

    func bindTo(cell: UIView, template: Template) {
        template.bind(cell, wrapper: self)
    }

    func bind<T:UIView where T:ComponentView>(view: T) {
        let controller:ComponentViewController? = self.lifecycle.presenterForSubview(view, createIfNeeded: true)

        guard (controller != nil) else {
            return
        }

        self.disposeBag.addDisposable(
            self.viewModel.observe { viewModel in
                controller!.viewModel = viewModel
            }
        )
    }
}

extension ViewModelWrapper where T.Element: CollectionItemViewModel {

    public func bindTo<T:UITableView where
        T.ViewModelType == ViewModelType,
        T:ComponentTableView,
        T.DataSourceType.DataType == T.ViewModelType.CollectionDataType,
        T.DataSourceType.ItemIdentifier.RawValue == String>
        (tableView: T?) {
            if let tableView = tableView {
                self.bindTo(tableView)
            }
    }

    public func bindTo<T:UITableView where
        T.ViewModelType == ViewModelType,
        T:ComponentTableView,
        T.DataSourceType.DataType == T.ViewModelType.CollectionDataType,
        T.DataSourceType.ItemIdentifier.RawValue == String>
        (tableView: T) {

        // FIXME that makes 2 signals to disposebag
        self.bind(tableView)

        let controller:ComponentViewController? = self.lifecycle.presenterForSubview(tableView, createIfNeeded: false)

        self.disposeBag.addDisposable(
            self.viewModel.observe { [weak tableView] viewModel in
                if let tableView = tableView {
                    let delegate = TableViewDelegate.init(tableView: tableView, collectionViewModel: viewModel)

                    delegate.becomeDataSource(controller!.lifecycle.binder, data: viewModel.collection)
                }
            }
        )
    }
}<commit_msg>[binding] removed deprecated code<commit_after>//
// This file is part of Akane
//
// Created by JC on 06/12/15.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code
//

import Foundation
import Bond

public class ViewModelWrapper<T: Observation where T.Element: ComponentViewModel> {
    public typealias ObservationType = T
    public typealias ViewModelType = T.Element

    let viewModel: ObservationType
    let disposeBag: DisposeBag
    unowned let lifecycle: Lifecycle

    init(viewModel: ObservationType, lifecycle: Lifecycle, disposeBag: DisposeBag) {
        self.viewModel = viewModel
        self.lifecycle = lifecycle
        self.disposeBag = disposeBag
    }

    public func bindTo<T:UIView where T:ComponentView>(view: T?) {
        if let view = view {
            self.bindTo(view)
        }
    }

    public func bindTo<T:UIView where T:ComponentView>(view: T) {
        self.bind(view)
    }

    func bindTo(cell: UIView, template: Template) {
        template.bind(cell, wrapper: self)
    }

    func bind<T:UIView where T:ComponentView>(view: T) {
        let controller:ComponentViewController? = self.lifecycle.presenterForSubview(view, createIfNeeded: true)

        guard (controller != nil) else {
            return
        }

        self.disposeBag.addDisposable(
            self.viewModel.observe { viewModel in
                controller!.viewModel = viewModel
            }
        )
    }
}
{'fraction_non_alphanumeric': 0.0821601661666282, 'fraction_numerical': 0.003000230786983614, 'mean_word_length': 2.509311740890688, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '7547950', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1167, 'n_words': 309}