<commit_before>//
//  AppViewModel.swift
//  UberGoCore
//
//  Created by <NAME> on 6/1/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import RxCocoa
import RxSwift

public enum PopoverState {
    case open
    case close
}

// MARK: - Protocol 
public protocol AppViewModelProtocol {

    var input: AppViewModelInput { get }
    var output: AppViewModelOutput { get }
}

public protocol AppViewModelInput {

    var switchPopoverPublish: PublishSubject<Void> { get }
    var actionPopoverPublish: PublishSubject<PopoverState> { get }

    // Debug
    var currentTripStatusPublish: PublishSubject<Void> { get }
    var cancelCurrentTripPublish: PublishSubject<Void> { get }
    var updateStatusTripPublish: PublishSubject<TripObjStatus> { get }
}

public protocol AppViewModelOutput {

    var popoverStateVariable: Variable<PopoverState> { get }
}

// MARK: - App ViewModel
open class AppViewModel: BaseViewModel, AppViewModelProtocol, AppViewModelInput, AppViewModelOutput {

    // MARK: - View model
    public var input: AppViewModelInput { return self }
    public var output: AppViewModelOutput { return self }

    // MARK: - Variable
    public var actionPopoverPublish = PublishSubject<PopoverState>()
    public var switchPopoverPublish = PublishSubject<Void>()
    public var popoverStateVariable = Variable<PopoverState>(.close)

    public var currentTripStatusPublish = PublishSubject<Void>()
    public var cancelCurrentTripPublish = PublishSubject<Void>()
    public var updateStatusTripPublish = PublishSubject<TripObjStatus>()
    fileprivate var sandboxStatus = TripObjStatus.unknown

    fileprivate let uberService = UberService()

    // MARK: - Init
    public override init() {
        super.init()
        // Switch
        switchPopoverPublish
            .map {[weak self] _ -> PopoverState in
            guard let `self` = self else { return .open }

            if self.popoverStateVariable.value == .open {
                return .close
            }
            return .open
            }
            .bind(to: popoverStateVariable)
            .addDisposableTo(disposeBag)

        actionPopoverPublish.bind(to: popoverStateVariable)
        .addDisposableTo(disposeBag)

        // Debug
        currentTripStatusPublish.asObserver()
        .flatMapLatest {[unowned self] _ -> Observable<TripObj> in
            return self.uberService.getCurrentTrip()
        }
        .subscribe(onNext: { (tripObj) in
            Logger.info("[DEBUG] CURRENT TRIP = \(tripObj)")
        })
        .addDisposableTo(disposeBag)

        // Cancel
        cancelCurrentTripPublish.asObserver()
        .flatMapLatest {[unowned self] (_) -> Observable<Void> in
            return self.uberService.cancelCurrentTrip()
        }
        .subscribe(onNext: { _ in
            Logger.info("[DEBUG] CANCEL TRIP OK")
        })
        .addDisposableTo(disposeBag)

        // Update status
        updateStatusTripPublish.asObserver()
        .do(onNext: {[unowned self] (status) in
            self.sandboxStatus = status
        })
        .flatMapLatest({[unowned self] _ -> Observable<TripObj> in
            return self.uberService.getCurrentTrip()
        })
        .flatMapLatest {[unowned self] (tripObj) -> Observable<Void> in
            guard let requestID = tripObj.requestId else {
                return Observable.empty()
            }
            return SandboxUberService().updateTripStateObserver(status: self.sandboxStatus, requestID: requestID)
        }
        .subscribe(onNext: {[unowned self] _ in
            Logger.info("[DEBUG] UPDATE TRIP = \(self.sandboxStatus.rawValue)")
        })
        .addDisposableTo(disposeBag)
    }
}<commit_msg>Refactor AppViewModel<commit_after>//
//  AppViewModel.swift
//  UberGoCore
//
//  Created by <NAME> on 6/1/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import RxCocoa
import RxSwift

public enum PopoverState {
    case open
    case close
}

// MARK: - Protocol 
public protocol AppViewModelProtocol {

    var input: AppViewModelInput { get }
    var output: AppViewModelOutput { get }
}

public protocol AppViewModelInput {

    var switchPopoverPublish: PublishSubject<Void> { get }
    var actionPopoverPublish: PublishSubject<PopoverState> { get }

    // Debug
    var currentTripStatusPublish: PublishSubject<Void> { get }
    var cancelCurrentTripPublish: PublishSubject<Void> { get }
    var updateStatusTripPublish: PublishSubject<TripObjStatus> { get }
}

public protocol AppViewModelOutput {

    var popoverStateVariable: Variable<PopoverState> { get }
}

// MARK: - App ViewModel
open class AppViewModel: AppViewModelProtocol,
                         AppViewModelInput,
                         AppViewModelOutput {

    // MARK: - View model
    public var input: AppViewModelInput { return self }
    public var output: AppViewModelOutput { return self }

    // MARK: - Variable
    public var actionPopoverPublish = PublishSubject<PopoverState>()
    public var switchPopoverPublish = PublishSubject<Void>()
    public var popoverStateVariable = Variable<PopoverState>(.close)

    public var currentTripStatusPublish = PublishSubject<Void>()
    public var cancelCurrentTripPublish = PublishSubject<Void>()
    public var updateStatusTripPublish = PublishSubject<TripObjStatus>()
    fileprivate var sandboxStatus = TripObjStatus.unknown

    fileprivate let uberService: UberService
    fileprivate let disposeBag = DisposeBag()

    // MARK: - Init
    public init(uberService: UberService = UberService()) {

        self.uberService = uberService

        // Switch
        switchPopoverPublish
            .withLatestFrom(popoverStateVariable.asObservable())
            .map({ return $0 == .open ? .close : .open })
            .bind(to: popoverStateVariable)
            .addDisposableTo(disposeBag)

        // Map
        actionPopoverPublish
            .bind(to: popoverStateVariable)
            .addDisposableTo(disposeBag)

        // Debug Current Trip
        currentTripStatusPublish
            .flatMapLatest { uberService.getCurrentTrip() }
            .subscribe(onNext: { (tripObj) in
                Logger.info("[DEBUG] CURRENT TRIP = \(tripObj)")
            })
            .addDisposableTo(disposeBag)

        // Debug Cancel
        cancelCurrentTripPublish
            .flatMapLatest { uberService.cancelCurrentTrip() }
            .subscribe(onNext: { _ in
                Logger.info("[DEBUG] CANCEL TRIP OK")
            })
            .addDisposableTo(disposeBag)

        // Debug Update status
        updateStatusTripPublish
            .do(onNext: {[unowned self] (status) in
                self.sandboxStatus = status
            })
            .flatMapLatest({ _ -> Observable<TripObj> in
                return uberService.getCurrentTrip()
            })
            .flatMapLatest {[unowned self] (tripObj) -> Observable<Void> in
                guard let requestID = tripObj.requestId else { return Observable.empty() }
                return SandboxUberService().updateTripStateObserver(status: self.sandboxStatus, requestID: requestID)
            }
            .subscribe(onNext: {[unowned self] _ in
                Logger.info("[DEBUG] UPDATE TRIP = \(self.sandboxStatus.rawValue)")
            })
            .addDisposableTo(disposeBag)
    }
}
{'fraction_non_alphanumeric': 0.09045226130653267, 'fraction_numerical': 0.002308841504821404, 'mean_word_length': 2.4817966903073287, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 44, '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': '7473559', 'n_tokens_mistral': 2127, 'n_tokens_neox': 2034, 'n_words': 469}