123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926 |
- //
- // VideoViewControllerQmera.swift
- // Qmera
- //
- // Created by Akhmad Al Qindi Irsyam on 07/10/21.
- //
- // Rn
- // (Kedip whiteboard)
- // extension Whiteboard
- // wbVC!.close wbTimer wbBlink
- import UIKit
- import nuSDKService
- import AVFoundation
- import NotificationBannerSwift
- import MediaPlayer
- class QmeraVideoViewController: UIViewController {
-
- static private var nMaxSPOn: Float! = 20.0
- static private var nMaxSPOff: Float! = 20.0
- static private var volumeView: MPVolumeView!
- static private var lastVolume: Float! = AVAudioSession.sharedInstance().outputVolume
- static private var bSpeakerPhone: Bool! = false
- private var tempSpeaker = false
- private var timerSpeaker: Timer?
- static private var isLoop = false
-
-
- var dataPerson: [[String: String?]] = []
- var fPin = ""
- var wbRoomId = ""
- var isInisiator = true
- // var isSpeaker = false
- var isMuted = false
- var isPresent = false
- var callFCM = true
- var isNavigationHidden = false
- var listRemoteViewFix: [UIImageView] = [
- UIImageView(),
- UIImageView(),
- UIImageView(),
- UIImageView(),
- UIImageView()
- ]
- var containerLabelName: [UIView] = [
- UIView(),
- UIView(),
- UIView(),
- UIView(),
- UIView()
- ]
- var imageMuted: [UIImageView] = [
- UIImageView(),
- UIImageView(),
- UIImageView(),
- UIImageView(),
- UIImageView()
- ]
- var mutedZoom: UIImageView = {
- let image = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 40))
- image.contentMode = .scaleAspectFit
- image.image = UIImage(systemName: "mic.slash")
- image.tintColor = .red
- image.isHidden = true
- return image
- }()
- let myImage = UIImageView()
- let name = UILabel()
- let profileImage = UIImageView()
- let labelIncomingOutgoing = UILabel()
- let buttonDecline = UIButton()
- let buttonAccept = UIButton()
- let zoomView = UIImageView()
- let cameraView = UIImageView()
- var constraintLeadingButtonDecline = NSLayoutConstraint()
- var constraintBottomButtonDecline = NSLayoutConstraint()
- var constraintBottomStackViewToolbar = NSLayoutConstraint()
- var constraintLeftStackViewToolbar2 = NSLayoutConstraint()
- let stackViewToolbar = UIStackView()
- let stackViewToolbar2 = UIStackView()
- let stackViewToolbar3 = UIStackView()
- var onScreenConstraintWB = [NSLayoutConstraint]()
- let buttonWB = UIButton()
- let buttonChat = UIButton()
- var wbVC : WhiteboardViewController?
- let buttonAddParticipant = UIButton()
- let buttonSpeaker = UIButton()
- let buttonRotate = UIButton()
- let buttonMuted = UIButton()
- var showStackViewToolbar = true
- let scrollRemoteView = UIScrollView()
- var isAutoAccept = false
- var wbTimer = Timer()
- var wbBlink = false
- var showNotifCCEnd = false
- var transformZoomAfterNewUserMore2 = false
- var isAddCall = ""
- var ticketId = ""
- var autoAcceptAPN = false
- var timeStartCall = ""
- var idCall = ""
- private var frontCamera = true
- var users: [User] = []
- let poweredByView: UIStackView = {
- let stackView = UIStackView()
- stackView.axis = .horizontal
- stackView.spacing = 5
- return stackView
- }()
- private var vcTimer = Timer()
- private var containerTimerVC = UIView()
- private var labelTimerVC = UILabel()
-
- let poweredByLabel: UILabel = {
- let label = UILabel()
- label.text = "Powered by Nexilis".localized()
- return label
- }()
-
- let qmeraLogo: UIButton = {
- let image = UIImage(named: "Q-Button-PNG", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
- let button = UIButton()
- button.setImage(image, for: .normal)
- button.imageView?.contentMode = .scaleAspectFit
- button.imageEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
- button.contentVerticalAlignment = .fill
- button.contentHorizontalAlignment = .fill
- // button.frame.size.width = 30
- // button.frame.size.height = 30
- return button
- }()
-
- let nexilisLogo: UIButton = {
- let image = UIImage(named: "pb_powered_button", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
- let button = UIButton()
- button.setImage(image, for: .normal)
- button.imageView?.contentMode = .scaleAspectFit
- button.imageEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
- button.contentVerticalAlignment = .fill
- button.contentHorizontalAlignment = .fill
- // button.frame.size.width = 30
- // button.frame.size.height = 30
- return button
- }()
-
- static func turnSpeakerOn() {
- do {
- let audioSession = AVAudioSession.sharedInstance()
- try audioSession.overrideOutputAudioPort(bSpeakerPhone ? .speaker : .none)
- } catch {
-
- }
- // var bAudioEngineIsAvtive: Bool! = false
- // API.turnSpeakerPhone(bSPon: bSpeakerPhone)
- // repeat {
- // Thread.sleep(forTimeInterval : 0.3)
- // bAudioEngineIsAvtive = API.bAudioEngineIsRunning()
- // print("Audio Session State: \(bAudioEngineIsAvtive ? "Active" : "Inactive" )")
- // if (bAudioEngineIsAvtive) {
- // break
- // }
- // API.restartAudioEngine()
- // } while (!bAudioEngineIsAvtive)
- // var volume:Float! = 0
- // if (bSpeakerPhone) {
- // volume = lastVolume * nMaxSPOn
- // } else {
- // volume = lastVolume * nMaxSPOff
- // }
- // API.adjustVolume(fValue: volume)
- }
- // static func toggleSpeakerPhone() {
- // bSpeakerPhone = !bSpeakerPhone
- // var volume:Float! = 0
- // if (bSpeakerPhone) {
- // volume = lastVolume * nMaxSPOn
- // } else {
- // volume = lastVolume * nMaxSPOff
- // }
- // API.adjustVolume(fValue: volume)
- // }
-
- deinit {
- navigationController?.changeAppearance(clear: false)
- let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
- navigationController?.navigationBar.titleTextAttributes = textAttributes
- navigationController?.navigationBar.topItem?.backBarButtonItem = nil
- navigationController?.interactivePopGestureRecognizer?.isEnabled = true
- NotificationCenter.default.removeObserver(self)
- Nexilis.floatingButton.isHidden = false
- }
-
- override func viewWillDisappear(_ animated: Bool) {
- if self.isMovingFromParent {
- navigationController?.changeAppearance(clear: false)
- let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
- navigationController?.navigationBar.titleTextAttributes = textAttributes
- navigationController?.navigationBar.topItem?.backBarButtonItem = nil
- navigationController?.interactivePopGestureRecognizer?.isEnabled = true
- NotificationCenter.default.removeObserver(self)
- }
- Nexilis.floatingButton.isHidden = false
- }
-
- private func backToDefaultAudioSession() {
- do {
- let audioSession = AVAudioSession.sharedInstance()
- try audioSession.setCategory(.playAndRecord, mode: .voiceChat, options: [.allowBluetooth, .mixWithOthers])
- try audioSession.overrideOutputAudioPort(.speaker)
- try audioSession.setPreferredSampleRate(48000)
- try audioSession.setActive(true)
- } catch {
- }
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- QmeraVideoViewController.volumeView = MPVolumeView(frame: .zero)
- QmeraVideoViewController.volumeView.isHidden = true
- Nexilis.setWhiteboardReceiver(receiver: self)
- Nexilis.floatingButton.isHidden = true
- self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
- navigationController?.changeAppearance(clear: true)
-
- NotificationCenter.default.addObserver(self, selector: #selector(self.onStatusCall(_:)), name: NSNotification.Name(rawValue: Nexilis.listenerStatusCall), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(onReceiveMessage(notification:)), name: NSNotification.Name(rawValue: Nexilis.listenerReceiveChat), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(onCallFCM(notification:)), name: NSNotification.Name(rawValue: Nexilis.callFCM), object: nil)
-
- view.backgroundColor = .clear
- navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
- navigationController?.interactivePopGestureRecognizer?.isEnabled = false
- navigationItem.setHidesBackButton(true, animated: false)
- if fPin != ""{
- getDataProfile(fPin: fPin)
- }
-
- addZoomView()
- addCameraView()
- addListRemoteView()
- addBackgroundIncoming()
- addProfileNameCalling()
- Calling()
- addToolbar()
- addTimerVC()
- addImageMutedOnZoom()
- if isAutoAccept {
- didTapAcceptCallButton()
- }
- if autoAcceptAPN {
- DispatchQueue.global().async {
- do {
- if API.nGetCLXConnState() == 0 {
- let id = Utils.getConnectionID()
- try API.initConnection(sAPIK: Nexilis.sAPIKey, cbiI: Callback(), sTCPAddr: Nexilis.ADDRESS, nTCPPort: Nexilis.PORT, sUserID: id, sStartWH: "09:00")
- }
- } catch {
-
- }
- self.backToDefaultAudioSession()
- while API.nGetCLXConnState() == 0 {
- Thread.sleep(forTimeInterval : 0.3)
- }
- _ = Nexilis.writeSync(message: CoreMessage_TMessageBank.getNotifyCalling(fPin: self.fPin, lPin: User.getMyPin()!, type: "2"))
- }
- }
- self.timeStartCall = String(Date().currentTimeMillis())
- self.idCall = (User.getMyPin() ?? "") + CoreMessage_TMessageUtil.getTID()
- }
-
- override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
- if (keyPath! == "outputVolume") {
- if let newKey = change?[NSKeyValueChangeKey.newKey] as? NSNumber {
- QmeraVideoViewController.lastVolume = newKey.floatValue
- if (QmeraVideoViewController.bSpeakerPhone) {
- let volume = QmeraVideoViewController.lastVolume * QmeraVideoViewController.nMaxSPOn
- API.adjustVolume(fValue: volume)
- } else {
- let volume = QmeraVideoViewController.lastVolume * QmeraVideoViewController.nMaxSPOff
- API.adjustVolume(fValue: volume)
- }
- }
- }
- }
-
- func getDataProfile(fPin: String) {
- let query = "SELECT f_pin, first_name, last_name, official_account, image_id, device_id, offline_mode, user_type FROM BUDDY where f_pin = '\(fPin)'"
- Database.shared.database?.inTransaction({ (fmdb, rollback) in
- do {
- if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
- var row: [String: String?] = [:]
- if cursorData.next() {
- row["f_pin"] = cursorData.string(forColumnIndex: 0)
- var name = ""
- if let firstname = cursorData.string(forColumnIndex: 1) {
- name = firstname
- }
- if let lastname = cursorData.string(forColumnIndex: 2) {
- name = name + " " + lastname
- }
- row["name"] = name
- row["picture"] = cursorData.string(forColumnIndex: 4)
- row["isOfficial"] = cursorData.string(forColumnIndex: 3)
- row["deviceId"] = cursorData.string(forColumnIndex: 5)
- row["isOffline"] = cursorData.string(forColumnIndex: 6)
- row["user_type"] = cursorData.string(forColumnIndex: 7)
- if fPin != User.getMyPin() {
- dataPerson.append(row)
- }
- } else {
- var row: [String: String?] = [:]
- row["f_pin"] = fPin
- row["name"] = "User".localized()
- row["picture"] = ""
- row["isOfficial"] = ""
- row["deviceId"] = ""
- row["isOffline"] = ""
- row["user_type"] = ""
- dataPerson.append(row)
- }
- cursorData.close()
- } else {
- var row: [String: String?] = [:]
- row["f_pin"] = fPin
- row["name"] = "User".localized()
- row["picture"] = ""
- row["isOfficial"] = ""
- row["deviceId"] = ""
- row["isOffline"] = ""
- row["user_type"] = ""
- dataPerson.append(row)
- // if let response = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getAddFriendQRCode(fpin: fPin)), response.isOk() {
- // self.getDataProfile(fPin: fPin)
- // }
- // Nexilis.addFriend (fpin: "\(fPin)") { result in
- // if result {
- // self.getDataProfile(fPin: fPin)
- // } else {
- // let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- // imageView.tintColor = .white
- // let banner = FloatingNotificationBanner(title: "Server busy, please try again later".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
- // banner.show()
- // }
- // }
- }
- } catch {
- rollback.pointee = true
- print("Access database error: \(error.localizedDescription)")
- }
- })
- }
-
- func addTimerVC() {
- view.addSubview(containerTimerVC)
- containerTimerVC.anchor(top: view.safeAreaLayoutGuide.topAnchor, centerX: view.centerXAnchor, minWidth: 40)
- containerTimerVC.makeRoundedView(radius: 8)
- containerTimerVC.backgroundColor = .black.withAlphaComponent(0.3)
- containerTimerVC.addSubview(labelTimerVC)
- labelTimerVC.anchor(left: containerTimerVC.leftAnchor, right: containerTimerVC.rightAnchor, paddingLeft: 8, paddingRight: 8, centerX: containerTimerVC.centerXAnchor, centerY: containerTimerVC.centerYAnchor)
- labelTimerVC.textColor = .white
- containerTimerVC.isHidden = true
- }
-
- func addImageMutedOnZoom() {
- view.addSubview(mutedZoom)
- mutedZoom.anchor(top: containerTimerVC.bottomAnchor, paddingTop: 10, centerX: view.centerXAnchor, width: 30, height: 40)
- }
-
- func addZoomView() {
- view.addSubview(zoomView)
- zoomView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- zoomView.topAnchor.constraint(equalTo: view.topAnchor),
- zoomView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
- zoomView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
- zoomView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
- ])
- zoomView.backgroundColor = .secondaryColor
- zoomView.isUserInteractionEnabled = true
- zoomView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideToolbar)))
- }
-
- func addCameraView() {
- view.addSubview(cameraView)
- // cameraView.frame = CGRect(x: view.frame.width - 130, y: 20, width: 120, height: 160)
- cameraView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- cameraView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
- cameraView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10.0),
- cameraView.widthAnchor.constraint(equalToConstant: 120.0),
- cameraView.heightAnchor.constraint(equalToConstant: 160.0)
- ])
- cameraView.backgroundColor = .secondaryColor
- cameraView.makeRoundedView(radius: 8)
- }
-
- func addListRemoteView() {
- view.addSubview(scrollRemoteView)
- scrollRemoteView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- scrollRemoteView.topAnchor.constraint(equalTo: cameraView.bottomAnchor, constant: 10),
- scrollRemoteView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10),
- scrollRemoteView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
- scrollRemoteView.widthAnchor.constraint(equalToConstant: 120.0)
- ])
-
- scrollRemoteView.showsHorizontalScrollIndicator = false
- scrollRemoteView.showsVerticalScrollIndicator = false
- scrollRemoteView.contentSize.width = 120.0
- scrollRemoteView.backgroundColor = .clear
- }
-
- func addBackgroundIncoming() {
- view.addSubview(myImage)
- myImage.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- myImage.topAnchor.constraint(equalTo: view.topAnchor),
- myImage.bottomAnchor.constraint(equalTo: view.bottomAnchor),
- myImage.leadingAnchor.constraint(equalTo: view.leadingAnchor),
- myImage.trailingAnchor.constraint(equalTo: view.trailingAnchor),
- ])
- myImage.backgroundColor = .lightGray
- myImage.tintColor = .secondaryColor
- let image = dataPerson[0]["picture"]!!
- if image.isEmpty {
- myImage.image = UIImage(systemName: "person")
- myImage.contentMode = .scaleAspectFit
- } else {
- myImage.setImage(name: image)
- myImage.contentMode = .scaleAspectFill
- }
- // let idMe = User.getMyPin() as String?
- // Database().database?.inTransaction({ fmdb, rollback in
- // if let c = Database().getRecords(fmdb: fmdb, query: "select image_id from BUDDY where f_pin = '\(idMe!)'"), c.next() {
- // let image = c.string(forColumnIndex: 0)!
- // if image.isEmpty {
- // myImage.image = UIImage(systemName: "person")
- // myImage.contentMode = .scaleAspectFit
- // } else {
- // myImage.setImage(name: image)
- // myImage.contentMode = .scaleAspectFill
- // }
- // c.close()
- // }
- // })
- }
-
- func addProfileNameCalling() {
- view.addSubview(profileImage)
- profileImage.translatesAutoresizingMaskIntoConstraints = false
- profileImage.frame.size = CGSize(width: 60.0, height: 60.0)
- NSLayoutConstraint.activate([
- profileImage.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
- profileImage.centerXAnchor.constraint(equalTo: view.centerXAnchor),
- profileImage.widthAnchor.constraint(equalToConstant: 60.0),
- profileImage.heightAnchor.constraint(equalToConstant: 63.0)
- ])
- profileImage.backgroundColor = .lightGray
- profileImage.tintColor = .secondaryColor
- profileImage.circle()
- let image = dataPerson[0]["picture"]!!
- if image.isEmpty {
- profileImage.image = UIImage(systemName: "person")
- profileImage.contentMode = .scaleAspectFit
- profileImage.layer.borderWidth = 1
- profileImage.layer.borderColor = UIColor.secondaryColor.cgColor
- } else {
- profileImage.setImage(name: image)
- profileImage.contentMode = .scaleAspectFill
- }
-
- view.addSubview(name)
- name.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- name.topAnchor.constraint(equalTo: profileImage.bottomAnchor, constant: 5.0),
- name.centerXAnchor.constraint(equalTo: view.centerXAnchor)
- ])
- name.font = UIFont.systemFont(ofSize: 12)
- name.backgroundColor = .black.withAlphaComponent(0.05)
- name.layer.cornerRadius = 5.0
- name.clipsToBounds = true
- name.textColor = .mainColor
- name.text = dataPerson[0]["name"]!?.trimmingCharacters(in: .whitespaces)
- }
-
- func Calling() {
- view.addSubview(labelIncomingOutgoing)
- labelIncomingOutgoing.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- labelIncomingOutgoing.topAnchor.constraint(equalTo: name.bottomAnchor, constant: 40.0),
- labelIncomingOutgoing.centerXAnchor.constraint(equalTo: view.centerXAnchor)
- ])
- if isInisiator {
- labelIncomingOutgoing.text = "Connecting".localized()
- if ticketId.isEmpty {
- backToDefaultAudioSession()
- Nexilis.playRingbacktoneCall()
- if callFCM {
- DispatchQueue.global().async {
- if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getCalling(fPin: self.dataPerson[0]["f_pin"]!!, type: "2"), timeout: 30 * 1000) {
- if response.isOk() {
- DispatchQueue.main.async {
- self.labelIncomingOutgoing.text = "Waiting for answer".localized()
- }
- } else if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "01" && self.dataPerson.count > 0 {
- API.initiateCCall(sParty: self.dataPerson[0]["f_pin"]!, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: self.listRemoteViewFix, ivLocalView: self.cameraView, ivRemoteZ: self.zoomView)
- } else {
- DispatchQueue.main.async {
- if self.labelIncomingOutgoing.isDescendant(of: self.view) {
- self.labelIncomingOutgoing.text = "Busy".localized()
- }
- if self.buttonDecline.isDescendant(of: self.view) {
- self.buttonDecline.removeFromSuperview()
- }
- if self.buttonAccept.isDescendant(of: self.view) {
- self.buttonAccept.removeFromSuperview()
- }
- }
- }
- } else {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Unable to access servers. Try again later".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
- banner.show()
- }
- }
- } else {
- API.initiateCCall(sParty: dataPerson[0]["f_pin"]!, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView)
- }
- } else {
- backToDefaultAudioSession()
- API.ccs(sTicketID: ticketId, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView, bCameraOn: true)
- if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getIncomingCallCS(f_pin_opposite: users[0].pin), timeout: 30 * 1000){
- if response.mBodies[CoreMessage_TMessageKey.ERRCOD] != "01" {
- endAllCall()
- }
- }
- }
- } else {
- if ticketId.isEmpty && !autoAcceptAPN {
- backToDefaultAudioSession()
- Nexilis.playRingtoneCall()
- }
- labelIncomingOutgoing.text = "Incoming video call".localized() + "..."
- }
- labelIncomingOutgoing.font = UIFont.systemFont(ofSize: 12)
- labelIncomingOutgoing.backgroundColor = .black.withAlphaComponent(0.05)
- labelIncomingOutgoing.layer.cornerRadius = 5.0
- labelIncomingOutgoing.clipsToBounds = true
- labelIncomingOutgoing.textColor = .mainColor
- }
-
- func addToolbar(resetToolbar: Bool = false) {
- view.addSubview(buttonDecline)
- buttonDecline.translatesAutoresizingMaskIntoConstraints = false
- buttonDecline.frame.size = CGSize(width: 70.0, height: 70.0)
- if isInisiator || resetToolbar {
- constraintLeadingButtonDecline = buttonDecline.centerXAnchor.constraint(equalTo: view.centerXAnchor)
- } else {
- constraintLeadingButtonDecline = buttonDecline.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: view.frame.width * 0.2)
- }
- constraintBottomButtonDecline = buttonDecline.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -60.0)
- NSLayoutConstraint.activate([
- constraintBottomButtonDecline,
- constraintLeadingButtonDecline,
- buttonDecline.widthAnchor.constraint(equalToConstant: 70.0),
- buttonDecline.heightAnchor.constraint(equalToConstant: 70.0)
- ])
- buttonDecline.backgroundColor = .red
- buttonDecline.circle()
- buttonDecline.setImage(UIImage(systemName: "xmark", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
- buttonDecline.tintColor = .white
- buttonDecline.addTarget(self, action: #selector(didTapDeclineCallButton(sender:)), for: .touchUpInside)
-
- if !isInisiator && !resetToolbar {
- view.addSubview(buttonAccept)
- buttonAccept.translatesAutoresizingMaskIntoConstraints = false
- buttonAccept.frame.size = CGSize(width: 70.0, height: 70.0)
- NSLayoutConstraint.activate([
- buttonAccept.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -60.0),
- buttonAccept.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -(view.frame.width * 0.2)),
- buttonAccept.widthAnchor.constraint(equalToConstant: 70.0),
- buttonAccept.heightAnchor.constraint(equalToConstant: 70.0)
- ])
- buttonAccept.backgroundColor = .greenColor
- buttonAccept.circle()
- buttonAccept.setImage(UIImage(systemName: "checkmark", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
- buttonAccept.tintColor = .white
- buttonAccept.addTarget(self, action: #selector(didTapAcceptCallButton), for: .touchUpInside)
- }
- }
-
- @objc func onCallFCM(notification: NSNotification) {
- DispatchQueue.main.async { [self] in
- let data:[AnyHashable : Any] = notification.userInfo!
- if let l_pin = data["l_pin"] as? String {
- if let f_pin = data["f_pin"] as? String {
- if f_pin == User.getMyPin()! {
- if !Nexilis.callAPNActivated {
- labelIncomingOutgoing.text = "Waiting for answer".localized()
- }
- API.initiateCCall(sParty: l_pin, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView)
- }
- }
- }
- }
- }
-
- @objc func onReceiveMessage(notification: NSNotification) {
- DispatchQueue.main.async {
- let data:[AnyHashable : Any] = notification.userInfo!
- if let dataMessage = data["message"] as? TMessage {
- if (dataMessage.getCode() == CoreMessage_TMessageCode.PUSH_MEMBER_ROOM_CONTACT_CENTER) {
- let data = dataMessage.getBody(key: CoreMessage_TMessageKey.DATA)
- if !data.isEmpty {
- if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions()) as? [AnyObject] {
- var members = ""
- let idMe = User.getMyPin()!
- for json in jsonArray {
- if "\(json)" != idMe {
- if members.isEmpty {
- members = "\(json)"
- } else {
- members += ",\(json)"
- }
- }
- }
- SecureUserDefaults.shared.set(members, forKey: "inEditorPersonal")
- SecureUserDefaults.shared.set("\(members)", forKey: "membersCC")
- }
- }
- self.users.append(User.getData(pin: dataMessage.getPIN())!)
- }
- }
- }
- }
-
- private func makeStateCall() {
- var longCall = "0"
- if self.vcTimer.isValid {
- longCall = self.labelTimerVC.text ?? ""
- }
- if self.isInisiator {
- Nexilis.saveMessageCall(idCall: self.idCall, textMessage: "Outgoing video call" + " at \(longCall)", fPin: User.getMyPin() ?? "", lPin: self.dataPerson[0]["f_pin"]!!, timeCall: self.timeStartCall, attachment_type: MessageScope.CALL)
- } else {
- if !self.vcTimer.isValid {
- Nexilis.saveMessageCall(idCall: self.idCall, textMessage: "Missed video call" + " at 0", fPin: self.dataPerson[0]["f_pin"]!!, lPin: User.getMyPin() ?? "", timeCall: self.timeStartCall, attachment_type: MessageScope.MISSED_CALL)
- } else {
- Nexilis.saveMessageCall(idCall: self.idCall, textMessage: "Incoming video call" + " at \(longCall)", fPin: self.dataPerson[0]["f_pin"]!!, lPin: User.getMyPin() ?? "", timeCall: self.timeStartCall, attachment_type: MessageScope.CALL)
- }
- }
- }
-
- @objc func didTapDeclineCallButton(sender: AnyObject){
- let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
- if !onGoingCC.isEmpty {
- let alert = LibAlertController(title: "Interaction with Call Center is in progress".localized(), message: "Are you sure you want to end the Call Center?".localized(), preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "No".localized(), style: UIAlertAction.Style.default, handler: nil))
- alert.addAction(UIAlertAction(title: "Yes".localized(), style: UIAlertAction.Style.default, handler: {(_) in
- if(!self.wbRoomId.isEmpty){
- DispatchQueue.main.async {
- self.wbTimer.invalidate()
- _ = Nexilis.getWhiteboardDelegate()?.terminate()
- }
- }
- Nexilis.stopRingtoneCall()
- Nexilis.stopRingbacktoneCall()
- QmeraVideoViewController.isLoop = false
- QmeraVideoViewController.bSpeakerPhone = false
- do { try AVAudioSession.sharedInstance().setActive(false) } catch {}
- Nexilis.callAPNActivated = false
- self.endAllCall()
- self.dismiss(animated: true, completion: nil)
- }))
- self.present(alert, animated: true, completion: nil)
- } else {
- let alert = LibAlertController(title: "End Video Call".localized(), message: "Are you sure you want to end video call?".localized(), preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "No".localized(), style: UIAlertAction.Style.default, handler: nil))
- alert.addAction(UIAlertAction(title: "Yes".localized(), style: UIAlertAction.Style.default, handler: {(_) in
- if self.callFCM && !self.vcTimer.isValid {
- DispatchQueue.global().async {
- if let _ = Nexilis.writeSync(message: CoreMessage_TMessageBank.getCancelCall(fPin: self.dataPerson[0]["f_pin"]!!, type: "2"), timeout: 30 * 1000) {
- } else {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Unable to access servers. Try again later".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
- banner.show()
- }
- }
- }
- self.makeStateCall()
- Nexilis.stopRingtoneCall()
- Nexilis.stopRingbacktoneCall()
- // if self.labelIncomingOutgoing.isDescendant(of: self.view) {
- // self.labelIncomingOutgoing.text = "Video call is over".localized()
- // }
- if self.stackViewToolbar.isDescendant(of: self.view){
- self.stackViewToolbar.removeFromSuperview()
- }
- if self.stackViewToolbar2.isDescendant(of: self.view){
- self.stackViewToolbar2.removeFromSuperview()
- }
- if self.buttonWB.isDescendant(of: self.view){
- self.buttonWB.removeFromSuperview()
- }
- if self.buttonChat.isDescendant(of: self.view){
- self.buttonChat.removeFromSuperview()
- }
- if self.buttonDecline.isDescendant(of: self.view) {
- self.buttonDecline.removeFromSuperview()
- }
- if self.buttonAccept.isDescendant(of: self.view) {
- self.buttonAccept.removeFromSuperview()
- }
- if self.buttonRotate.isDescendant(of: self.view) {
- self.buttonRotate.removeFromSuperview()
- }
- if self.buttonMuted.isDescendant(of: self.view) {
- self.buttonMuted.removeFromSuperview()
- }
- if self.wbVC != nil{
- self.wbVC!.close?()
- }
- self.wbTimer.invalidate()
- self.vcTimer.invalidate()
- // self.labelTimerVC.text = "Video call is over".localized()
- QmeraVideoViewController.isLoop = false
- QmeraVideoViewController.bSpeakerPhone = false
- do { try AVAudioSession.sharedInstance().setActive(false) } catch {}
- Nexilis.callAPNActivated = false
- self.endAllCall()
- DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
- if self.isInisiator && !self.isPresent {
- self.navigationController?.popViewController(animated: true)
- } else {
- self.dismiss(animated: true, completion: nil)
- }
- }
- }))
- self.present(alert, animated: true, completion: nil)
- }
- }
-
- @objc func didTapAcceptCallButton() {
- if !isInisiator{
- let goAudioCall = Nexilis.checkMicPermission()
- let goVideoCall = Nexilis.checkCameraPermission()
- if goVideoCall == 0 {
- let alert = LibAlertController(title: "Attention!".localized(), message: !goAudioCall && goVideoCall == 0 ? "Please allow microphone & camera permission in your settings".localized() : !goAudioCall ? "Please allow microphone permission in your settings".localized() : "Please allow camera permission in your settings", preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "OK".localized(), style: UIAlertAction.Style.default, handler: {_ in
- if let url = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(url) {
- UIApplication.shared.open(url, options: [:], completionHandler: nil)
- }
- }))
- self.navigationController?.present(alert, animated: true, completion: nil)
- return
- } else if goVideoCall == -1 {
- return
- }
- // Nexilis.startAudio()
- if ticketId.isEmpty {
- API.receiveCCall(sParty: dataPerson[0]["f_pin"]!, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView,ivRemoteZ: zoomView)
- } else {
- backToDefaultAudioSession()
- API.csa(sTicketID: ticketId, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView, bCameraOn: true)
- }
- }
- DispatchQueue.main.async {
- self.myImage.removeFromSuperview()
- self.name.removeFromSuperview()
- self.profileImage.removeFromSuperview()
- self.labelIncomingOutgoing.removeFromSuperview()
- self.buttonAccept.removeFromSuperview()
- NSLayoutConstraint.deactivate([
- self.constraintLeadingButtonDecline,
- self.constraintBottomButtonDecline
- ])
- self.addToolbarAfterAccept()
- self.buttonDecline.setImage(UIImage(systemName: "phone.down", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
- UIView.animate(withDuration: 1.0, animations: {
- self.view.layoutIfNeeded()
- })
- DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
- self.containerTimerVC.isHidden = false
- self.buttonRotate.isHidden = false
- self.buttonAddParticipant.isHidden = false
- self.buttonSpeaker.isHidden = false
- self.buttonWB.isHidden = false
- self.buttonChat.isHidden = false
- self.poweredByView.isHidden = false
- self.buttonMuted.isHidden = false
- let connectDate = Date()
- self.vcTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
- let format = Utils.callDurationFormatter.string(from: Date().timeIntervalSince(connectDate))
- self.labelTimerVC.text = format
- }
- self.vcTimer.fire()
- }
- }
- }
-
- private func resetViewToOutgoing() {
- self.vcTimer.invalidate()
- self.zoomView.image = nil
- self.cameraView.image = nil
- if self.stackViewToolbar.isDescendant(of: self.view){
- self.stackViewToolbar.removeFromSuperview()
- }
- if self.stackViewToolbar2.isDescendant(of: self.view){
- self.stackViewToolbar2.removeFromSuperview()
- }
- if self.buttonWB.isDescendant(of: self.view){
- self.buttonWB.removeFromSuperview()
- }
- if self.buttonChat.isDescendant(of: self.view){
- self.buttonChat.removeFromSuperview()
- }
- if self.buttonDecline.isDescendant(of: self.view) {
- self.buttonDecline.removeFromSuperview()
- }
- if self.buttonAccept.isDescendant(of: self.view) {
- self.buttonAccept.removeFromSuperview()
- }
- if self.buttonRotate.isDescendant(of: self.view) {
- self.buttonRotate.removeFromSuperview()
- }
- if self.buttonMuted.isDescendant(of: self.view) {
- self.buttonMuted.removeFromSuperview()
- }
- addBackgroundIncoming()
- addProfileNameCalling()
- addToolbar(resetToolbar: true)
- }
-
- @objc func didTapChatButton(){
- let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
- let members: String = SecureUserDefaults.shared.value(forKey: "membersCC") ?? ""
- let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
- let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
- editorPersonalVC.hidesBottomBarWhenPushed = true
- editorPersonalVC.unique_l_pin = officer
- editorPersonalVC.fromNotification = true
- editorPersonalVC.isContactCenter = true
- editorPersonalVC.fPinContacCenter = members
- editorPersonalVC.complaintId = ticketId
- editorPersonalVC.onGoingCC = true
- editorPersonalVC.isRequestContactCenter = false
- editorPersonalVC.channelContactCenter = "2"
- editorPersonalVC.users = users
- editorPersonalVC.fromVCAC = true
- let navigationController = CustomNavigationController(rootViewController: editorPersonalVC)
- navigationController.modalPresentationStyle = .overCurrentContext
- navigationController.navigationBar.tintColor = .white
- navigationController.navigationBar.barTintColor = self.traitCollection.userInterfaceStyle == .dark ? .blackDarkMode : .mainColor
- navigationController.navigationBar.isTranslucent = false
- navigationController.navigationBar.overrideUserInterfaceStyle = .dark
- navigationController.navigationBar.barStyle = .black
- let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]
- UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, for: .normal)
- let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
- navigationController.navigationBar.titleTextAttributes = textAttributes
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
- }
- }
-
- @objc func didTapWBButton(){
- if(wbVC == nil){
- wbVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "wbVC") as? WhiteboardViewController
- if(wbRoomId.isEmpty){
- let me = User.getMyPin()!
- let tid = CoreMessage_TMessageUtil.getTID()
- wbRoomId = "\(me)wbvc\(tid)"
- wbVC!.roomId = wbRoomId
- var destinations = [String]()
- var destString = ""
- for d in dataPerson{
- destinations.append(d["f_pin"]!!)
- if destString.isEmpty{
- destString = d["f_pin"]!!
- } else {
- destString = destString + ",\(d["f_pin"]!!)"
- }
- }
- wbVC!.destinations = destinations
- wbVC!.sendInit()
- SecureUserDefaults.shared.set("\(me),\(destString)", forKey: "wb_vc")
- }
- else {
- self.wbTimer.invalidate()
- self.buttonWB.backgroundColor = .lightGray
- wbVC!.roomId = wbRoomId
- wbVC!.sendJoin()
- }
- }
- wbVC!.close = {
- DispatchQueue.main.async {
- if self.wbVC!.view.isDescendant(of: self.view){
- self.wbVC!.view.removeFromSuperview()
- }
- self.buttonDecline.isHidden = false
- self.buttonSpeaker.isHidden = false
- self.buttonAddParticipant.isHidden = false
- self.buttonRotate.isHidden = false
- self.buttonMuted.isHidden = false
- // if(!self.wbRoomId.isEmpty){
- // DispatchQueue.main.async {
- // self.wbTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.runTimer), userInfo: nil, repeats: true)
- // }
- // }
- }
- }
- self.buttonDecline.isHidden = true
- self.buttonSpeaker.isHidden = true
- self.buttonMuted.isHidden = true
- self.buttonAddParticipant.isHidden = true
- self.buttonRotate.isHidden = true
- addChild(wbVC!)
- wbVC!.view.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(wbVC!.view)
- onScreenConstraintWB = [
- wbVC!.view.topAnchor.constraint(equalTo: self.view.topAnchor),
- wbVC!.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
- wbVC!.view.rightAnchor.constraint(equalTo: self.view.rightAnchor),
- wbVC!.view.leftAnchor.constraint(equalTo: self.view.leftAnchor),
- ]
- NSLayoutConstraint.activate(onScreenConstraintWB)
-
- // Notify the child view controller that the move is complete.
- wbVC!.didMove(toParent: self)
- // self.navigationController?.setNavigationBarHidden(false, animated: true)
- // controller.modalPresentationStyle = .overCurrentContext
- // self.navigationController?.present(controller, animated: true)
- }
-
- func addToolbarAfterAccept() {
- Nexilis.stopRingtoneCall()
- view.addSubview(self.stackViewToolbar)
- self.stackViewToolbar.translatesAutoresizingMaskIntoConstraints = false
- constraintBottomStackViewToolbar = self.stackViewToolbar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -60.0)
- NSLayoutConstraint.activate([
- self.stackViewToolbar.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
- constraintBottomStackViewToolbar
- ])
- self.stackViewToolbar.axis = .horizontal
- self.stackViewToolbar.distribution = .equalSpacing
- self.stackViewToolbar.alignment = .center
- self.stackViewToolbar.spacing = 30
-
- view.addSubview(self.stackViewToolbar3)
- self.stackViewToolbar3.anchor(bottom: self.stackViewToolbar.topAnchor, paddingBottom: 10, centerX: self.view.centerXAnchor)
- self.stackViewToolbar3.axis = .horizontal
- self.stackViewToolbar3.distribution = .equalSpacing
- self.stackViewToolbar3.alignment = .center
- self.stackViewToolbar3.spacing = 30
-
- view.addSubview(buttonRotate)
- buttonRotate.translatesAutoresizingMaskIntoConstraints = false
- buttonRotate.frame.size = CGSize(width: 70.0, height: 70.0)
- NSLayoutConstraint.activate([
- buttonRotate.widthAnchor.constraint(equalToConstant: 70.0),
- buttonRotate.heightAnchor.constraint(equalToConstant: 70.0),
- ])
- buttonRotate.backgroundColor = .secondaryColor
- buttonRotate.setImage(UIImage(systemName: "arrow.triangle.2.circlepath.camera", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
- buttonRotate.tintColor = .mainColor
- buttonRotate.circle()
- buttonRotate.isHidden = true
- buttonRotate.addTarget(self, action: #selector(camera(sender:)), for: .touchUpInside)
-
- view.addSubview(buttonMuted)
- buttonMuted.translatesAutoresizingMaskIntoConstraints = false
- buttonMuted.frame.size = CGSize(width: 70.0, height: 70.0)
- NSLayoutConstraint.activate([
- buttonMuted.widthAnchor.constraint(equalToConstant: 70.0),
- buttonMuted.heightAnchor.constraint(equalToConstant: 70.0),
- ])
- buttonMuted.backgroundColor = .secondaryColor
- buttonMuted.setImage(UIImage(systemName: "mic", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
- buttonMuted.tintColor = .mainColor
- buttonMuted.circle()
- buttonMuted.isHidden = true
- buttonMuted.addTarget(self, action: #selector(muted(sender:)), for: .touchUpInside)
-
- view.addSubview(buttonAddParticipant)
- buttonAddParticipant.translatesAutoresizingMaskIntoConstraints = false
- buttonAddParticipant.frame.size = CGSize(width: 70.0, height: 70.0)
- NSLayoutConstraint.activate([
- buttonAddParticipant.widthAnchor.constraint(equalToConstant: 70.0),
- buttonAddParticipant.heightAnchor.constraint(equalToConstant: 70.0)
- ])
- buttonAddParticipant.backgroundColor = .secondaryColor
- buttonAddParticipant.setImage(UIImage(systemName: "person.badge.plus", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
- buttonAddParticipant.tintColor = .mainColor
- buttonAddParticipant.circle()
- buttonAddParticipant.isHidden = true
- buttonAddParticipant.addTarget(self, action: #selector(didTapAddParticipantButton(sender:)), for: .touchUpInside)
-
- view.addSubview(buttonSpeaker)
- buttonSpeaker.translatesAutoresizingMaskIntoConstraints = false
- buttonSpeaker.frame.size = CGSize(width: 70.0, height: 70.0)
- NSLayoutConstraint.activate([
- buttonSpeaker.widthAnchor.constraint(equalToConstant: 70.0),
- buttonSpeaker.heightAnchor.constraint(equalToConstant: 70.0)
- ])
- buttonSpeaker.backgroundColor = .secondaryColor
- buttonSpeaker.tintColor = .mainColor
- buttonSpeaker.setImage(UIImage(systemName: "speaker.slash", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
- buttonSpeaker.circle()
- buttonSpeaker.isHidden = true
- buttonSpeaker.addTarget(self, action: #selector(didTapSpeakerButton(sender:)), for: .touchUpInside)
-
- view.addSubview(self.stackViewToolbar2)
- self.stackViewToolbar2.translatesAutoresizingMaskIntoConstraints = false
- constraintLeftStackViewToolbar2 = self.stackViewToolbar2.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 10.0)
- NSLayoutConstraint.activate([
- self.stackViewToolbar2.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
- constraintLeftStackViewToolbar2
- ])
- self.stackViewToolbar2.axis = .vertical
- self.stackViewToolbar2.distribution = .equalSpacing
- self.stackViewToolbar2.alignment = .center
- self.stackViewToolbar2.spacing = 5
-
- view.addSubview(buttonWB)
- buttonWB.translatesAutoresizingMaskIntoConstraints = false
- buttonWB.frame.size = CGSize(width: 40.0, height: 40.0)
- NSLayoutConstraint.activate([
- buttonWB.widthAnchor.constraint(equalToConstant: 40.0),
- buttonWB.heightAnchor.constraint(equalToConstant: 40.0)
- ])
- buttonWB.backgroundColor = .lightGray
- buttonWB.setImage(UIImage(systemName: "ipad.landscape", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .medium, scale: .default)), for: .normal)
- buttonWB.circle()
- buttonWB.tintColor = .black
- buttonWB.isHidden = true
- buttonWB.addTarget(self, action: #selector(didTapWBButton), for: .touchUpInside)
-
- if !ticketId.isEmpty {
- view.addSubview(buttonChat)
- buttonChat.translatesAutoresizingMaskIntoConstraints = false
- buttonChat.frame.size = CGSize(width: 40.0, height: 40.0)
- NSLayoutConstraint.activate([
- buttonChat.widthAnchor.constraint(equalToConstant: 40.0),
- buttonChat.heightAnchor.constraint(equalToConstant: 40.0)
- ])
- buttonChat.backgroundColor = .lightGray
- buttonChat.setImage(UIImage(systemName: "bubble.right", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .medium, scale: .default)), for: .normal)
- buttonChat.circle()
- buttonChat.tintColor = .black
- buttonChat.isHidden = true
- buttonChat.addTarget(self, action: #selector(didTapChatButton), for: .touchUpInside)
- }
-
- view.addSubview(poweredByView)
- self.poweredByView.translatesAutoresizingMaskIntoConstraints = false
- let constraintRightPowered = self.poweredByView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -10.0)
- let constraintBottomPowered = self.poweredByView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10.0)
- NSLayoutConstraint.activate([
- constraintRightPowered,
- constraintBottomPowered,
- nexilisLogo.widthAnchor.constraint(equalToConstant: 30.0),
- nexilisLogo.heightAnchor.constraint(equalToConstant: 30.0)
- ])
-
- poweredByView.addArrangedSubview(poweredByLabel)
- poweredByView.addArrangedSubview(nexilisLogo)
- poweredByView.isHidden = true
-
- stackViewToolbar.addArrangedSubview(buttonAddParticipant)
- stackViewToolbar.addArrangedSubview(buttonDecline)
- stackViewToolbar.addArrangedSubview(buttonSpeaker)
- stackViewToolbar2.addArrangedSubview(buttonWB)
- stackViewToolbar2.addArrangedSubview(buttonChat)
- stackViewToolbar3.addArrangedSubview(buttonRotate)
- stackViewToolbar3.addArrangedSubview(buttonMuted)
- // startFaceTimer()
- }
-
- func endAllCall() {
- let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
- if !onGoingCC.isEmpty {
- let requester = onGoingCC.components(separatedBy: ",")[0]
- let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
- let complaintId = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2]
- let idMe = User.getMyPin()!
- let startTimeCC: String = SecureUserDefaults.shared.value(forKey: "startTimeCC") ?? ""
- DispatchQueue.global().async {
- let date = "\(Date().currentTimeMillis())"
- Database.shared.database?.inTransaction({ (fmdb, rollback) in
- do {
- _ = try Database.shared.insertRecord(fmdb: fmdb, table: "CALL_CENTER_HISTORY", cvalues: [
- "type" : "2",
- "title" : "Contact Center".localized(),
- "time" : startTimeCC,
- "f_pin" : officer,
- "data" : complaintId,
- "time_end" : date,
- "complaint_id" : complaintId,
- "members" : "",
- "requester": requester
- ], replace: true)
- } catch {
- rollback.pointee = true
- print("Access database error: \(error.localizedDescription)")
- }
- })
- if officer == idMe {
- _ = Nexilis.write(message: CoreMessage_TMessageBank.endCallCenter(complaint_id: complaintId, l_pin: requester))
- } else {
- if requester == idMe {
- _ = Nexilis.write(message: CoreMessage_TMessageBank.endCallCenter(complaint_id: complaintId, l_pin: officer))
- } else {
- _ = Nexilis.write(message: CoreMessage_TMessageBank.leaveCCRoomInvite(ticket_id: complaintId))
- }
- }
- SecureUserDefaults.shared.removeValue(forKey: "onGoingCC")
- SecureUserDefaults.shared.removeValue(forKey: "membersCC")
- SecureUserDefaults.shared.removeValue(forKey: "startTimeCC")
- SecureUserDefaults.shared.removeValue(forKey: "waitingRequestCC")
- }
- }
- API.terminateCall(sParty: nil)
- if APIS.uuidCall != nil {
- CallManager.shared.endCall(uuid: APIS.uuidCall!) {
- APIS.uuidCall = nil
- }
- }
- cameraView.image = nil
- zoomView.image = nil
- listRemoteViewFix.removeAll()
- dataPerson.removeAll()
- }
-
- func setSpeaker() {
- DispatchQueue.main.async {
- self.timerSpeaker?.invalidate()
- self.tempSpeaker = !self.tempSpeaker
- if (self.tempSpeaker) {
- self.buttonSpeaker.backgroundColor = .lightGray
- self.buttonSpeaker.tintColor = .mainColor
- self.buttonSpeaker.setImage(UIImage(systemName: "speaker.wave.2", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
- } else {
- self.buttonSpeaker.backgroundColor = .secondaryColor
- self.buttonSpeaker.tintColor = .mainColor
- self.buttonSpeaker.setImage(UIImage(systemName: "speaker.slash", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
- }
- self.timerSpeaker = Timer.scheduledTimer(withTimeInterval: 1, repeats: false, block: {_ in
- if QmeraVideoViewController.bSpeakerPhone != self.tempSpeaker {
- QmeraVideoViewController.bSpeakerPhone = !QmeraVideoViewController.bSpeakerPhone
- self.tempSpeaker = QmeraVideoViewController.bSpeakerPhone
- DispatchQueue.global().async {
- QmeraVideoViewController.turnSpeakerOn()
- }
- }
- })
- }
- }
-
- @objc func didTapSpeakerButton(sender: AnyObject){
- setSpeaker()
- }
-
- @objc func didTapAddParticipantButton(sender: AnyObject){
- if let contactViewController = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactSID") as? ContactCallViewController {
- contactViewController.isAddParticipantVideo = true
- contactViewController.connectedCall = dataPerson
- contactViewController.isDismiss = { data in
- let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
- if !onGoingCC.isEmpty {
- DispatchQueue.global().async {
- _ = Nexilis.write(message: CoreMessage_TMessageBank.getCCRoomInvite(l_pin: data["f_pin"]!!, ticket_id: onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2], channel: "2"))
- }
- DispatchQueue.main.async {
- self.isAddCall = data["f_pin"]!!
- }
- } else {
- if self.callFCM {
- DispatchQueue.global().async {
- if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getCalling(fPin: data["f_pin"]!!, type: "2"), timeout: 30 * 1000) {
- if response.isOk() {
- } else if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "01" {
- self.dataPerson.append(data)
- if let user = User.getData(pin: String(data["f_pin"]!!)) {
- self.users.append(user)
- }
- API.initiateCCall(sParty: data["f_pin"]!, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: self.listRemoteViewFix, ivLocalView: self.cameraView, ivRemoteZ: self.zoomView)
- Nexilis.playRingbacktoneCall()
- } else {
- Nexilis.stopRingbacktoneCall()
- }
- } else {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Unable to access servers. Try again later".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
- banner.show()
- }
- }
- } else {
- DispatchQueue.main.async {
- self.dataPerson.append(data)
- if let user = User.getData(pin: String(data["f_pin"]!!)) {
- self.users.append(user)
- }
- API.initiateCCall(sParty: data["f_pin"]!, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: self.listRemoteViewFix, ivLocalView: self.cameraView, ivRemoteZ: self.zoomView)
- Nexilis.playRingbacktoneCall()
- }
- }
- }
- }
- present(CustomNavigationController(rootViewController: contactViewController), animated: true, completion: nil)
- }
- }
-
- @objc func camera(sender: Any?) {
- DispatchQueue.global().async { [self] in
- if frontCamera {
- API.changeCameraParam(nCameraIdx: 0, nResolutionIndex: 2, nQuality: 4)
- frontCamera = false
- } else {
- API.changeCameraParam(nCameraIdx: 1, nResolutionIndex: 2, nQuality: 4)
- frontCamera = true
- }
- }
- }
-
- @objc func muted(sender: Any?) {
- isMuted = !isMuted
- API.mmc(int: 1, boolean: isMuted)
- DispatchQueue.main.async {
- if (self.isMuted) {
- self.buttonMuted.backgroundColor = .lightGray
- self.buttonMuted.tintColor = .mainColor
- self.buttonMuted.setImage(UIImage(systemName: "mic.slash", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
- } else {
- self.buttonMuted.backgroundColor = .secondaryColor
- self.buttonMuted.tintColor = .mainColor
- self.buttonMuted.setImage(UIImage(systemName: "mic", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
- }
- }
- }
-
- @objc func hideToolbar() {
- DispatchQueue.main.async {
- if self.showStackViewToolbar {
- self.showStackViewToolbar = false
- self.constraintBottomStackViewToolbar.constant = 150
- self.constraintLeftStackViewToolbar2.constant = -60
- UIView.animate(withDuration: 0.35, animations: {
- self.view.layoutIfNeeded()
- })
- } else {
- self.showStackViewToolbar = true
- self.constraintBottomStackViewToolbar.constant = -60
- self.constraintLeftStackViewToolbar2.constant = 10
- UIView.animate(withDuration: 0.35, animations: {
- self.view.layoutIfNeeded()
- })
- }
- }
- }
-
- @objc func onStatusCall(_ notification: NSNotification) {
- let data = notification.userInfo
- let state = (data?["state"] ?? 0) as! Int
- let message = (data?["message"] ?? "") as! String
- var remoteChannel = [String:String]()
- let arrayMessage = message.split(separator: ",")
- if state == Nexilis.VIDEO_CALL_INCOMING {
- if autoAcceptAPN {
- didTapAcceptCallButton()
- }
- }
- else if state == Nexilis.AUDIO_VIDEO_CALL_MUTED {
- DispatchQueue.main.async { [self] in
- if self.dataPerson.count == 1 {
- if arrayMessage[1] == "1" {
- mutedZoom.isHidden = false
- } else {
- mutedZoom.isHidden = true
- }
- }
- }
- } else if (state == Nexilis.VIDEO_CALL_ZOOM) && self.dataPerson.count > 1 {
- DispatchQueue.main.async {
- self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 2.0).rotated(by: (-CGFloat.pi)/2)
- self.zoomView.contentMode = .scaleAspectFit
- }
- } else if (state == Nexilis.VIDEO_CAMERA_PARAMS_CHANED){
- if(arrayMessage[3] == "0"){
- DispatchQueue.main.async {
- if self.dataPerson.count == 1 && arrayMessage[2] == "1" && arrayMessage[4] == "1" {
- self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 2.0).rotated(by: (-CGFloat.pi)/2)
- } else {
- self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 2.0).rotated(by: (CGFloat.pi)/2)
- }
- }
- }
- }
- else if state == Nexilis.STREAMING_SEMINAR_ENDED { // always call turnspeaker
- // QmeraVideoViewController.isLoop = true
- // DispatchQueue.global().asyncAfter(deadline: .now() + 0.5, execute: {
- // API.adjustVolume(fValue: 10.0)
- // self.setSpeaker()
- // DispatchQueue.global(qos: .userInitiated).async {
- // repeat {
- // Thread.sleep(forTimeInterval : 1)
- // if (QmeraVideoViewController.isLoop && !API.bAudioEngineIsRunning()) {
- // API.turnSpeakerPhone(bSPon: QmeraVideoViewController.bSpeakerPhone!)
- // }
- // } while (QmeraVideoViewController.isLoop)
- // }
- // })
- self.setSpeaker()
- }
- else if (state == Nexilis.VIDEO_CALL_OFFHOOK) {
- DispatchQueue.main.async {
- Nexilis.stopRingbacktoneCall()
- }
- let channel = arrayMessage[3]
- remoteChannel[String(channel)] = String(arrayMessage[5])
- DispatchQueue.main.async {
- if (self.dataPerson.count == 1 && String(arrayMessage[1]) != self.dataPerson[0]["f_pin"]!!) {
- self.getDataProfile(fPin: String(arrayMessage[1]))
- for i in 0...1 {
- self.scrollRemoteView.addSubview(self.listRemoteViewFix[i])
- self.listRemoteViewFix[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 160)
- self.listRemoteViewFix[i].backgroundColor = .clear
- self.listRemoteViewFix[i].makeRoundedView(radius: 8.0)
- self.scrollRemoteView.addSubview(self.containerLabelName[i])
- self.containerLabelName[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 30)
- self.containerLabelName[i].backgroundColor = .orangeBNI.withAlphaComponent(0.5)
- self.containerLabelName[i].makeRoundedView(radius: 8.0)
- if i == 0 {
- if self.dataPerson[0]["user_type"] == "2" {
- self.listRemoteViewFix[0].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi)/2)
- } else {
- self.listRemoteViewFix[0].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (-CGFloat.pi)/2)
- }
- } else {
- if arrayMessage[5] == "2" {
- self.listRemoteViewFix[1].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi)/2)
- } else {
- self.listRemoteViewFix[1].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (-CGFloat.pi)/2)
- }
- }
- let pictureImage = self.dataPerson[i]["picture"] ?? ""
- let namePerson = self.dataPerson[i]["name"] ?? ""
- if (!pictureImage!.isEmpty) {
- self.listRemoteViewFix[i].setImage(name: pictureImage!)
- self.listRemoteViewFix[i].contentMode = .scaleAspectFill
- } else {
- self.listRemoteViewFix[i].image = UIImage(systemName: "person")
- self.listRemoteViewFix[i].backgroundColor = UIColor.systemGray6
- self.listRemoteViewFix[i].contentMode = .scaleAspectFit
- }
- let labelName = UILabel()
- self.containerLabelName[i].addSubview(labelName)
- labelName.anchor(left: self.containerLabelName[i].leftAnchor, right: self.containerLabelName[i].rightAnchor, paddingLeft: 5, paddingRight: 5, centerX: self.containerLabelName[i].centerXAnchor, centerY: self.containerLabelName[i].centerYAnchor)
- labelName.text = namePerson
- labelName.textAlignment = .center
- labelName.textColor = .white
- }
- self.scrollRemoteView.contentSize.height = CGFloat(170 * 2)
- if self.buttonWB.isEnabled {
- self.buttonWB.isEnabled = false
- }
- if self.buttonRotate.isEnabled {
- self.buttonRotate.isEnabled = false
- if self.wbVC != nil && self.wbVC!.view.isDescendant(of: self.view){
- self.wbVC!.view.removeFromSuperview()
- self.buttonDecline.isHidden = false
- self.buttonSpeaker.isHidden = false
- self.buttonAddParticipant.isHidden = false
- self.buttonRotate.isHidden = false
- self.buttonMuted.isHidden = false
- }
- }
- } else if self.dataPerson.count > 1 {
- if self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[1]}) == nil {
- self.getDataProfile(fPin: String(arrayMessage[1]))
- let i = self.dataPerson.count - 1
- self.scrollRemoteView.addSubview(self.listRemoteViewFix[i])
- self.listRemoteViewFix[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 160)
- self.listRemoteViewFix[i].backgroundColor = .clear
- self.listRemoteViewFix[i].makeRoundedView(radius: 8.0)
- self.scrollRemoteView.addSubview(self.containerLabelName[i])
- self.containerLabelName[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 30)
- self.containerLabelName[i].backgroundColor = .orangeBNI.withAlphaComponent(0.5)
- self.containerLabelName[i].makeRoundedView(radius: 8.0)
- if arrayMessage[5] == "2" {
- self.listRemoteViewFix[i].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi)/2)
- } else {
- self.listRemoteViewFix[i].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (-CGFloat.pi)/2)
- }
- let pictureImage = self.dataPerson[self.dataPerson.count - 1]["picture"] ?? ""
- let namePerson = self.dataPerson[self.dataPerson.count - 1]["name"] ?? ""
- if (!pictureImage!.isEmpty) {
- self.listRemoteViewFix[i].setImage(name: pictureImage!)
- self.listRemoteViewFix[i].contentMode = .scaleAspectFill
- } else {
- self.listRemoteViewFix[i].image = UIImage(systemName: "person")
- self.listRemoteViewFix[i].backgroundColor = UIColor.systemGray6
- self.listRemoteViewFix[i].contentMode = .scaleAspectFit
- }
- self.scrollRemoteView.contentSize.height = CGFloat(170 * (i + 1))
- let labelName = UILabel()
- self.containerLabelName[i].addSubview(labelName)
- labelName.anchor(left: self.containerLabelName[i].leftAnchor, right: self.containerLabelName[i].rightAnchor, paddingLeft: 5, paddingRight: 5, centerX: self.containerLabelName[i].centerXAnchor, centerY: self.containerLabelName[i].centerYAnchor)
- labelName.text = namePerson
- labelName.textAlignment = .center
- labelName.textColor = .white
- }
- }
-
- if let user = User.getData(pin: String(arrayMessage[1])) {
- if !self.users.contains(user) {
- user.isConnected = true
- self.users.append(user)
- } else if let userEx = self.users.firstIndex(where: { $0.pin == String(arrayMessage[1]) }) {
- self.users[userEx].isConnected = true
- }
- }
- if arrayMessage[5] == "2" && self.dataPerson.count == 1 {
- DispatchQueue.main.async {
- self.zoomView.transform = CGAffineTransform.init(scaleX: -1.9, y: 2.0).rotated(by: (CGFloat.pi)/2)
- self.zoomView.contentMode = .scaleAspectFit
- }
- }
- else if self.dataPerson.count == 1 {
- DispatchQueue.main.async {
- self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 2.0).rotated(by: (-CGFloat.pi)/2)
- self.zoomView.contentMode = .scaleAspectFit
- }
- } else if self.dataPerson.count > 1 {
- DispatchQueue.main.async {
- for i in 0..<self.dataPerson.count {
- self.listRemoteViewFix[i].image = nil
- if self.dataPerson[i]["user_type"] == "2" || arrayMessage[5] == "2" {
- self.listRemoteViewFix[i].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi)/2)
- } else {
- self.listRemoteViewFix[i].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (-CGFloat.pi)/2)
- }
- }
- }
- }
- }
- DispatchQueue.main.async {
- if self.name.isDescendant(of: self.view) {
- self.didTapAcceptCallButton()
- if QmeraVideoViewController.bSpeakerPhone {
- DispatchQueue.main.async {
- QmeraVideoViewController.bSpeakerPhone = false
- self.setSpeaker()
- }
- }
- }
- let indexPerson = self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[1]})
- if indexPerson != nil {
- self.dataPerson[indexPerson!]["user_type"] = String(arrayMessage[5])
- }
- }
- } else if (state == Nexilis.VIDEO_CALL_END || state == Nexilis.AUDIO_CALL_END) {
- DispatchQueue.main.async {
- Nexilis.stopRingtoneCall()
- Nexilis.stopRingbacktoneCall()
- }
- let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
- if !onGoingCC.isEmpty {
- let requester = onGoingCC.components(separatedBy: ",")[0]
- let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
- if arrayMessage[0] == requester || arrayMessage[0] == officer {
- DispatchQueue.main.async {
- let controller = self.presentedViewController
- if controller != nil {
- controller!.dismiss(animated: true)
- }
- if !self.showNotifCCEnd{
- let imageView = UIImageView(image: UIImage(systemName: "info.circle"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Call Center Session has ended".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .info, colors: nil, iconPosition: .center)
- banner.show()
- self.showNotifCCEnd = true
- }
- if self.stackViewToolbar.isDescendant(of: self.view){
- self.stackViewToolbar.removeFromSuperview()
- }
- if self.stackViewToolbar2.isDescendant(of: self.view){
- self.stackViewToolbar2.removeFromSuperview()
- }
- if self.buttonWB.isDescendant(of: self.view){
- self.buttonWB.removeFromSuperview()
- }
- if self.buttonChat.isDescendant(of: self.view){
- self.buttonChat.removeFromSuperview()
- }
- if self.buttonDecline.isDescendant(of: self.view) {
- self.buttonDecline.removeFromSuperview()
- }
- if self.buttonAccept.isDescendant(of: self.view) {
- self.buttonAccept.removeFromSuperview()
- }
- if self.buttonRotate.isDescendant(of: self.view) {
- self.buttonRotate.removeFromSuperview()
- }
- if self.buttonMuted.isDescendant(of: self.view) {
- self.buttonMuted.removeFromSuperview()
- }
- if self.wbVC != nil{
- self.wbVC!.close?()
- }
- self.wbTimer.invalidate()
- _ = Nexilis.getWhiteboardDelegate()?.terminate()
- }
- QmeraVideoViewController.isLoop = false
- QmeraVideoViewController.bSpeakerPhone = false
- do { try AVAudioSession.sharedInstance().setActive(false) } catch {}
- Nexilis.callAPNActivated = false
- DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
- self.endAllCall()
- self.dismiss(animated: true, completion: nil)
- }
- return
- }
- }
- DispatchQueue.main.async {
- if (self.dataPerson.count == 1) {
- self.makeStateCall()
- // if self.labelIncomingOutgoing.isDescendant(of: self.view) {
- // self.labelIncomingOutgoing.text = "Video call is over".localized()
- // }
- if self.stackViewToolbar.isDescendant(of: self.view){
- self.stackViewToolbar.removeFromSuperview()
- }
- if self.stackViewToolbar2.isDescendant(of: self.view){
- self.stackViewToolbar2.removeFromSuperview()
- }
- if self.buttonWB.isDescendant(of: self.view){
- self.buttonWB.removeFromSuperview()
- }
- if self.buttonChat.isDescendant(of: self.view){
- self.buttonChat.removeFromSuperview()
- }
- if self.buttonDecline.isDescendant(of: self.view) {
- self.buttonDecline.removeFromSuperview()
- }
- if self.buttonAccept.isDescendant(of: self.view) {
- self.buttonAccept.removeFromSuperview()
- }
- if self.buttonRotate.isDescendant(of: self.view) {
- self.buttonRotate.removeFromSuperview()
- }
- if self.buttonMuted.isDescendant(of: self.view) {
- self.buttonMuted.removeFromSuperview()
- }
- if self.wbVC != nil{
- self.wbVC!.close?()
- }
- self.wbTimer.invalidate()
- self.vcTimer.invalidate()
- // self.labelTimerVC.text = "Video call is over".localized()
- _ = Nexilis.getWhiteboardDelegate()?.terminate()
- let controller = self.presentedViewController
- if controller != nil {
- controller!.dismiss(animated: true)
- }
- QmeraVideoViewController.isLoop = false
- QmeraVideoViewController.bSpeakerPhone = false
- do { try AVAudioSession.sharedInstance().setActive(false) } catch {}
- Nexilis.callAPNActivated = false
- DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
- self.endAllCall()
- if self.isInisiator && !self.isPresent {
- self.navigationController?.popViewController(animated: true)
- } else {
- self.dismiss(animated: true, completion: nil)
- }
- }
- } else {
- let indexPerson = self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[0]})
- if indexPerson != nil {
- if (self.dataPerson.count == 2) {
- self.containerLabelName.forEach({ $0.subviews.forEach({ $0.removeFromSuperview() }) })
- self.scrollRemoteView.subviews.forEach({ $0.removeFromSuperview() })
- if !self.buttonWB.isEnabled {
- self.buttonWB.isEnabled = true
- }
- if !self.buttonRotate.isEnabled {
- self.buttonRotate.isEnabled = true
- }
- } else {
- self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
- self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
- self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
- self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
- if indexPerson! + 1 <= self.listRemoteViewFix.count {
- let iLoop = (self.listRemoteViewFix.count - 1) - (indexPerson! + 1)
- if iLoop >= 0 {
- for i in 0...iLoop {
- let viewAfterRemote = self.listRemoteViewFix[(indexPerson! + i) + 1]
- let viewAfterName = self.containerLabelName[(indexPerson! + i) + 1]
- viewAfterRemote.frame.origin.y = viewAfterRemote.frame.origin.y - 170
- viewAfterName.frame.origin.y = viewAfterName.frame.origin.y - 170
- UIView.animate(withDuration: 0.35, animations: {
- self.scrollRemoteView.layoutIfNeeded()
- })
- }
- }
- }
- }
- self.dataPerson.remove(at: indexPerson!)
- }
- let pin = "\(arrayMessage[0])"
- if let index = self.users.firstIndex(where: { $0.pin == pin }) {
- self.users.remove(at: index)
- }
-
- if self.dataPerson.count == 1 {
- self.transformZoomAfterNewUserMore2 = false
- self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 2.0).rotated(by: (CGFloat.pi)/2)
-
- if !self.users[0].isConnected {
- self.resetViewToOutgoing()
- }
- }
- }
- }
- } else if (state == Nexilis.OFFLINE) {
- DispatchQueue.main.async {
- Nexilis.stopRingtoneCall()
- Nexilis.stopRingbacktoneCall()
- }
- let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
- DispatchQueue.main.async {
- if (self.dataPerson.count == 1) {
- if self.labelIncomingOutgoing.isDescendant(of: self.view) {
- self.labelIncomingOutgoing.text = "Offline".localized()
- }
- if self.buttonDecline.isDescendant(of: self.view) {
- self.buttonDecline.removeFromSuperview()
- }
- if self.buttonAccept.isDescendant(of: self.view) {
- self.buttonAccept.removeFromSuperview()
- }
- } else {
- let indexPerson = self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[0]})
- if indexPerson != nil {
- if (self.dataPerson.count == 2) {
- self.containerLabelName.forEach({ $0.subviews.forEach({ $0.removeFromSuperview() }) })
- self.scrollRemoteView.subviews.forEach({ $0.removeFromSuperview() })
- if !self.buttonWB.isEnabled {
- self.buttonWB.isEnabled = true
- }
- if !self.buttonRotate.isEnabled {
- self.buttonRotate.isEnabled = true
- }
- } else {
- self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
- self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
- self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
- self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
- if indexPerson! + 1 <= self.listRemoteViewFix.count {
- let viewAfterRemote = self.listRemoteViewFix[indexPerson! + 1]
- let viewAfterName = self.containerLabelName[indexPerson! + 1]
- viewAfterRemote.frame.origin.y = viewAfterRemote.frame.origin.y - 170
- viewAfterName.frame.origin.y = viewAfterName.frame.origin.y - 170
- UIView.animate(withDuration: 0.35, animations: {
- self.scrollRemoteView.layoutIfNeeded()
- })
- }
- }
- }
- if !onGoingCC.isEmpty {
- if let pin = arrayMessage.first, let index = self.users.firstIndex(of: User(pin: String(pin))) {
- self.users.remove(at: index)
- if !onGoingCC.isEmpty && self.users.count != 0 {
- DispatchQueue.main.async {
- var members = ""
- for user in self.users {
- if members.isEmpty {
- members = "\(user.pin)"
- } else {
- members = ",\(user.pin)"
- }
- }
- SecureUserDefaults.shared.set("\(members)", forKey: "membersCC")
- }
- }
- }
- }
- self.dataPerson.remove(at: indexPerson!)
- }
- }
- if (self.dataPerson.count == 1) {
- DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
- self.endAllCall()
- if self.isInisiator && onGoingCC.isEmpty && !self.isPresent {
- self.navigationController?.popViewController(animated: true)
- } else {
- self.dismiss(animated: true, completion: nil)
- }
- }
- }
- } else if (state == Nexilis.BUSY) {
- DispatchQueue.main.async {
- Nexilis.stopRingtoneCall()
- Nexilis.stopRingbacktoneCall()
- }
- let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
- DispatchQueue.main.async { [self] in
- if (self.dataPerson.count == 1) {
- self.makeStateCall()
- if self.labelIncomingOutgoing.isDescendant(of: self.view) {
- self.labelIncomingOutgoing.text = "Busy".localized()
- }
- if self.buttonDecline.isDescendant(of: self.view) {
- self.buttonDecline.removeFromSuperview()
- }
- if self.buttonAccept.isDescendant(of: self.view) {
- self.buttonAccept.removeFromSuperview()
- }
- } else {
- let indexPerson = self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[0]})
- if indexPerson != nil {
- if (self.dataPerson.count == 2) {
- self.containerLabelName.forEach({ $0.subviews.forEach({ $0.removeFromSuperview() }) })
- self.scrollRemoteView.subviews.forEach({ $0.removeFromSuperview() })
- if !self.buttonWB.isEnabled {
- self.buttonWB.isEnabled = true
- }
- if !self.buttonRotate.isEnabled {
- self.buttonRotate.isEnabled = true
- }
- } else {
- self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
- self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
- self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
- self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
- if indexPerson! + 1 <= self.listRemoteViewFix.count {
- let viewAfterRemote = self.listRemoteViewFix[indexPerson! + 1]
- let viewAfterName = self.containerLabelName[indexPerson! + 1]
- viewAfterRemote.frame.origin.y = viewAfterRemote.frame.origin.y - 170
- viewAfterName.frame.origin.y = viewAfterName.frame.origin.y - 170
- UIView.animate(withDuration: 0.35, animations: {
- self.scrollRemoteView.layoutIfNeeded()
- })
- }
- }
- }
- if !onGoingCC.isEmpty {
- if let pin = arrayMessage.first, let index = self.users.firstIndex(of: User(pin: String(pin))) {
- self.users.remove(at: index)
- if !onGoingCC.isEmpty && users.count != 0 {
- DispatchQueue.main.async {
- var members = ""
- for user in self.users {
- if members.isEmpty {
- members = "\(user.pin)"
- } else {
- members = ",\(user.pin)"
- }
- }
- SecureUserDefaults.shared.set("\(members)", forKey: "membersCC")
- }
- }
- }
- }
- if let indexP = indexPerson {
- self.dataPerson.remove(at: indexP)
- }
- }
- }
- if (self.dataPerson.count == 1) {
- DispatchQueue.main.async {
- Nexilis.playBusyCall()
- }
- DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
- Nexilis.stopBusyCall()
- self.endAllCall()
- if self.isInisiator && onGoingCC.isEmpty && !self.isPresent {
- self.navigationController?.popViewController(animated: true)
- } else {
- self.dismiss(animated: true, completion: nil)
- }
- }
- }
- } else if (state == Nexilis.VIDEO_CALL_RINGING) {
- DispatchQueue.main.async {
- if (self.dataPerson.count > 1) {
- if (self.dataPerson.count == 2) {
- for i in 0...1{
- self.scrollRemoteView.addSubview(self.listRemoteViewFix[i])
- self.listRemoteViewFix[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 160)
- self.listRemoteViewFix[i].backgroundColor = .white
- self.listRemoteViewFix[i].makeRoundedView(radius: 8.0)
- self.scrollRemoteView.addSubview(self.containerLabelName[i])
- self.containerLabelName[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 30)
- self.containerLabelName[i].backgroundColor = .orangeBNI.withAlphaComponent(0.5)
- self.containerLabelName[i].makeRoundedView(radius: 8.0)
- let pictureImage = self.dataPerson[i]["picture"] ?? ""
- let namePerson = self.dataPerson[i]["name"] ?? ""
- if (!pictureImage!.isEmpty) {
- self.listRemoteViewFix[i].setImage(name: pictureImage!)
- self.listRemoteViewFix[i].contentMode = .scaleAspectFill
- } else {
- self.listRemoteViewFix[i].image = UIImage(systemName: "person")
- self.listRemoteViewFix[i].backgroundColor = UIColor.systemGray6
- self.listRemoteViewFix[i].contentMode = .scaleAspectFit
- }
- let labelName = UILabel()
- self.containerLabelName[i].addSubview(labelName)
- labelName.anchor(left: self.containerLabelName[i].leftAnchor, right: self.containerLabelName[i].rightAnchor, paddingLeft: 5, paddingRight: 5, centerX: self.containerLabelName[i].centerXAnchor, centerY: self.containerLabelName[i].centerYAnchor)
- labelName.text = namePerson
- labelName.textAlignment = .center
- labelName.textColor = .white
- }
- self.scrollRemoteView.contentSize.height = CGFloat(170 * 2)
- } else {
- let i = self.dataPerson.count - 1
- self.scrollRemoteView.addSubview(self.listRemoteViewFix[i])
- self.listRemoteViewFix[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 160)
- self.listRemoteViewFix[i].backgroundColor = .white
- self.listRemoteViewFix[i].makeRoundedView(radius: 8.0)
- self.scrollRemoteView.addSubview(self.containerLabelName[i])
- self.containerLabelName[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 30)
- self.containerLabelName[i].backgroundColor = .orangeBNI.withAlphaComponent(0.5)
- self.containerLabelName[i].makeRoundedView(radius: 8.0)
- let pictureImage = self.dataPerson[self.dataPerson.count - 1]["picture"] ?? ""
- let namePerson = self.dataPerson[self.dataPerson.count - 1]["name"] ?? ""
- if (!pictureImage!.isEmpty) {
- self.listRemoteViewFix[i].setImage(name: pictureImage!)
- self.listRemoteViewFix[i].contentMode = .scaleAspectFill
- } else {
- self.listRemoteViewFix[i].image = UIImage(systemName: "person")
- self.listRemoteViewFix[i].backgroundColor = UIColor.systemGray6
- self.listRemoteViewFix[i].contentMode = .scaleAspectFit
- }
- self.scrollRemoteView.contentSize.height = CGFloat(170 * (i + 1))
- let labelName = UILabel()
- self.containerLabelName[i].addSubview(labelName)
- labelName.anchor(left: self.containerLabelName[i].leftAnchor, right: self.containerLabelName[i].rightAnchor, paddingLeft: 5, paddingRight: 5, centerX: self.containerLabelName[i].centerXAnchor, centerY: self.containerLabelName[i].centerYAnchor)
- labelName.text = namePerson
- labelName.textAlignment = .center
- labelName.textColor = .white
- }
- if self.buttonWB.isEnabled {
- self.buttonWB.isEnabled = false
- }
- if self.buttonRotate.isEnabled {
- self.buttonRotate.isEnabled = false
- }
- } else {
- if self.dataPerson.count == 1 && !self.autoAcceptAPN {
- DispatchQueue.main.async {
- self.labelIncomingOutgoing.text = "Waiting for answer".localized()
- }
- }
- }
- }
- }
- }
- }
- extension QmeraVideoViewController : WhiteboardReceiver {
-
- func incomingWB(roomId: String) {
- //print("incoming wb")
- self.wbTimer.invalidate()
- if(wbRoomId.isEmpty){
- //print("wbroom empty")
- DispatchQueue.main.async {
- self.wbTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.runTimer), userInfo: nil, repeats: true)
- }
- let me = User.getMyPin()!
- var destString = ""
- for d in dataPerson{
- if d["f_pin"]!! == roomId.components(separatedBy: "wbvc")[0] {
- continue
- }
- if destString.isEmpty{
- destString = d["f_pin"]!!
- } else {
- destString = destString + ",\(d["f_pin"]!!)"
- }
- }
- if destString.isEmpty {
- SecureUserDefaults.shared.set("\(roomId.components(separatedBy: "wbvc")[0]),\(me)", forKey: "wb_vc")
- } else {
- SecureUserDefaults.shared.set("\(roomId.components(separatedBy: "wbvc")[0]),\(me),\(destString)", forKey: "wb_vc")
- }
- wbRoomId = roomId
- }
- }
-
- func cancel(roomId: String) {
- DispatchQueue.main.async {
- self.wbTimer.invalidate()
- self.wbBlink = false
- self.buttonWB.backgroundColor = .lightGray
- self.buttonWB.setNeedsDisplay()
- }
- wbRoomId = ""
- }
-
- @objc func runTimer(){
- DispatchQueue.main.async {
- if !self.buttonWB.isEnabled {
- return
- }
- self.wbBlink = !self.wbBlink
- if(self.wbBlink){
- self.buttonWB.backgroundColor = .green
- }
- else {
- //print("set wb blink off")
- self.buttonWB.backgroundColor = .lightGray
- }
- self.buttonWB.setNeedsDisplay()
- }
- }
-
- }
|