ConversationNavigation.swift 6.02 KB
Newer Older
1
/*
2
 *  Copyright (C) 2018-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
 *
 *  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

24
enum ConversationState: State {
25 26
    case startCall(contactRingId: String, userName: String)
    case startAudioCall(contactRingId: String, userName: String)
27 28
    case conversationDetail(conversationViewModel: ConversationViewModel)
    case contactDetail(conversationViewModel: ConversationModel)
29 30
    case qrCode
    case createNewAccount
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
31
    case showDialpad(inCall: Bool)
32
    case showGeneralSettings
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
33
    case navigateToCall(call: CallModel)
34 35
}

36
protocol ConversationNavigation: class {
37 38

    var injectionBag: InjectionBag { get }
39
    func addLockFlags()
40 41
}

42
extension ConversationNavigation where Self: Coordinator, Self: StateableResponsive {
43

44
    func callbackPlaceCall() {
45
        self.stateSubject.subscribe(onNext: { [unowned self] (state) in
46
            guard let state = state as? ConversationState else { return }
47 48 49 50 51
            switch state {
            case .startCall(let contactRingId, let name):
                self.startOutgoingCall(contactRingId: contactRingId, userName: name)
            case .startAudioCall(let contactRingId, let name):
                self.startOutgoingCall(contactRingId: contactRingId, userName: name, isAudioOnly: true)
52 53 54 55
            case .conversationDetail (let conversationViewModel):
                self.showConversation(withConversationViewModel: conversationViewModel)
            case .contactDetail(let conversationModel):
                self.presentContactInfo(conversation: conversationModel)
56
            case .qrCode:
Quentin Muret's avatar
Quentin Muret committed
57
                self.openQRCode()
58 59
            default:
                break
60 61 62 63
            }
        }).disposed(by: self.disposeBag)
    }

Quentin Muret's avatar
Quentin Muret committed
64
    func openQRCode () {
65
        let scanViewController = ScanViewController.instantiate(with: self.injectionBag)
Quentin Muret's avatar
Quentin Muret committed
66 67 68
        self.present(viewController: scanViewController,
                     withStyle: .present,
                     withAnimation: true,
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
69
                     withStateable: scanViewController.viewModel)
Quentin Muret's avatar
Quentin Muret committed
70 71
    }

72
    func presentContactInfo(conversation: ConversationModel) {
73 74 75 76
        if let flag = self.presentingVC[VCType.contact.rawValue], flag {
            return
        }
        self.presentingVC[VCType.contact.rawValue] = true
77 78
        let contactViewController = ContactViewController.instantiate(with: self.injectionBag)
        contactViewController.viewModel.conversation = conversation
79 80 81 82 83
        self.present(viewController: contactViewController,
                     withStyle: .show,
                     withAnimation: true,
                     withStateable: contactViewController.viewModel,
                     lockWhilePresenting: VCType.contact.rawValue)
84 85 86
    }

    private func showConversation (withConversationViewModel conversationViewModel: ConversationViewModel) {
87 88 89 90
        if let flag = self.presentingVC[VCType.conversation.rawValue], flag {
            return
        }
        self.presentingVC[VCType.conversation.rawValue] = true
91 92
        let conversationViewController = ConversationViewController.instantiate(with: self.injectionBag)
        conversationViewController.viewModel = conversationViewModel
93 94 95 96 97
        self.present(viewController: conversationViewController,
                     withStyle: .show,
                     withAnimation: true,
                     withStateable: conversationViewController.viewModel,
                     lockWhilePresenting: VCType.conversation.rawValue)
98 99
    }

100 101 102 103 104 105 106 107 108 109 110 111 112 113
    func pushConversation(withConversationViewModel conversationViewModel: ConversationViewModel) {
        if let flag = self.presentingVC[VCType.conversation.rawValue], flag {
            return
        }
        self.presentingVC[VCType.conversation.rawValue] = true
        let conversationViewController = ConversationViewController.instantiate(with: self.injectionBag)
        conversationViewController.viewModel = conversationViewModel
        self.present(viewController: conversationViewController,
                     withStyle: .push,
                     withAnimation: false,
                     withStateable: conversationViewController.viewModel,
                     lockWhilePresenting: VCType.conversation.rawValue)
    }

114 115 116 117 118 119 120 121 122 123 124
    func getTopController() -> UIViewController? {
        guard var topController = UIApplication.shared
            .keyWindow?.rootViewController else {
                return nil
        }
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        return topController
    }

125
    func startOutgoingCall(contactRingId: String, userName: String, isAudioOnly: Bool = false) {
126 127 128 129
        guard let topController = getTopController(),
            !topController.isKind(of: (CallViewController).self) else {
                return
        }
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
130 131 132 133 134 135 136 137
        DispatchQueue.main.async {
            let callViewController = CallViewController.instantiate(with: self.injectionBag)
            callViewController.viewModel.placeCall(with: contactRingId, userName: userName, isAudioOnly: isAudioOnly)
            self.present(viewController: callViewController,
                         withStyle: .present,
                         withAnimation: false,
                         withStateable: callViewController.viewModel)
        }
138 139
    }
}