LinkDeviceViewModel.swift 3.15 KB
Newer Older
Thibault Wittemberg's avatar
Thibault Wittemberg committed
1
/*
2
 *  Copyright (C) 2017 Savoir-faire Linux Inc.
Thibault Wittemberg's avatar
Thibault Wittemberg committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 *  Author: Thibault Wittemberg <thibault.wittemberg@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

import Foundation
22 23 24 25 26 27 28 29 30
import RxSwift

class LinkDeviceViewModel: Stateable, ViewModel {

    // MARK: - Rx Stateable
    private let stateSubject = PublishSubject<State>()
    lazy var state: Observable<State> = {
        return self.stateSubject.asObservable()
    }()
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
31 32 33 34 35 36 37
    private let accountService: AccountsService
    private let accountCreationState = Variable<AccountCreationState>(.unknown)
    lazy var createState: Observable<AccountCreationState> = {
        return self.accountCreationState.asObservable()
    }()

    lazy var linkButtonEnabledState: Observable<Bool>  = {
38 39 40
        return self.pin.asObservable().map({ pin in
            return !pin.isEmpty
        })
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
41 42 43 44 45
    }()

    let pin = Variable<String>("")
    let password = Variable<String>("")
    let disposeBag = DisposeBag()
46 47

    required init (with injectionBag: InjectionBag) {
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        self.accountService = injectionBag.accountService

        //Account creation state observer
        self.accountService
            .sharedResponseStream
            .subscribe(onNext: { [unowned self] event in
                if event.getEventInput(ServiceEventInput.registrationState) == Registered {
                    self.accountCreationState.value = .success
                    Observable<Int>.timer(Durations.alertFlashDuration.value, period: nil, scheduler: MainScheduler.instance).subscribe(onNext: { [unowned self] (_) in
                        self.stateSubject.onNext(WalkthroughState.deviceLinked)
                    }).disposed(by: self.disposeBag)
                } else if event.getEventInput(ServiceEventInput.registrationState) == ErrorGeneric {
                    self.accountCreationState.value = .error(error: AccountCreationError.linkError)
                } else if event.getEventInput(ServiceEventInput.registrationState) == ErrorNetwork {
                    self.accountCreationState.value = .error(error: AccountCreationError.network)
                }
            }, onError: { [unowned self] _ in
                self.accountCreationState.value = .error(error: AccountCreationError.unknown)
            }).disposed(by: disposeBag)
67 68 69
    }

    func linkDevice () {
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
70 71 72
        self.accountCreationState.value = .started
        self.accountService.linkToRingAccount(withPin: self.pin.value,
                                              password: self.password.value)
73
    }
Thibault Wittemberg's avatar
Thibault Wittemberg committed
74
}
75