ButtonsContainerViewModel.swift 2.96 KB
Newer Older
1
/*
Quentin Muret's avatar
Quentin Muret committed
2
 *  Copyright (C) 2017-2018 Savoir-faire Linux Inc.
3 4
 *
 *  Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
5
 *  Author: Quentin Muret <quentin.muret@savoirfairelinux.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 *  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 RxSwift

enum CallOptions {
    case none
    case optionsWithoutSpeakerphone
    case optionsWithSpeakerphone
}

class ButtonsContainerViewModel {

    let callService: CallsService
32
    let audioService: AudioService
33 34
    let callID: String
    let disposeBag = DisposeBag()
35
    var isAudioOnly: Bool
36 37

    let avalaibleCallOptions = BehaviorSubject<CallOptions>(value: .none)
38
    lazy var observableCallOptions: Observable<CallOptions> = { [unowned self] in
39 40 41
        return self.avalaibleCallOptions.asObservable()
    }()

42
    init(isAudioOnly: Bool, with callService: CallsService, audioService: AudioService, callID: String) {
43
        self.callService = callService
44
        self.audioService = audioService
45
        self.callID = callID
46
        self.isAudioOnly = isAudioOnly
47 48 49 50 51
        checkCallOptions()
    }

    private func checkCallOptions() {
        let callIsActive: Observable<Bool> = {
52 53
            self.callService.currentCall.filter({ [weak self] call in
                return call.state == .current && call.callId == self?.callID
54 55 56 57
            }).map({_ in
                return true
            })
        }()
58
        callIsActive
59
            .subscribe(onNext: { [weak self] active in
60 61 62 63
            if !active {
                return
            }
            if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
64
                self?.avalaibleCallOptions.onNext(.optionsWithoutSpeakerphone)
65 66
                return
            }
67
            self?.connectToSpeaker()
68 69 70 71
        }).disposed(by: self.disposeBag)
    }

    private func connectToSpeaker() {
72
        let speakerIsAvailable: Observable<Bool> = { [unowned self] in
73 74 75
            return self.audioService.enableSwitchAudio.map({ (hide)  in
                !hide
            })
76
        }()
77
        speakerIsAvailable.subscribe(onNext: { [weak self] available in
78
            if available {
79
                self?.avalaibleCallOptions.onNext(.optionsWithSpeakerphone)
80 81 82
                return
            }

83
            self?.avalaibleCallOptions.onNext(.optionsWithoutSpeakerphone)
84 85 86
        }).disposed(by: self.disposeBag)
    }
}