<commit_before>//
//  PullingViewController.swift
//  NKit
//
//  Created by <NAME> on 9/11/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import RxSwift

public protocol NKPullingViewControllerProtocol {
    associatedtype ViewModel: NKPullingViewModel
    
    var model: ViewModel {get}
    var collectionView: NKCollectionView {get}
    var loadingView: UIView {get}
    var errorView: UIView {get}
    
    func loadMore()
    func refresh()
}

public extension NKPullingViewControllerProtocol where Self: UIViewController {
    public func loadMore() {
        self.model.loadMoreObservable().subscribe().addDisposableTo(self.nk_disposeBag)
    }
    
    public func refresh() {
        var model = self.model
        model.refreshObservable().subscribe().addDisposableTo(self.nk_disposeBag)
    }
}

public protocol NKPullingViewModel: NKLoadable {
    var rx_items: Variable<[Any]> {get set}
    
    var page: Int {get set}
    
    func loadMoreObservable() -> Observable<Void>
    mutating func refreshObservable() -> Observable<Void><commit_msg>cherry pick 0.9.19<commit_after>//
//  PullingViewController.swift
//  NKit
//
//  Created by <NAME> on 9/11/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import RxSwift

public protocol NKPullingViewControllerProtocol {
    associatedtype ViewModel: NKPullingViewModel
    
    var model: ViewModel {get}
    var collectionView: NKCollectionView {get}
    var loadingView: UIView {get}
    var errorView: UIView {get}
    
    func loadMore()
    func refresh()
}

public extension NKPullingViewControllerProtocol where Self: UIViewController {
    public func loadMore() {
        self.model.loadMoreObservable().subscribe().addDisposableTo(self.nk_disposeBag)
    }
    
    public func refresh() {
        var model = self.model
        model.refreshObservable().subscribe().addDisposableTo(self.nk_disposeBag)
    }
}

public class NKPullingViewModelImp: NSObject, NKPullingViewModel {
    public let rx_items = Variable<[Any]>([])
    public let rx_error = Variable<Error?>(nil)
    public let rx_isLoading = Variable<Bool>(false)
    public var page: Int
    public let initPage: Int
    private let loadItemsBlock: ((Int) -> Observable<[Any]>)
  
    public init(initPage: Int, loadItemsBlock: @escaping (Int) -> Observable<[Any]>) {
        self.initPage = initPage
        self.page = self.initPage
        self.loadItemsBlock = loadItemsBlock
    }
    
    public func resetPage() {
        self.page = self.initPage
    }
    
    public func loadItems(page: Int) -> Observable<[Any]> {
        return self.loadItemsBlock(page)
        
    }
}

public protocol NKPullingViewModel: NKLoadable {
    var rx_items: Variable<[Any]> {get}
    
    var page: Int {get set}
    
    func loadMoreObservable() -> Observable<Void>
    mutating func refreshObservable() -> Observable<Void>
{'fraction_non_alphanumeric': 0.10103448275862069, 'fraction_numerical': 0.007586206896551724, 'mean_word_length': 3.168103448275862, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 26, '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': '131431', 'n_tokens_mistral': 927, 'n_tokens_neox': 853, 'n_words': 221}