ButtonsContainerView.swift 6.15 KB
Newer Older
1
/*
2
 *  Copyright (C) 2017-2019 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
 *
 *  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 Reusable
import RxSwift

class ButtonsContainerView: UIView, NibLoadable {

27
    //Outlets
28 29
    @IBOutlet var containerView: UIView!
    @IBOutlet  weak var container: UIView!
30 31
    @IBOutlet weak var stackView: UIStackView!
    @IBOutlet weak var backgroundBlurEffect: UIVisualEffectView!
32 33 34
    @IBOutlet  weak var muteAudioButton: UIButton!
    @IBOutlet  weak var muteVideoButton: UIButton!
    @IBOutlet  weak var pauseCallButton: UIButton!
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
35
    @IBOutlet  weak var dialpadButton: UIButton!
36 37
    @IBOutlet  weak var switchSpeakerButton: UIButton!
    @IBOutlet  weak var cancelButton: UIButton!
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
38
    @IBOutlet  weak var switchCameraButton: UIButton!
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
39
    @IBOutlet  weak var acceptCallButton: UIButton!
40 41 42

    //Constraints
    @IBOutlet weak var cancelButtonWidthConstraint: NSLayoutConstraint!
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
43
    @IBOutlet weak var cancelButtonCenterConstraint: NSLayoutConstraint!
44
    @IBOutlet weak var cancelButtonBottomConstraint: NSLayoutConstraint!
45
    @IBOutlet weak var cancelButtonHeightConstraint: NSLayoutConstraint!
46
    @IBOutlet weak var stackViewYConstraint: NSLayoutConstraint!
47
    @IBOutlet weak var stackViewWidthConstraint: NSLayoutConstraint!
48
    @IBOutlet weak var containerHeightConstraint: NSLayoutConstraint!
49

50
    let disposeBag = DisposeBag()
51
    var isCallStarted: Bool = false
52 53 54 55

    var viewModel: ButtonsContainerViewModel? {
        didSet {
            self.viewModel?.observableCallOptions
56
                .observeOn(MainScheduler.instance)
57
                .subscribe(onNext: { [weak self] callOptions in
58 59
                    switch callOptions {
                    case .none:
60
                        self?.withoutOptions()
61
                    case .optionsWithoutSpeakerphone:
62
                        self?.optionsWithoutSpeaker()
63
                    case .optionsWithSpeakerphone:
64
                        self?.optionsWithSpeaker()
65 66 67 68 69 70 71 72 73
                    }
                }).disposed(by: self.disposeBag)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.commonInit()
    }

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
74 75
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
76 77 78 79 80 81 82 83 84 85 86 87
        self.commonInit()
    }

    override open func didMoveToWindow() {
        super.didMoveToWindow()
        self.cancelButton.backgroundColor = UIColor.red
    }

    func commonInit() {
        Bundle.main.loadNibNamed("ButtonsContainerView", owner: self, options: nil)
        addSubview(containerView)
        containerView.frame = self.bounds
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
88
        self.container.clipsToBounds = false
89 90 91
    }

    func withoutOptions() {
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
92 93 94 95 96 97 98 99 100 101 102 103
        self.container.backgroundColor = UIColor.clear
        self.backgroundBlurEffect.isHidden = true
        switchCameraButton.isHidden = true
        muteAudioButton.isHidden = true
        muteVideoButton.isHidden = true
        pauseCallButton.isHidden = true
        dialpadButton.isHidden = true
        switchSpeakerButton.isHidden = true
        cancelButton.isHidden = false
        if self.viewModel?.isIncoming ?? false {
            acceptCallButton.isHidden = false
            cancelButtonBottomConstraint.constant = 60
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
104
            cancelButtonCenterConstraint.constant = -80
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
105 106 107 108
            return
        }
        cancelButtonCenterConstraint.constant = 0
        cancelButtonBottomConstraint.constant = 20
109 110 111
    }

    func optionsWithSpeaker() {
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
112 113
        acceptCallButton.isHidden = true
        cancelButtonCenterConstraint.constant = 0
114 115 116 117 118 119
        if !self.isCallStarted {
            self.isCallStarted = true
            self.backgroundBlurEffect.isHidden = false
            muteAudioButton.isHidden = false
            if self.viewModel?.isAudioOnly ?? false {
                muteVideoButton.isHidden = true
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
120
                switchCameraButton.isHidden = true
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
121 122 123
                if self.viewModel?.isSipCall ?? false {
                    dialpadButton.isHidden = false
                }
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
124
                cancelButtonBottomConstraint.constant = 20
125 126
            } else {
                muteVideoButton.isHidden = false
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
127 128
                switchCameraButton.isHidden = false
                cancelButtonBottomConstraint.constant = 120
129 130 131
            }
            pauseCallButton.isHidden = false
            switchSpeakerButton.isEnabled = true
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
132
            switchSpeakerButton.isHidden = false
133
            cancelButton.isHidden = false
134
        }
135 136 137
    }

    func optionsWithoutSpeaker() {
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
138 139
        acceptCallButton.isHidden = true
        cancelButtonCenterConstraint.constant = 0
140 141 142 143
        if !self.isCallStarted {
            self.isCallStarted = true
            if self.viewModel?.isAudioOnly ?? false {
                muteVideoButton.isHidden = true
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
144
                switchCameraButton.isHidden = true
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
145 146 147
                if self.viewModel?.isSipCall ?? false {
                    dialpadButton.isHidden = false
                }
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
148
                cancelButtonBottomConstraint.constant = 20
149
            } else {
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
150
                switchCameraButton.isHidden = false
151
                muteVideoButton.isHidden = false
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
152
                cancelButtonBottomConstraint.constant = 120
153
            }
154
            switchSpeakerButton.isEnabled = false
155
            self.muteAudioButton.isHidden = false
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
156
            switchSpeakerButton.isHidden = false
157 158 159
            self.backgroundBlurEffect.isHidden = false
            pauseCallButton.isHidden = false
            cancelButton.isHidden = false
160
        }
161 162
    }
}