CallViewModel.swift 6.64 KB
Newer Older
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
1 2 3 4 5 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 32 33 34 35 36 37 38 39
/*
 *  Copyright (C) 2017 Savoir-faire Linux Inc.
 *
 *  Author: Silbino Gonçalves Matado <silbino.gmatado@savoirfairelinux.com>
 *  Author: Kateryna Kostiuk <kateryna.kostiuk@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 RxSwift
import SwiftyBeaver
import Contacts

class CallViewModel: Stateable, ViewModel {

    //stateable
    private let stateSubject = PublishSubject<State>()
    lazy var state: Observable<State> = {
        return self.stateSubject.asObservable()
    }()

    fileprivate let callService: CallsService
    fileprivate let contactsService: ContactsService
    fileprivate let accountService: AccountsService
    private let disposeBag = DisposeBag()
    fileprivate let log = SwiftyBeaver.self

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    var call: CallModel? {
        didSet {
            guard let call = self.call else {
                return
            }
            self.contactsService.getProfileForUri(uri: call.participantRingId)
                .subscribe(onNext: { [unowned self] profile in
                    self.profileUpdated(profile: profile)
                })
                .disposed(by: self.disposeBag)

            self.callService
                .sharedResponseStream
                .filter({ (event) in
                    if let uri: String = event.getEventInput(ServiceEventInput.uri) {
                        return event.eventType == ServiceEventType.profileUpdated
                            && uri == call.participantRingId
                    }
                    return false
                })
                .subscribe(onNext: { [unowned self] _ in
                    self.contactsService.getProfileForUri(uri: call.participantRingId)
                        .subscribe(onNext: { profile in
                            self.profileUpdated(profile: profile)
                        })
                        .disposed(by: self.disposeBag)
                })
                .disposed(by: disposeBag)
        }
    }
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
70 71 72

    // data for ViewCintroller binding

73 74
    var contactImageData = Variable<Data?>(nil)

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    lazy var dismisVC: Observable<Bool> = {
        return callService.currentCall.map({[weak self] call in
            return call.state == .over || call.state == .failure && call.callId == self?.call?.callId
        }).map({ hide in
            return hide
        })
    }()

    lazy var contactName: Observable<String> = {
        return callService.currentCall.filter({ [weak self] call in
            return call.state != .over && call.state != .inactive && call.callId == self?.call?.callId
        }).map({ call in
            if !call.displayName.isEmpty {
                return call.displayName
            } else if !call.registeredName.isEmpty {
                return call.registeredName
            } else {
                return L10n.Calls.unknown
            }
        })
    }()

    lazy var callDuration: Observable<String> = {
        let timer = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
            .takeUntil(self.callService.currentCall
                .filter { [weak self] call in
                    call.state == .over &&
                        call.callId == self?.call?.callId
            })
            .map({ elapsed in
                return CallViewModel.formattedDurationFrom(interval: elapsed)
            })
        return self.callService.currentCall.filter({ call in
            return call.state == .current
        }).flatMap({ _ in
            return timer
        })
    }()

    lazy var bottomInfo: Observable<String> = {
        return callService.currentCall.map({ [weak self] call in
            if call.state == .connecting || call.state == .ringing && call.callType == .outgoing && call.callId == self?.call?.callId {
                return L10n.Calls.calling
            } else if call.state == .over {
                return L10n.Calls.callFinished
            } else {
                return ""
            }
        })
    }()
125

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    required init(with injectionBag: InjectionBag) {
        self.callService = injectionBag.callService
        self.contactsService = injectionBag.contactsService
        self.accountService = injectionBag.accountService
    }
    static func formattedDurationFrom(interval: Int) -> String {
        let seconds = interval % 60
        let minutes = (interval / 60) % 60
        let hours = (interval / 3600)
        return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
    }

    func cancelCall() {
        guard let call = self.call else {
            return
        }
        self.callService.hangUp(callId: call.callId)
            .subscribe(onCompleted: { [weak self] in
                self?.log.info("Call canceled")
                }, onError: { [weak self] error in
                    self?.log.error("Failed to cancel the call")
            }).disposed(by: self.disposeBag)
    }



    func answerCall() {
        guard let call = self.call else {
            return
        }
        self.callService.accept(callId: call.callId)
            .subscribe(onCompleted: { [weak self] in
                self?.log.info("Call answered")
                }, onError: { [weak self] error in
                    self?.log.error("Failed to answer the call")
            }).disposed(by: self.disposeBag)
    }

    func placeCall(with uri: String, userName: String) {
165

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
166 167 168 169 170 171 172 173 174 175
        guard let account = self.accountService.currentAccount else {
            return
        }
        self.callService.placeCall(withAccount: account,
                                   toRingId: uri,
                                   userName: userName)
            .subscribe(onSuccess: { [unowned self] callModel in
                self.call = callModel
            }).disposed(by: self.disposeBag)
    }
176 177 178 179 180 181 182 183 184 185

    func profileUpdated(profile: Profile) {
        guard let photo = profile.photo else {
            return
        }
        guard let data = NSData(base64Encoded: photo, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters) as Data? else {
            return
        }
        self.contactImageData.value = data
    }
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
186
}