CallViewController.swift 6.8 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
/*
 *  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 UIKit
import Chameleon
import RxSwift
import Reusable
26
import SwiftyBeaver
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
27 28 29

class CallViewController: UIViewController, StoryboardBased, ViewModelBased {

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    //preview screen
    @IBOutlet private weak var profileImageView: UIImageView!
    @IBOutlet private weak var nameLabel: UILabel!
    @IBOutlet private weak var durationLabel: UILabel!
    @IBOutlet private weak var infoBottomLabel: UILabel!

    @IBOutlet private weak var cancelButton: UIButton!
    @IBOutlet private weak var mainView: UIView!

    //video screen
    @IBOutlet private weak var callView: UIView!
    @IBOutlet private weak var incomingVideo: UIImageView!
    @IBOutlet private weak var capturedVideo: UIImageView!
    @IBOutlet private weak var infoContainer: UIView!
    @IBOutlet private weak var callProfileImage: UIImageView!
    @IBOutlet private weak var callNameLabel: UILabel!
    @IBOutlet private weak var callInfoTimerLabel: UILabel!
    @IBOutlet private weak var infoLabelConstraint: NSLayoutConstraint!

    // call options buttons
    @IBOutlet private weak var buttonsContainer: UIView!
    @IBOutlet private weak var muteAudioButton: UIButton!
    @IBOutlet private weak var muteVideoButton: UIButton!
    @IBOutlet private weak var pauseCallButton: UIButton!
    @IBOutlet private weak var switchCameraButton: UIButton!
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
55 56 57 58 59

    var viewModel: CallViewModel!

    fileprivate let disposeBag = DisposeBag()

60 61
    private let log = SwiftyBeaver.self

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
62 63
    private var task: DispatchWorkItem?

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
64 65
    override func viewDidLoad() {
        super.viewDidLoad()
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
66 67
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(screenTaped))
        self.mainView.addGestureRecognizer(tapGestureRecognizer)
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
68 69 70 71 72 73
        self.setupUI()
        self.setupBindings()
    }

    func setupUI() {
        self.cancelButton.backgroundColor = UIColor.red
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
74 75
        self.infoContainer.backgroundColor = UIColor.black.withAlphaComponent(0.3)
        self.buttonsContainer.backgroundColor = UIColor.black.withAlphaComponent(0.3)
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
76 77 78 79 80 81 82 83 84 85 86
    }

    func setupBindings() {
        //Cancel button action
        self.cancelButton.rx.tap
            .subscribe(onNext: { [weak self] in
            self?.removeFromScreen()
            self?.viewModel.cancelCall()
        }).disposed(by: self.disposeBag)

        //Data bindings
87 88 89 90 91 92
        self.viewModel.contactImageData.asObservable()
            .observeOn(MainScheduler.instance)
            .subscribe(onNext: { [weak self] dataOrNil in
            if let imageData = dataOrNil {
                if let image = UIImage(data: imageData) {
                    self?.profileImageView.image = image
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
93
                    self?.callProfileImage.image = image
94 95 96 97
                }
            }
        }).disposed(by: self.disposeBag)

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
98 99 100 101 102 103 104 105
        self.viewModel.dismisVC
            .observeOn(MainScheduler.instance)
            .subscribe(onNext: { [weak self] dismiss in
            if dismiss {
                self?.removeFromScreen()
            }
        }).disposed(by: self.disposeBag)

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
106
        self.viewModel.contactName.drive(self.nameLabel.rx.text)
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
107 108
            .disposed(by: self.disposeBag)

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
109 110 111 112 113 114 115
        self.viewModel.contactName.drive(self.callNameLabel.rx.text)
            .disposed(by: self.disposeBag)

        self.viewModel.callDuration.drive(self.durationLabel.rx.text)
            .disposed(by: self.disposeBag)

        self.viewModel.callDuration.drive(self.callInfoTimerLabel.rx.text)
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
116 117 118 119 120 121
            .disposed(by: self.disposeBag)

        self.viewModel.bottomInfo
            .observeOn(MainScheduler.instance)
            .bind(to: self.infoBottomLabel.rx.text)
            .disposed(by: self.disposeBag)
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
122 123 124 125 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

        self.viewModel.incomingFrame
            .observeOn(MainScheduler.instance)
            .subscribe(onNext: { [weak self] frame in
            if let image = frame {
                DispatchQueue.main.async {
                    self?.callView.isHidden = false
                    self?.incomingVideo.image = image
                }
            }
        }).disposed(by: self.disposeBag)

        self.viewModel.capturedFrame
            .observeOn(MainScheduler.instance)
            .subscribe(onNext: { [weak self] frame in
            if let image = frame {
                DispatchQueue.main.async {
                    self?.capturedVideo.image = image
                }
            }
        }).disposed(by: self.disposeBag)

        self.viewModel.showCallOptions
            .subscribeOn(MainScheduler.instance)
            .subscribe(onNext: { show in
                if show {
                    self.showContactInfo()
                }
            }).disposed(by: self.disposeBag)
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
151 152 153 154 155
    }

    func removeFromScreen() {
        self.dismiss(animated: false)
    }
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

    @objc func screenTaped() {
        self.viewModel.respondOnTap()
    }

    func showContactInfo() {
        if !self.infoContainer.isHidden {
            task?.cancel()
            self.hideContactInfo()
            return
        }

        self.infoLabelConstraint.constant = -200.00
        self.buttonsContainer.isHidden = false
        self.infoContainer.isHidden = false
        self.view.layoutIfNeeded()

        UIView.animate(withDuration: 0.2, delay: 0.0,
                       options: .curveEaseOut,
                       animations: { [weak self] in
            self?.infoLabelConstraint.constant = 0.00
            self?.view.layoutIfNeeded()
        }, completion: nil)

        task = DispatchWorkItem { self.hideContactInfo() }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2, execute: task!)
    }

    func hideContactInfo() {
        UIView.animate(withDuration: 0.2, delay: 0.00,
                       options: .curveEaseOut,
                       animations: { [weak self] in
            self?.infoLabelConstraint.constant = -200.00
            self?.view.layoutIfNeeded()
        }, completion: { [weak self] _ in
            self?.infoContainer.isHidden = true
            self?.buttonsContainer.isHidden = true
        })
    }
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
195
}