QmeraVideoViewController.swift 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926
  1. //
  2. // VideoViewControllerQmera.swift
  3. // Qmera
  4. //
  5. // Created by Akhmad Al Qindi Irsyam on 07/10/21.
  6. //
  7. // Rn
  8. // (Kedip whiteboard)
  9. // extension Whiteboard
  10. // wbVC!.close wbTimer wbBlink
  11. import UIKit
  12. import nuSDKService
  13. import AVFoundation
  14. import NotificationBannerSwift
  15. import MediaPlayer
  16. class QmeraVideoViewController: UIViewController {
  17. static private var nMaxSPOn: Float! = 20.0
  18. static private var nMaxSPOff: Float! = 20.0
  19. static private var volumeView: MPVolumeView!
  20. static private var lastVolume: Float! = AVAudioSession.sharedInstance().outputVolume
  21. static private var bSpeakerPhone: Bool! = false
  22. private var tempSpeaker = false
  23. private var timerSpeaker: Timer?
  24. static private var isLoop = false
  25. var dataPerson: [[String: String?]] = []
  26. var fPin = ""
  27. var wbRoomId = ""
  28. var isInisiator = true
  29. // var isSpeaker = false
  30. var isMuted = false
  31. var isPresent = false
  32. var callFCM = true
  33. var isNavigationHidden = false
  34. var listRemoteViewFix: [UIImageView] = [
  35. UIImageView(),
  36. UIImageView(),
  37. UIImageView(),
  38. UIImageView(),
  39. UIImageView()
  40. ]
  41. var containerLabelName: [UIView] = [
  42. UIView(),
  43. UIView(),
  44. UIView(),
  45. UIView(),
  46. UIView()
  47. ]
  48. var imageMuted: [UIImageView] = [
  49. UIImageView(),
  50. UIImageView(),
  51. UIImageView(),
  52. UIImageView(),
  53. UIImageView()
  54. ]
  55. var mutedZoom: UIImageView = {
  56. let image = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 40))
  57. image.contentMode = .scaleAspectFit
  58. image.image = UIImage(systemName: "mic.slash")
  59. image.tintColor = .red
  60. image.isHidden = true
  61. return image
  62. }()
  63. let myImage = UIImageView()
  64. let name = UILabel()
  65. let profileImage = UIImageView()
  66. let labelIncomingOutgoing = UILabel()
  67. let buttonDecline = UIButton()
  68. let buttonAccept = UIButton()
  69. let zoomView = UIImageView()
  70. let cameraView = UIImageView()
  71. var constraintLeadingButtonDecline = NSLayoutConstraint()
  72. var constraintBottomButtonDecline = NSLayoutConstraint()
  73. var constraintBottomStackViewToolbar = NSLayoutConstraint()
  74. var constraintLeftStackViewToolbar2 = NSLayoutConstraint()
  75. let stackViewToolbar = UIStackView()
  76. let stackViewToolbar2 = UIStackView()
  77. let stackViewToolbar3 = UIStackView()
  78. var onScreenConstraintWB = [NSLayoutConstraint]()
  79. let buttonWB = UIButton()
  80. let buttonChat = UIButton()
  81. var wbVC : WhiteboardViewController?
  82. let buttonAddParticipant = UIButton()
  83. let buttonSpeaker = UIButton()
  84. let buttonRotate = UIButton()
  85. let buttonMuted = UIButton()
  86. var showStackViewToolbar = true
  87. let scrollRemoteView = UIScrollView()
  88. var isAutoAccept = false
  89. var wbTimer = Timer()
  90. var wbBlink = false
  91. var showNotifCCEnd = false
  92. var transformZoomAfterNewUserMore2 = false
  93. var isAddCall = ""
  94. var ticketId = ""
  95. var autoAcceptAPN = false
  96. var timeStartCall = ""
  97. var idCall = ""
  98. private var frontCamera = true
  99. var users: [User] = []
  100. let poweredByView: UIStackView = {
  101. let stackView = UIStackView()
  102. stackView.axis = .horizontal
  103. stackView.spacing = 5
  104. return stackView
  105. }()
  106. private var vcTimer = Timer()
  107. private var containerTimerVC = UIView()
  108. private var labelTimerVC = UILabel()
  109. let poweredByLabel: UILabel = {
  110. let label = UILabel()
  111. label.text = "Powered by Nexilis".localized()
  112. return label
  113. }()
  114. let qmeraLogo: UIButton = {
  115. let image = UIImage(named: "Q-Button-PNG", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  116. let button = UIButton()
  117. button.setImage(image, for: .normal)
  118. button.imageView?.contentMode = .scaleAspectFit
  119. button.imageEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
  120. button.contentVerticalAlignment = .fill
  121. button.contentHorizontalAlignment = .fill
  122. // button.frame.size.width = 30
  123. // button.frame.size.height = 30
  124. return button
  125. }()
  126. let nexilisLogo: UIButton = {
  127. let image = UIImage(named: "pb_powered_button", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  128. let button = UIButton()
  129. button.setImage(image, for: .normal)
  130. button.imageView?.contentMode = .scaleAspectFit
  131. button.imageEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
  132. button.contentVerticalAlignment = .fill
  133. button.contentHorizontalAlignment = .fill
  134. // button.frame.size.width = 30
  135. // button.frame.size.height = 30
  136. return button
  137. }()
  138. static func turnSpeakerOn() {
  139. do {
  140. let audioSession = AVAudioSession.sharedInstance()
  141. try audioSession.overrideOutputAudioPort(bSpeakerPhone ? .speaker : .none)
  142. } catch {
  143. }
  144. // var bAudioEngineIsAvtive: Bool! = false
  145. // API.turnSpeakerPhone(bSPon: bSpeakerPhone)
  146. // repeat {
  147. // Thread.sleep(forTimeInterval : 0.3)
  148. // bAudioEngineIsAvtive = API.bAudioEngineIsRunning()
  149. // print("Audio Session State: \(bAudioEngineIsAvtive ? "Active" : "Inactive" )")
  150. // if (bAudioEngineIsAvtive) {
  151. // break
  152. // }
  153. // API.restartAudioEngine()
  154. // } while (!bAudioEngineIsAvtive)
  155. // var volume:Float! = 0
  156. // if (bSpeakerPhone) {
  157. // volume = lastVolume * nMaxSPOn
  158. // } else {
  159. // volume = lastVolume * nMaxSPOff
  160. // }
  161. // API.adjustVolume(fValue: volume)
  162. }
  163. // static func toggleSpeakerPhone() {
  164. // bSpeakerPhone = !bSpeakerPhone
  165. // var volume:Float! = 0
  166. // if (bSpeakerPhone) {
  167. // volume = lastVolume * nMaxSPOn
  168. // } else {
  169. // volume = lastVolume * nMaxSPOff
  170. // }
  171. // API.adjustVolume(fValue: volume)
  172. // }
  173. deinit {
  174. navigationController?.changeAppearance(clear: false)
  175. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  176. navigationController?.navigationBar.titleTextAttributes = textAttributes
  177. navigationController?.navigationBar.topItem?.backBarButtonItem = nil
  178. navigationController?.interactivePopGestureRecognizer?.isEnabled = true
  179. NotificationCenter.default.removeObserver(self)
  180. Nexilis.floatingButton.isHidden = false
  181. }
  182. override func viewWillDisappear(_ animated: Bool) {
  183. if self.isMovingFromParent {
  184. navigationController?.changeAppearance(clear: false)
  185. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  186. navigationController?.navigationBar.titleTextAttributes = textAttributes
  187. navigationController?.navigationBar.topItem?.backBarButtonItem = nil
  188. navigationController?.interactivePopGestureRecognizer?.isEnabled = true
  189. NotificationCenter.default.removeObserver(self)
  190. }
  191. Nexilis.floatingButton.isHidden = false
  192. }
  193. private func backToDefaultAudioSession() {
  194. do {
  195. let audioSession = AVAudioSession.sharedInstance()
  196. try audioSession.setCategory(.playAndRecord, mode: .voiceChat, options: [.allowBluetooth, .mixWithOthers])
  197. try audioSession.overrideOutputAudioPort(.speaker)
  198. try audioSession.setPreferredSampleRate(48000)
  199. try audioSession.setActive(true)
  200. } catch {
  201. }
  202. }
  203. override func viewDidLoad() {
  204. super.viewDidLoad()
  205. QmeraVideoViewController.volumeView = MPVolumeView(frame: .zero)
  206. QmeraVideoViewController.volumeView.isHidden = true
  207. Nexilis.setWhiteboardReceiver(receiver: self)
  208. Nexilis.floatingButton.isHidden = true
  209. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
  210. navigationController?.changeAppearance(clear: true)
  211. NotificationCenter.default.addObserver(self, selector: #selector(self.onStatusCall(_:)), name: NSNotification.Name(rawValue: Nexilis.listenerStatusCall), object: nil)
  212. NotificationCenter.default.addObserver(self, selector: #selector(onReceiveMessage(notification:)), name: NSNotification.Name(rawValue: Nexilis.listenerReceiveChat), object: nil)
  213. NotificationCenter.default.addObserver(self, selector: #selector(onCallFCM(notification:)), name: NSNotification.Name(rawValue: Nexilis.callFCM), object: nil)
  214. view.backgroundColor = .clear
  215. navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
  216. navigationController?.interactivePopGestureRecognizer?.isEnabled = false
  217. navigationItem.setHidesBackButton(true, animated: false)
  218. if fPin != ""{
  219. getDataProfile(fPin: fPin)
  220. }
  221. addZoomView()
  222. addCameraView()
  223. addListRemoteView()
  224. addBackgroundIncoming()
  225. addProfileNameCalling()
  226. Calling()
  227. addToolbar()
  228. addTimerVC()
  229. addImageMutedOnZoom()
  230. if isAutoAccept {
  231. didTapAcceptCallButton()
  232. }
  233. if autoAcceptAPN {
  234. DispatchQueue.global().async {
  235. do {
  236. if API.nGetCLXConnState() == 0 {
  237. let id = Utils.getConnectionID()
  238. try API.initConnection(sAPIK: Nexilis.sAPIKey, cbiI: Callback(), sTCPAddr: Nexilis.ADDRESS, nTCPPort: Nexilis.PORT, sUserID: id, sStartWH: "09:00")
  239. }
  240. } catch {
  241. }
  242. self.backToDefaultAudioSession()
  243. while API.nGetCLXConnState() == 0 {
  244. Thread.sleep(forTimeInterval : 0.3)
  245. }
  246. _ = Nexilis.writeSync(message: CoreMessage_TMessageBank.getNotifyCalling(fPin: self.fPin, lPin: User.getMyPin()!, type: "2"))
  247. }
  248. }
  249. self.timeStartCall = String(Date().currentTimeMillis())
  250. self.idCall = (User.getMyPin() ?? "") + CoreMessage_TMessageUtil.getTID()
  251. }
  252. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  253. if (keyPath! == "outputVolume") {
  254. if let newKey = change?[NSKeyValueChangeKey.newKey] as? NSNumber {
  255. QmeraVideoViewController.lastVolume = newKey.floatValue
  256. if (QmeraVideoViewController.bSpeakerPhone) {
  257. let volume = QmeraVideoViewController.lastVolume * QmeraVideoViewController.nMaxSPOn
  258. API.adjustVolume(fValue: volume)
  259. } else {
  260. let volume = QmeraVideoViewController.lastVolume * QmeraVideoViewController.nMaxSPOff
  261. API.adjustVolume(fValue: volume)
  262. }
  263. }
  264. }
  265. }
  266. func getDataProfile(fPin: String) {
  267. 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)'"
  268. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  269. do {
  270. if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
  271. var row: [String: String?] = [:]
  272. if cursorData.next() {
  273. row["f_pin"] = cursorData.string(forColumnIndex: 0)
  274. var name = ""
  275. if let firstname = cursorData.string(forColumnIndex: 1) {
  276. name = firstname
  277. }
  278. if let lastname = cursorData.string(forColumnIndex: 2) {
  279. name = name + " " + lastname
  280. }
  281. row["name"] = name
  282. row["picture"] = cursorData.string(forColumnIndex: 4)
  283. row["isOfficial"] = cursorData.string(forColumnIndex: 3)
  284. row["deviceId"] = cursorData.string(forColumnIndex: 5)
  285. row["isOffline"] = cursorData.string(forColumnIndex: 6)
  286. row["user_type"] = cursorData.string(forColumnIndex: 7)
  287. if fPin != User.getMyPin() {
  288. dataPerson.append(row)
  289. }
  290. } else {
  291. var row: [String: String?] = [:]
  292. row["f_pin"] = fPin
  293. row["name"] = "User".localized()
  294. row["picture"] = ""
  295. row["isOfficial"] = ""
  296. row["deviceId"] = ""
  297. row["isOffline"] = ""
  298. row["user_type"] = ""
  299. dataPerson.append(row)
  300. }
  301. cursorData.close()
  302. } else {
  303. var row: [String: String?] = [:]
  304. row["f_pin"] = fPin
  305. row["name"] = "User".localized()
  306. row["picture"] = ""
  307. row["isOfficial"] = ""
  308. row["deviceId"] = ""
  309. row["isOffline"] = ""
  310. row["user_type"] = ""
  311. dataPerson.append(row)
  312. // if let response = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getAddFriendQRCode(fpin: fPin)), response.isOk() {
  313. // self.getDataProfile(fPin: fPin)
  314. // }
  315. // Nexilis.addFriend (fpin: "\(fPin)") { result in
  316. // if result {
  317. // self.getDataProfile(fPin: fPin)
  318. // } else {
  319. // let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  320. // imageView.tintColor = .white
  321. // 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)
  322. // banner.show()
  323. // }
  324. // }
  325. }
  326. } catch {
  327. rollback.pointee = true
  328. print("Access database error: \(error.localizedDescription)")
  329. }
  330. })
  331. }
  332. func addTimerVC() {
  333. view.addSubview(containerTimerVC)
  334. containerTimerVC.anchor(top: view.safeAreaLayoutGuide.topAnchor, centerX: view.centerXAnchor, minWidth: 40)
  335. containerTimerVC.makeRoundedView(radius: 8)
  336. containerTimerVC.backgroundColor = .black.withAlphaComponent(0.3)
  337. containerTimerVC.addSubview(labelTimerVC)
  338. labelTimerVC.anchor(left: containerTimerVC.leftAnchor, right: containerTimerVC.rightAnchor, paddingLeft: 8, paddingRight: 8, centerX: containerTimerVC.centerXAnchor, centerY: containerTimerVC.centerYAnchor)
  339. labelTimerVC.textColor = .white
  340. containerTimerVC.isHidden = true
  341. }
  342. func addImageMutedOnZoom() {
  343. view.addSubview(mutedZoom)
  344. mutedZoom.anchor(top: containerTimerVC.bottomAnchor, paddingTop: 10, centerX: view.centerXAnchor, width: 30, height: 40)
  345. }
  346. func addZoomView() {
  347. view.addSubview(zoomView)
  348. zoomView.translatesAutoresizingMaskIntoConstraints = false
  349. NSLayoutConstraint.activate([
  350. zoomView.topAnchor.constraint(equalTo: view.topAnchor),
  351. zoomView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  352. zoomView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  353. zoomView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  354. ])
  355. zoomView.backgroundColor = .secondaryColor
  356. zoomView.isUserInteractionEnabled = true
  357. zoomView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideToolbar)))
  358. }
  359. func addCameraView() {
  360. view.addSubview(cameraView)
  361. // cameraView.frame = CGRect(x: view.frame.width - 130, y: 20, width: 120, height: 160)
  362. cameraView.translatesAutoresizingMaskIntoConstraints = false
  363. NSLayoutConstraint.activate([
  364. cameraView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
  365. cameraView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10.0),
  366. cameraView.widthAnchor.constraint(equalToConstant: 120.0),
  367. cameraView.heightAnchor.constraint(equalToConstant: 160.0)
  368. ])
  369. cameraView.backgroundColor = .secondaryColor
  370. cameraView.makeRoundedView(radius: 8)
  371. }
  372. func addListRemoteView() {
  373. view.addSubview(scrollRemoteView)
  374. scrollRemoteView.translatesAutoresizingMaskIntoConstraints = false
  375. NSLayoutConstraint.activate([
  376. scrollRemoteView.topAnchor.constraint(equalTo: cameraView.bottomAnchor, constant: 10),
  377. scrollRemoteView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10),
  378. scrollRemoteView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
  379. scrollRemoteView.widthAnchor.constraint(equalToConstant: 120.0)
  380. ])
  381. scrollRemoteView.showsHorizontalScrollIndicator = false
  382. scrollRemoteView.showsVerticalScrollIndicator = false
  383. scrollRemoteView.contentSize.width = 120.0
  384. scrollRemoteView.backgroundColor = .clear
  385. }
  386. func addBackgroundIncoming() {
  387. view.addSubview(myImage)
  388. myImage.translatesAutoresizingMaskIntoConstraints = false
  389. NSLayoutConstraint.activate([
  390. myImage.topAnchor.constraint(equalTo: view.topAnchor),
  391. myImage.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  392. myImage.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  393. myImage.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  394. ])
  395. myImage.backgroundColor = .lightGray
  396. myImage.tintColor = .secondaryColor
  397. let image = dataPerson[0]["picture"]!!
  398. if image.isEmpty {
  399. myImage.image = UIImage(systemName: "person")
  400. myImage.contentMode = .scaleAspectFit
  401. } else {
  402. myImage.setImage(name: image)
  403. myImage.contentMode = .scaleAspectFill
  404. }
  405. // let idMe = User.getMyPin() as String?
  406. // Database().database?.inTransaction({ fmdb, rollback in
  407. // if let c = Database().getRecords(fmdb: fmdb, query: "select image_id from BUDDY where f_pin = '\(idMe!)'"), c.next() {
  408. // let image = c.string(forColumnIndex: 0)!
  409. // if image.isEmpty {
  410. // myImage.image = UIImage(systemName: "person")
  411. // myImage.contentMode = .scaleAspectFit
  412. // } else {
  413. // myImage.setImage(name: image)
  414. // myImage.contentMode = .scaleAspectFill
  415. // }
  416. // c.close()
  417. // }
  418. // })
  419. }
  420. func addProfileNameCalling() {
  421. view.addSubview(profileImage)
  422. profileImage.translatesAutoresizingMaskIntoConstraints = false
  423. profileImage.frame.size = CGSize(width: 60.0, height: 60.0)
  424. NSLayoutConstraint.activate([
  425. profileImage.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
  426. profileImage.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  427. profileImage.widthAnchor.constraint(equalToConstant: 60.0),
  428. profileImage.heightAnchor.constraint(equalToConstant: 63.0)
  429. ])
  430. profileImage.backgroundColor = .lightGray
  431. profileImage.tintColor = .secondaryColor
  432. profileImage.circle()
  433. let image = dataPerson[0]["picture"]!!
  434. if image.isEmpty {
  435. profileImage.image = UIImage(systemName: "person")
  436. profileImage.contentMode = .scaleAspectFit
  437. profileImage.layer.borderWidth = 1
  438. profileImage.layer.borderColor = UIColor.secondaryColor.cgColor
  439. } else {
  440. profileImage.setImage(name: image)
  441. profileImage.contentMode = .scaleAspectFill
  442. }
  443. view.addSubview(name)
  444. name.translatesAutoresizingMaskIntoConstraints = false
  445. NSLayoutConstraint.activate([
  446. name.topAnchor.constraint(equalTo: profileImage.bottomAnchor, constant: 5.0),
  447. name.centerXAnchor.constraint(equalTo: view.centerXAnchor)
  448. ])
  449. name.font = UIFont.systemFont(ofSize: 12)
  450. name.backgroundColor = .black.withAlphaComponent(0.05)
  451. name.layer.cornerRadius = 5.0
  452. name.clipsToBounds = true
  453. name.textColor = .mainColor
  454. name.text = dataPerson[0]["name"]!?.trimmingCharacters(in: .whitespaces)
  455. }
  456. func Calling() {
  457. view.addSubview(labelIncomingOutgoing)
  458. labelIncomingOutgoing.translatesAutoresizingMaskIntoConstraints = false
  459. NSLayoutConstraint.activate([
  460. labelIncomingOutgoing.topAnchor.constraint(equalTo: name.bottomAnchor, constant: 40.0),
  461. labelIncomingOutgoing.centerXAnchor.constraint(equalTo: view.centerXAnchor)
  462. ])
  463. if isInisiator {
  464. labelIncomingOutgoing.text = "Connecting".localized()
  465. if ticketId.isEmpty {
  466. backToDefaultAudioSession()
  467. Nexilis.playRingbacktoneCall()
  468. if callFCM {
  469. DispatchQueue.global().async {
  470. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getCalling(fPin: self.dataPerson[0]["f_pin"]!!, type: "2"), timeout: 30 * 1000) {
  471. if response.isOk() {
  472. DispatchQueue.main.async {
  473. self.labelIncomingOutgoing.text = "Waiting for answer".localized()
  474. }
  475. } else if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "01" && self.dataPerson.count > 0 {
  476. API.initiateCCall(sParty: self.dataPerson[0]["f_pin"]!, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: self.listRemoteViewFix, ivLocalView: self.cameraView, ivRemoteZ: self.zoomView)
  477. } else {
  478. DispatchQueue.main.async {
  479. if self.labelIncomingOutgoing.isDescendant(of: self.view) {
  480. self.labelIncomingOutgoing.text = "Busy".localized()
  481. }
  482. if self.buttonDecline.isDescendant(of: self.view) {
  483. self.buttonDecline.removeFromSuperview()
  484. }
  485. if self.buttonAccept.isDescendant(of: self.view) {
  486. self.buttonAccept.removeFromSuperview()
  487. }
  488. }
  489. }
  490. } else {
  491. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  492. imageView.tintColor = .white
  493. 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)
  494. banner.show()
  495. }
  496. }
  497. } else {
  498. API.initiateCCall(sParty: dataPerson[0]["f_pin"]!, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView)
  499. }
  500. } else {
  501. backToDefaultAudioSession()
  502. API.ccs(sTicketID: ticketId, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView, bCameraOn: true)
  503. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getIncomingCallCS(f_pin_opposite: users[0].pin), timeout: 30 * 1000){
  504. if response.mBodies[CoreMessage_TMessageKey.ERRCOD] != "01" {
  505. endAllCall()
  506. }
  507. }
  508. }
  509. } else {
  510. if ticketId.isEmpty && !autoAcceptAPN {
  511. backToDefaultAudioSession()
  512. Nexilis.playRingtoneCall()
  513. }
  514. labelIncomingOutgoing.text = "Incoming video call".localized() + "..."
  515. }
  516. labelIncomingOutgoing.font = UIFont.systemFont(ofSize: 12)
  517. labelIncomingOutgoing.backgroundColor = .black.withAlphaComponent(0.05)
  518. labelIncomingOutgoing.layer.cornerRadius = 5.0
  519. labelIncomingOutgoing.clipsToBounds = true
  520. labelIncomingOutgoing.textColor = .mainColor
  521. }
  522. func addToolbar(resetToolbar: Bool = false) {
  523. view.addSubview(buttonDecline)
  524. buttonDecline.translatesAutoresizingMaskIntoConstraints = false
  525. buttonDecline.frame.size = CGSize(width: 70.0, height: 70.0)
  526. if isInisiator || resetToolbar {
  527. constraintLeadingButtonDecline = buttonDecline.centerXAnchor.constraint(equalTo: view.centerXAnchor)
  528. } else {
  529. constraintLeadingButtonDecline = buttonDecline.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: view.frame.width * 0.2)
  530. }
  531. constraintBottomButtonDecline = buttonDecline.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -60.0)
  532. NSLayoutConstraint.activate([
  533. constraintBottomButtonDecline,
  534. constraintLeadingButtonDecline,
  535. buttonDecline.widthAnchor.constraint(equalToConstant: 70.0),
  536. buttonDecline.heightAnchor.constraint(equalToConstant: 70.0)
  537. ])
  538. buttonDecline.backgroundColor = .red
  539. buttonDecline.circle()
  540. buttonDecline.setImage(UIImage(systemName: "xmark", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  541. buttonDecline.tintColor = .white
  542. buttonDecline.addTarget(self, action: #selector(didTapDeclineCallButton(sender:)), for: .touchUpInside)
  543. if !isInisiator && !resetToolbar {
  544. view.addSubview(buttonAccept)
  545. buttonAccept.translatesAutoresizingMaskIntoConstraints = false
  546. buttonAccept.frame.size = CGSize(width: 70.0, height: 70.0)
  547. NSLayoutConstraint.activate([
  548. buttonAccept.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -60.0),
  549. buttonAccept.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -(view.frame.width * 0.2)),
  550. buttonAccept.widthAnchor.constraint(equalToConstant: 70.0),
  551. buttonAccept.heightAnchor.constraint(equalToConstant: 70.0)
  552. ])
  553. buttonAccept.backgroundColor = .greenColor
  554. buttonAccept.circle()
  555. buttonAccept.setImage(UIImage(systemName: "checkmark", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  556. buttonAccept.tintColor = .white
  557. buttonAccept.addTarget(self, action: #selector(didTapAcceptCallButton), for: .touchUpInside)
  558. }
  559. }
  560. @objc func onCallFCM(notification: NSNotification) {
  561. DispatchQueue.main.async { [self] in
  562. let data:[AnyHashable : Any] = notification.userInfo!
  563. if let l_pin = data["l_pin"] as? String {
  564. if let f_pin = data["f_pin"] as? String {
  565. if f_pin == User.getMyPin()! {
  566. if !Nexilis.callAPNActivated {
  567. labelIncomingOutgoing.text = "Waiting for answer".localized()
  568. }
  569. API.initiateCCall(sParty: l_pin, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView)
  570. }
  571. }
  572. }
  573. }
  574. }
  575. @objc func onReceiveMessage(notification: NSNotification) {
  576. DispatchQueue.main.async {
  577. let data:[AnyHashable : Any] = notification.userInfo!
  578. if let dataMessage = data["message"] as? TMessage {
  579. if (dataMessage.getCode() == CoreMessage_TMessageCode.PUSH_MEMBER_ROOM_CONTACT_CENTER) {
  580. let data = dataMessage.getBody(key: CoreMessage_TMessageKey.DATA)
  581. if !data.isEmpty {
  582. if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions()) as? [AnyObject] {
  583. var members = ""
  584. let idMe = User.getMyPin()!
  585. for json in jsonArray {
  586. if "\(json)" != idMe {
  587. if members.isEmpty {
  588. members = "\(json)"
  589. } else {
  590. members += ",\(json)"
  591. }
  592. }
  593. }
  594. SecureUserDefaults.shared.set(members, forKey: "inEditorPersonal")
  595. SecureUserDefaults.shared.set("\(members)", forKey: "membersCC")
  596. }
  597. }
  598. self.users.append(User.getData(pin: dataMessage.getPIN())!)
  599. }
  600. }
  601. }
  602. }
  603. private func makeStateCall() {
  604. var longCall = "0"
  605. if self.vcTimer.isValid {
  606. longCall = self.labelTimerVC.text ?? ""
  607. }
  608. if self.isInisiator {
  609. 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)
  610. } else {
  611. if !self.vcTimer.isValid {
  612. 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)
  613. } else {
  614. 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)
  615. }
  616. }
  617. }
  618. @objc func didTapDeclineCallButton(sender: AnyObject){
  619. let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
  620. if !onGoingCC.isEmpty {
  621. 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)
  622. alert.addAction(UIAlertAction(title: "No".localized(), style: UIAlertAction.Style.default, handler: nil))
  623. alert.addAction(UIAlertAction(title: "Yes".localized(), style: UIAlertAction.Style.default, handler: {(_) in
  624. if(!self.wbRoomId.isEmpty){
  625. DispatchQueue.main.async {
  626. self.wbTimer.invalidate()
  627. _ = Nexilis.getWhiteboardDelegate()?.terminate()
  628. }
  629. }
  630. Nexilis.stopRingtoneCall()
  631. Nexilis.stopRingbacktoneCall()
  632. QmeraVideoViewController.isLoop = false
  633. QmeraVideoViewController.bSpeakerPhone = false
  634. do { try AVAudioSession.sharedInstance().setActive(false) } catch {}
  635. Nexilis.callAPNActivated = false
  636. self.endAllCall()
  637. self.dismiss(animated: true, completion: nil)
  638. }))
  639. self.present(alert, animated: true, completion: nil)
  640. } else {
  641. let alert = LibAlertController(title: "End Video Call".localized(), message: "Are you sure you want to end video call?".localized(), preferredStyle: .alert)
  642. alert.addAction(UIAlertAction(title: "No".localized(), style: UIAlertAction.Style.default, handler: nil))
  643. alert.addAction(UIAlertAction(title: "Yes".localized(), style: UIAlertAction.Style.default, handler: {(_) in
  644. if self.callFCM && !self.vcTimer.isValid {
  645. DispatchQueue.global().async {
  646. if let _ = Nexilis.writeSync(message: CoreMessage_TMessageBank.getCancelCall(fPin: self.dataPerson[0]["f_pin"]!!, type: "2"), timeout: 30 * 1000) {
  647. } else {
  648. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  649. imageView.tintColor = .white
  650. 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)
  651. banner.show()
  652. }
  653. }
  654. }
  655. self.makeStateCall()
  656. Nexilis.stopRingtoneCall()
  657. Nexilis.stopRingbacktoneCall()
  658. // if self.labelIncomingOutgoing.isDescendant(of: self.view) {
  659. // self.labelIncomingOutgoing.text = "Video call is over".localized()
  660. // }
  661. if self.stackViewToolbar.isDescendant(of: self.view){
  662. self.stackViewToolbar.removeFromSuperview()
  663. }
  664. if self.stackViewToolbar2.isDescendant(of: self.view){
  665. self.stackViewToolbar2.removeFromSuperview()
  666. }
  667. if self.buttonWB.isDescendant(of: self.view){
  668. self.buttonWB.removeFromSuperview()
  669. }
  670. if self.buttonChat.isDescendant(of: self.view){
  671. self.buttonChat.removeFromSuperview()
  672. }
  673. if self.buttonDecline.isDescendant(of: self.view) {
  674. self.buttonDecline.removeFromSuperview()
  675. }
  676. if self.buttonAccept.isDescendant(of: self.view) {
  677. self.buttonAccept.removeFromSuperview()
  678. }
  679. if self.buttonRotate.isDescendant(of: self.view) {
  680. self.buttonRotate.removeFromSuperview()
  681. }
  682. if self.buttonMuted.isDescendant(of: self.view) {
  683. self.buttonMuted.removeFromSuperview()
  684. }
  685. if self.wbVC != nil{
  686. self.wbVC!.close?()
  687. }
  688. self.wbTimer.invalidate()
  689. self.vcTimer.invalidate()
  690. // self.labelTimerVC.text = "Video call is over".localized()
  691. QmeraVideoViewController.isLoop = false
  692. QmeraVideoViewController.bSpeakerPhone = false
  693. do { try AVAudioSession.sharedInstance().setActive(false) } catch {}
  694. Nexilis.callAPNActivated = false
  695. self.endAllCall()
  696. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  697. if self.isInisiator && !self.isPresent {
  698. self.navigationController?.popViewController(animated: true)
  699. } else {
  700. self.dismiss(animated: true, completion: nil)
  701. }
  702. }
  703. }))
  704. self.present(alert, animated: true, completion: nil)
  705. }
  706. }
  707. @objc func didTapAcceptCallButton() {
  708. if !isInisiator{
  709. let goAudioCall = Nexilis.checkMicPermission()
  710. let goVideoCall = Nexilis.checkCameraPermission()
  711. if goVideoCall == 0 {
  712. 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)
  713. alert.addAction(UIAlertAction(title: "OK".localized(), style: UIAlertAction.Style.default, handler: {_ in
  714. if let url = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(url) {
  715. UIApplication.shared.open(url, options: [:], completionHandler: nil)
  716. }
  717. }))
  718. self.navigationController?.present(alert, animated: true, completion: nil)
  719. return
  720. } else if goVideoCall == -1 {
  721. return
  722. }
  723. // Nexilis.startAudio()
  724. if ticketId.isEmpty {
  725. API.receiveCCall(sParty: dataPerson[0]["f_pin"]!, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView,ivRemoteZ: zoomView)
  726. } else {
  727. backToDefaultAudioSession()
  728. API.csa(sTicketID: ticketId, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView, bCameraOn: true)
  729. }
  730. }
  731. DispatchQueue.main.async {
  732. self.myImage.removeFromSuperview()
  733. self.name.removeFromSuperview()
  734. self.profileImage.removeFromSuperview()
  735. self.labelIncomingOutgoing.removeFromSuperview()
  736. self.buttonAccept.removeFromSuperview()
  737. NSLayoutConstraint.deactivate([
  738. self.constraintLeadingButtonDecline,
  739. self.constraintBottomButtonDecline
  740. ])
  741. self.addToolbarAfterAccept()
  742. self.buttonDecline.setImage(UIImage(systemName: "phone.down", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  743. UIView.animate(withDuration: 1.0, animations: {
  744. self.view.layoutIfNeeded()
  745. })
  746. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  747. self.containerTimerVC.isHidden = false
  748. self.buttonRotate.isHidden = false
  749. self.buttonAddParticipant.isHidden = false
  750. self.buttonSpeaker.isHidden = false
  751. self.buttonWB.isHidden = false
  752. self.buttonChat.isHidden = false
  753. self.poweredByView.isHidden = false
  754. self.buttonMuted.isHidden = false
  755. let connectDate = Date()
  756. self.vcTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
  757. let format = Utils.callDurationFormatter.string(from: Date().timeIntervalSince(connectDate))
  758. self.labelTimerVC.text = format
  759. }
  760. self.vcTimer.fire()
  761. }
  762. }
  763. }
  764. private func resetViewToOutgoing() {
  765. self.vcTimer.invalidate()
  766. self.zoomView.image = nil
  767. self.cameraView.image = nil
  768. if self.stackViewToolbar.isDescendant(of: self.view){
  769. self.stackViewToolbar.removeFromSuperview()
  770. }
  771. if self.stackViewToolbar2.isDescendant(of: self.view){
  772. self.stackViewToolbar2.removeFromSuperview()
  773. }
  774. if self.buttonWB.isDescendant(of: self.view){
  775. self.buttonWB.removeFromSuperview()
  776. }
  777. if self.buttonChat.isDescendant(of: self.view){
  778. self.buttonChat.removeFromSuperview()
  779. }
  780. if self.buttonDecline.isDescendant(of: self.view) {
  781. self.buttonDecline.removeFromSuperview()
  782. }
  783. if self.buttonAccept.isDescendant(of: self.view) {
  784. self.buttonAccept.removeFromSuperview()
  785. }
  786. if self.buttonRotate.isDescendant(of: self.view) {
  787. self.buttonRotate.removeFromSuperview()
  788. }
  789. if self.buttonMuted.isDescendant(of: self.view) {
  790. self.buttonMuted.removeFromSuperview()
  791. }
  792. addBackgroundIncoming()
  793. addProfileNameCalling()
  794. addToolbar(resetToolbar: true)
  795. }
  796. @objc func didTapChatButton(){
  797. let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
  798. let members: String = SecureUserDefaults.shared.value(forKey: "membersCC") ?? ""
  799. let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
  800. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  801. editorPersonalVC.hidesBottomBarWhenPushed = true
  802. editorPersonalVC.unique_l_pin = officer
  803. editorPersonalVC.fromNotification = true
  804. editorPersonalVC.isContactCenter = true
  805. editorPersonalVC.fPinContacCenter = members
  806. editorPersonalVC.complaintId = ticketId
  807. editorPersonalVC.onGoingCC = true
  808. editorPersonalVC.isRequestContactCenter = false
  809. editorPersonalVC.channelContactCenter = "2"
  810. editorPersonalVC.users = users
  811. editorPersonalVC.fromVCAC = true
  812. let navigationController = CustomNavigationController(rootViewController: editorPersonalVC)
  813. navigationController.modalPresentationStyle = .overCurrentContext
  814. navigationController.navigationBar.tintColor = .white
  815. navigationController.navigationBar.barTintColor = self.traitCollection.userInterfaceStyle == .dark ? .blackDarkMode : .mainColor
  816. navigationController.navigationBar.isTranslucent = false
  817. navigationController.navigationBar.overrideUserInterfaceStyle = .dark
  818. navigationController.navigationBar.barStyle = .black
  819. let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]
  820. UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, for: .normal)
  821. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  822. navigationController.navigationBar.titleTextAttributes = textAttributes
  823. if UIApplication.shared.visibleViewController?.navigationController != nil {
  824. UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
  825. } else {
  826. UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
  827. }
  828. }
  829. @objc func didTapWBButton(){
  830. if(wbVC == nil){
  831. wbVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "wbVC") as? WhiteboardViewController
  832. if(wbRoomId.isEmpty){
  833. let me = User.getMyPin()!
  834. let tid = CoreMessage_TMessageUtil.getTID()
  835. wbRoomId = "\(me)wbvc\(tid)"
  836. wbVC!.roomId = wbRoomId
  837. var destinations = [String]()
  838. var destString = ""
  839. for d in dataPerson{
  840. destinations.append(d["f_pin"]!!)
  841. if destString.isEmpty{
  842. destString = d["f_pin"]!!
  843. } else {
  844. destString = destString + ",\(d["f_pin"]!!)"
  845. }
  846. }
  847. wbVC!.destinations = destinations
  848. wbVC!.sendInit()
  849. SecureUserDefaults.shared.set("\(me),\(destString)", forKey: "wb_vc")
  850. }
  851. else {
  852. self.wbTimer.invalidate()
  853. self.buttonWB.backgroundColor = .lightGray
  854. wbVC!.roomId = wbRoomId
  855. wbVC!.sendJoin()
  856. }
  857. }
  858. wbVC!.close = {
  859. DispatchQueue.main.async {
  860. if self.wbVC!.view.isDescendant(of: self.view){
  861. self.wbVC!.view.removeFromSuperview()
  862. }
  863. self.buttonDecline.isHidden = false
  864. self.buttonSpeaker.isHidden = false
  865. self.buttonAddParticipant.isHidden = false
  866. self.buttonRotate.isHidden = false
  867. self.buttonMuted.isHidden = false
  868. // if(!self.wbRoomId.isEmpty){
  869. // DispatchQueue.main.async {
  870. // self.wbTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.runTimer), userInfo: nil, repeats: true)
  871. // }
  872. // }
  873. }
  874. }
  875. self.buttonDecline.isHidden = true
  876. self.buttonSpeaker.isHidden = true
  877. self.buttonMuted.isHidden = true
  878. self.buttonAddParticipant.isHidden = true
  879. self.buttonRotate.isHidden = true
  880. addChild(wbVC!)
  881. wbVC!.view.translatesAutoresizingMaskIntoConstraints = false
  882. view.addSubview(wbVC!.view)
  883. onScreenConstraintWB = [
  884. wbVC!.view.topAnchor.constraint(equalTo: self.view.topAnchor),
  885. wbVC!.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
  886. wbVC!.view.rightAnchor.constraint(equalTo: self.view.rightAnchor),
  887. wbVC!.view.leftAnchor.constraint(equalTo: self.view.leftAnchor),
  888. ]
  889. NSLayoutConstraint.activate(onScreenConstraintWB)
  890. // Notify the child view controller that the move is complete.
  891. wbVC!.didMove(toParent: self)
  892. // self.navigationController?.setNavigationBarHidden(false, animated: true)
  893. // controller.modalPresentationStyle = .overCurrentContext
  894. // self.navigationController?.present(controller, animated: true)
  895. }
  896. func addToolbarAfterAccept() {
  897. Nexilis.stopRingtoneCall()
  898. view.addSubview(self.stackViewToolbar)
  899. self.stackViewToolbar.translatesAutoresizingMaskIntoConstraints = false
  900. constraintBottomStackViewToolbar = self.stackViewToolbar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -60.0)
  901. NSLayoutConstraint.activate([
  902. self.stackViewToolbar.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
  903. constraintBottomStackViewToolbar
  904. ])
  905. self.stackViewToolbar.axis = .horizontal
  906. self.stackViewToolbar.distribution = .equalSpacing
  907. self.stackViewToolbar.alignment = .center
  908. self.stackViewToolbar.spacing = 30
  909. view.addSubview(self.stackViewToolbar3)
  910. self.stackViewToolbar3.anchor(bottom: self.stackViewToolbar.topAnchor, paddingBottom: 10, centerX: self.view.centerXAnchor)
  911. self.stackViewToolbar3.axis = .horizontal
  912. self.stackViewToolbar3.distribution = .equalSpacing
  913. self.stackViewToolbar3.alignment = .center
  914. self.stackViewToolbar3.spacing = 30
  915. view.addSubview(buttonRotate)
  916. buttonRotate.translatesAutoresizingMaskIntoConstraints = false
  917. buttonRotate.frame.size = CGSize(width: 70.0, height: 70.0)
  918. NSLayoutConstraint.activate([
  919. buttonRotate.widthAnchor.constraint(equalToConstant: 70.0),
  920. buttonRotate.heightAnchor.constraint(equalToConstant: 70.0),
  921. ])
  922. buttonRotate.backgroundColor = .secondaryColor
  923. buttonRotate.setImage(UIImage(systemName: "arrow.triangle.2.circlepath.camera", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  924. buttonRotate.tintColor = .mainColor
  925. buttonRotate.circle()
  926. buttonRotate.isHidden = true
  927. buttonRotate.addTarget(self, action: #selector(camera(sender:)), for: .touchUpInside)
  928. view.addSubview(buttonMuted)
  929. buttonMuted.translatesAutoresizingMaskIntoConstraints = false
  930. buttonMuted.frame.size = CGSize(width: 70.0, height: 70.0)
  931. NSLayoutConstraint.activate([
  932. buttonMuted.widthAnchor.constraint(equalToConstant: 70.0),
  933. buttonMuted.heightAnchor.constraint(equalToConstant: 70.0),
  934. ])
  935. buttonMuted.backgroundColor = .secondaryColor
  936. buttonMuted.setImage(UIImage(systemName: "mic", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  937. buttonMuted.tintColor = .mainColor
  938. buttonMuted.circle()
  939. buttonMuted.isHidden = true
  940. buttonMuted.addTarget(self, action: #selector(muted(sender:)), for: .touchUpInside)
  941. view.addSubview(buttonAddParticipant)
  942. buttonAddParticipant.translatesAutoresizingMaskIntoConstraints = false
  943. buttonAddParticipant.frame.size = CGSize(width: 70.0, height: 70.0)
  944. NSLayoutConstraint.activate([
  945. buttonAddParticipant.widthAnchor.constraint(equalToConstant: 70.0),
  946. buttonAddParticipant.heightAnchor.constraint(equalToConstant: 70.0)
  947. ])
  948. buttonAddParticipant.backgroundColor = .secondaryColor
  949. buttonAddParticipant.setImage(UIImage(systemName: "person.badge.plus", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  950. buttonAddParticipant.tintColor = .mainColor
  951. buttonAddParticipant.circle()
  952. buttonAddParticipant.isHidden = true
  953. buttonAddParticipant.addTarget(self, action: #selector(didTapAddParticipantButton(sender:)), for: .touchUpInside)
  954. view.addSubview(buttonSpeaker)
  955. buttonSpeaker.translatesAutoresizingMaskIntoConstraints = false
  956. buttonSpeaker.frame.size = CGSize(width: 70.0, height: 70.0)
  957. NSLayoutConstraint.activate([
  958. buttonSpeaker.widthAnchor.constraint(equalToConstant: 70.0),
  959. buttonSpeaker.heightAnchor.constraint(equalToConstant: 70.0)
  960. ])
  961. buttonSpeaker.backgroundColor = .secondaryColor
  962. buttonSpeaker.tintColor = .mainColor
  963. buttonSpeaker.setImage(UIImage(systemName: "speaker.slash", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  964. buttonSpeaker.circle()
  965. buttonSpeaker.isHidden = true
  966. buttonSpeaker.addTarget(self, action: #selector(didTapSpeakerButton(sender:)), for: .touchUpInside)
  967. view.addSubview(self.stackViewToolbar2)
  968. self.stackViewToolbar2.translatesAutoresizingMaskIntoConstraints = false
  969. constraintLeftStackViewToolbar2 = self.stackViewToolbar2.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 10.0)
  970. NSLayoutConstraint.activate([
  971. self.stackViewToolbar2.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
  972. constraintLeftStackViewToolbar2
  973. ])
  974. self.stackViewToolbar2.axis = .vertical
  975. self.stackViewToolbar2.distribution = .equalSpacing
  976. self.stackViewToolbar2.alignment = .center
  977. self.stackViewToolbar2.spacing = 5
  978. view.addSubview(buttonWB)
  979. buttonWB.translatesAutoresizingMaskIntoConstraints = false
  980. buttonWB.frame.size = CGSize(width: 40.0, height: 40.0)
  981. NSLayoutConstraint.activate([
  982. buttonWB.widthAnchor.constraint(equalToConstant: 40.0),
  983. buttonWB.heightAnchor.constraint(equalToConstant: 40.0)
  984. ])
  985. buttonWB.backgroundColor = .lightGray
  986. buttonWB.setImage(UIImage(systemName: "ipad.landscape", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .medium, scale: .default)), for: .normal)
  987. buttonWB.circle()
  988. buttonWB.tintColor = .black
  989. buttonWB.isHidden = true
  990. buttonWB.addTarget(self, action: #selector(didTapWBButton), for: .touchUpInside)
  991. if !ticketId.isEmpty {
  992. view.addSubview(buttonChat)
  993. buttonChat.translatesAutoresizingMaskIntoConstraints = false
  994. buttonChat.frame.size = CGSize(width: 40.0, height: 40.0)
  995. NSLayoutConstraint.activate([
  996. buttonChat.widthAnchor.constraint(equalToConstant: 40.0),
  997. buttonChat.heightAnchor.constraint(equalToConstant: 40.0)
  998. ])
  999. buttonChat.backgroundColor = .lightGray
  1000. buttonChat.setImage(UIImage(systemName: "bubble.right", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .medium, scale: .default)), for: .normal)
  1001. buttonChat.circle()
  1002. buttonChat.tintColor = .black
  1003. buttonChat.isHidden = true
  1004. buttonChat.addTarget(self, action: #selector(didTapChatButton), for: .touchUpInside)
  1005. }
  1006. view.addSubview(poweredByView)
  1007. self.poweredByView.translatesAutoresizingMaskIntoConstraints = false
  1008. let constraintRightPowered = self.poweredByView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -10.0)
  1009. let constraintBottomPowered = self.poweredByView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10.0)
  1010. NSLayoutConstraint.activate([
  1011. constraintRightPowered,
  1012. constraintBottomPowered,
  1013. nexilisLogo.widthAnchor.constraint(equalToConstant: 30.0),
  1014. nexilisLogo.heightAnchor.constraint(equalToConstant: 30.0)
  1015. ])
  1016. poweredByView.addArrangedSubview(poweredByLabel)
  1017. poweredByView.addArrangedSubview(nexilisLogo)
  1018. poweredByView.isHidden = true
  1019. stackViewToolbar.addArrangedSubview(buttonAddParticipant)
  1020. stackViewToolbar.addArrangedSubview(buttonDecline)
  1021. stackViewToolbar.addArrangedSubview(buttonSpeaker)
  1022. stackViewToolbar2.addArrangedSubview(buttonWB)
  1023. stackViewToolbar2.addArrangedSubview(buttonChat)
  1024. stackViewToolbar3.addArrangedSubview(buttonRotate)
  1025. stackViewToolbar3.addArrangedSubview(buttonMuted)
  1026. // startFaceTimer()
  1027. }
  1028. func endAllCall() {
  1029. let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
  1030. if !onGoingCC.isEmpty {
  1031. let requester = onGoingCC.components(separatedBy: ",")[0]
  1032. let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
  1033. let complaintId = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2]
  1034. let idMe = User.getMyPin()!
  1035. let startTimeCC: String = SecureUserDefaults.shared.value(forKey: "startTimeCC") ?? ""
  1036. DispatchQueue.global().async {
  1037. let date = "\(Date().currentTimeMillis())"
  1038. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  1039. do {
  1040. _ = try Database.shared.insertRecord(fmdb: fmdb, table: "CALL_CENTER_HISTORY", cvalues: [
  1041. "type" : "2",
  1042. "title" : "Contact Center".localized(),
  1043. "time" : startTimeCC,
  1044. "f_pin" : officer,
  1045. "data" : complaintId,
  1046. "time_end" : date,
  1047. "complaint_id" : complaintId,
  1048. "members" : "",
  1049. "requester": requester
  1050. ], replace: true)
  1051. } catch {
  1052. rollback.pointee = true
  1053. print("Access database error: \(error.localizedDescription)")
  1054. }
  1055. })
  1056. if officer == idMe {
  1057. _ = Nexilis.write(message: CoreMessage_TMessageBank.endCallCenter(complaint_id: complaintId, l_pin: requester))
  1058. } else {
  1059. if requester == idMe {
  1060. _ = Nexilis.write(message: CoreMessage_TMessageBank.endCallCenter(complaint_id: complaintId, l_pin: officer))
  1061. } else {
  1062. _ = Nexilis.write(message: CoreMessage_TMessageBank.leaveCCRoomInvite(ticket_id: complaintId))
  1063. }
  1064. }
  1065. SecureUserDefaults.shared.removeValue(forKey: "onGoingCC")
  1066. SecureUserDefaults.shared.removeValue(forKey: "membersCC")
  1067. SecureUserDefaults.shared.removeValue(forKey: "startTimeCC")
  1068. SecureUserDefaults.shared.removeValue(forKey: "waitingRequestCC")
  1069. }
  1070. }
  1071. API.terminateCall(sParty: nil)
  1072. if APIS.uuidCall != nil {
  1073. CallManager.shared.endCall(uuid: APIS.uuidCall!) {
  1074. APIS.uuidCall = nil
  1075. }
  1076. }
  1077. cameraView.image = nil
  1078. zoomView.image = nil
  1079. listRemoteViewFix.removeAll()
  1080. dataPerson.removeAll()
  1081. }
  1082. func setSpeaker() {
  1083. DispatchQueue.main.async {
  1084. self.timerSpeaker?.invalidate()
  1085. self.tempSpeaker = !self.tempSpeaker
  1086. if (self.tempSpeaker) {
  1087. self.buttonSpeaker.backgroundColor = .lightGray
  1088. self.buttonSpeaker.tintColor = .mainColor
  1089. self.buttonSpeaker.setImage(UIImage(systemName: "speaker.wave.2", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  1090. } else {
  1091. self.buttonSpeaker.backgroundColor = .secondaryColor
  1092. self.buttonSpeaker.tintColor = .mainColor
  1093. self.buttonSpeaker.setImage(UIImage(systemName: "speaker.slash", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  1094. }
  1095. self.timerSpeaker = Timer.scheduledTimer(withTimeInterval: 1, repeats: false, block: {_ in
  1096. if QmeraVideoViewController.bSpeakerPhone != self.tempSpeaker {
  1097. QmeraVideoViewController.bSpeakerPhone = !QmeraVideoViewController.bSpeakerPhone
  1098. self.tempSpeaker = QmeraVideoViewController.bSpeakerPhone
  1099. DispatchQueue.global().async {
  1100. QmeraVideoViewController.turnSpeakerOn()
  1101. }
  1102. }
  1103. })
  1104. }
  1105. }
  1106. @objc func didTapSpeakerButton(sender: AnyObject){
  1107. setSpeaker()
  1108. }
  1109. @objc func didTapAddParticipantButton(sender: AnyObject){
  1110. if let contactViewController = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactSID") as? ContactCallViewController {
  1111. contactViewController.isAddParticipantVideo = true
  1112. contactViewController.connectedCall = dataPerson
  1113. contactViewController.isDismiss = { data in
  1114. let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
  1115. if !onGoingCC.isEmpty {
  1116. DispatchQueue.global().async {
  1117. _ = Nexilis.write(message: CoreMessage_TMessageBank.getCCRoomInvite(l_pin: data["f_pin"]!!, ticket_id: onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2], channel: "2"))
  1118. }
  1119. DispatchQueue.main.async {
  1120. self.isAddCall = data["f_pin"]!!
  1121. }
  1122. } else {
  1123. if self.callFCM {
  1124. DispatchQueue.global().async {
  1125. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getCalling(fPin: data["f_pin"]!!, type: "2"), timeout: 30 * 1000) {
  1126. if response.isOk() {
  1127. } else if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "01" {
  1128. self.dataPerson.append(data)
  1129. if let user = User.getData(pin: String(data["f_pin"]!!)) {
  1130. self.users.append(user)
  1131. }
  1132. API.initiateCCall(sParty: data["f_pin"]!, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: self.listRemoteViewFix, ivLocalView: self.cameraView, ivRemoteZ: self.zoomView)
  1133. Nexilis.playRingbacktoneCall()
  1134. } else {
  1135. Nexilis.stopRingbacktoneCall()
  1136. }
  1137. } else {
  1138. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  1139. imageView.tintColor = .white
  1140. 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)
  1141. banner.show()
  1142. }
  1143. }
  1144. } else {
  1145. DispatchQueue.main.async {
  1146. self.dataPerson.append(data)
  1147. if let user = User.getData(pin: String(data["f_pin"]!!)) {
  1148. self.users.append(user)
  1149. }
  1150. API.initiateCCall(sParty: data["f_pin"]!, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: self.listRemoteViewFix, ivLocalView: self.cameraView, ivRemoteZ: self.zoomView)
  1151. Nexilis.playRingbacktoneCall()
  1152. }
  1153. }
  1154. }
  1155. }
  1156. present(CustomNavigationController(rootViewController: contactViewController), animated: true, completion: nil)
  1157. }
  1158. }
  1159. @objc func camera(sender: Any?) {
  1160. DispatchQueue.global().async { [self] in
  1161. if frontCamera {
  1162. API.changeCameraParam(nCameraIdx: 0, nResolutionIndex: 2, nQuality: 4)
  1163. frontCamera = false
  1164. } else {
  1165. API.changeCameraParam(nCameraIdx: 1, nResolutionIndex: 2, nQuality: 4)
  1166. frontCamera = true
  1167. }
  1168. }
  1169. }
  1170. @objc func muted(sender: Any?) {
  1171. isMuted = !isMuted
  1172. API.mmc(int: 1, boolean: isMuted)
  1173. DispatchQueue.main.async {
  1174. if (self.isMuted) {
  1175. self.buttonMuted.backgroundColor = .lightGray
  1176. self.buttonMuted.tintColor = .mainColor
  1177. self.buttonMuted.setImage(UIImage(systemName: "mic.slash", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  1178. } else {
  1179. self.buttonMuted.backgroundColor = .secondaryColor
  1180. self.buttonMuted.tintColor = .mainColor
  1181. self.buttonMuted.setImage(UIImage(systemName: "mic", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  1182. }
  1183. }
  1184. }
  1185. @objc func hideToolbar() {
  1186. DispatchQueue.main.async {
  1187. if self.showStackViewToolbar {
  1188. self.showStackViewToolbar = false
  1189. self.constraintBottomStackViewToolbar.constant = 150
  1190. self.constraintLeftStackViewToolbar2.constant = -60
  1191. UIView.animate(withDuration: 0.35, animations: {
  1192. self.view.layoutIfNeeded()
  1193. })
  1194. } else {
  1195. self.showStackViewToolbar = true
  1196. self.constraintBottomStackViewToolbar.constant = -60
  1197. self.constraintLeftStackViewToolbar2.constant = 10
  1198. UIView.animate(withDuration: 0.35, animations: {
  1199. self.view.layoutIfNeeded()
  1200. })
  1201. }
  1202. }
  1203. }
  1204. @objc func onStatusCall(_ notification: NSNotification) {
  1205. let data = notification.userInfo
  1206. let state = (data?["state"] ?? 0) as! Int
  1207. let message = (data?["message"] ?? "") as! String
  1208. var remoteChannel = [String:String]()
  1209. let arrayMessage = message.split(separator: ",")
  1210. if state == Nexilis.VIDEO_CALL_INCOMING {
  1211. if autoAcceptAPN {
  1212. didTapAcceptCallButton()
  1213. }
  1214. }
  1215. else if state == Nexilis.AUDIO_VIDEO_CALL_MUTED {
  1216. DispatchQueue.main.async { [self] in
  1217. if self.dataPerson.count == 1 {
  1218. if arrayMessage[1] == "1" {
  1219. mutedZoom.isHidden = false
  1220. } else {
  1221. mutedZoom.isHidden = true
  1222. }
  1223. }
  1224. }
  1225. } else if (state == Nexilis.VIDEO_CALL_ZOOM) && self.dataPerson.count > 1 {
  1226. DispatchQueue.main.async {
  1227. self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 2.0).rotated(by: (-CGFloat.pi)/2)
  1228. self.zoomView.contentMode = .scaleAspectFit
  1229. }
  1230. } else if (state == Nexilis.VIDEO_CAMERA_PARAMS_CHANED){
  1231. if(arrayMessage[3] == "0"){
  1232. DispatchQueue.main.async {
  1233. if self.dataPerson.count == 1 && arrayMessage[2] == "1" && arrayMessage[4] == "1" {
  1234. self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 2.0).rotated(by: (-CGFloat.pi)/2)
  1235. } else {
  1236. self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 2.0).rotated(by: (CGFloat.pi)/2)
  1237. }
  1238. }
  1239. }
  1240. }
  1241. else if state == Nexilis.STREAMING_SEMINAR_ENDED { // always call turnspeaker
  1242. // QmeraVideoViewController.isLoop = true
  1243. // DispatchQueue.global().asyncAfter(deadline: .now() + 0.5, execute: {
  1244. // API.adjustVolume(fValue: 10.0)
  1245. // self.setSpeaker()
  1246. // DispatchQueue.global(qos: .userInitiated).async {
  1247. // repeat {
  1248. // Thread.sleep(forTimeInterval : 1)
  1249. // if (QmeraVideoViewController.isLoop && !API.bAudioEngineIsRunning()) {
  1250. // API.turnSpeakerPhone(bSPon: QmeraVideoViewController.bSpeakerPhone!)
  1251. // }
  1252. // } while (QmeraVideoViewController.isLoop)
  1253. // }
  1254. // })
  1255. self.setSpeaker()
  1256. }
  1257. else if (state == Nexilis.VIDEO_CALL_OFFHOOK) {
  1258. DispatchQueue.main.async {
  1259. Nexilis.stopRingbacktoneCall()
  1260. }
  1261. let channel = arrayMessage[3]
  1262. remoteChannel[String(channel)] = String(arrayMessage[5])
  1263. DispatchQueue.main.async {
  1264. if (self.dataPerson.count == 1 && String(arrayMessage[1]) != self.dataPerson[0]["f_pin"]!!) {
  1265. self.getDataProfile(fPin: String(arrayMessage[1]))
  1266. for i in 0...1 {
  1267. self.scrollRemoteView.addSubview(self.listRemoteViewFix[i])
  1268. self.listRemoteViewFix[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 160)
  1269. self.listRemoteViewFix[i].backgroundColor = .clear
  1270. self.listRemoteViewFix[i].makeRoundedView(radius: 8.0)
  1271. self.scrollRemoteView.addSubview(self.containerLabelName[i])
  1272. self.containerLabelName[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 30)
  1273. self.containerLabelName[i].backgroundColor = .orangeBNI.withAlphaComponent(0.5)
  1274. self.containerLabelName[i].makeRoundedView(radius: 8.0)
  1275. if i == 0 {
  1276. if self.dataPerson[0]["user_type"] == "2" {
  1277. self.listRemoteViewFix[0].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi)/2)
  1278. } else {
  1279. self.listRemoteViewFix[0].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (-CGFloat.pi)/2)
  1280. }
  1281. } else {
  1282. if arrayMessage[5] == "2" {
  1283. self.listRemoteViewFix[1].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi)/2)
  1284. } else {
  1285. self.listRemoteViewFix[1].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (-CGFloat.pi)/2)
  1286. }
  1287. }
  1288. let pictureImage = self.dataPerson[i]["picture"] ?? ""
  1289. let namePerson = self.dataPerson[i]["name"] ?? ""
  1290. if (!pictureImage!.isEmpty) {
  1291. self.listRemoteViewFix[i].setImage(name: pictureImage!)
  1292. self.listRemoteViewFix[i].contentMode = .scaleAspectFill
  1293. } else {
  1294. self.listRemoteViewFix[i].image = UIImage(systemName: "person")
  1295. self.listRemoteViewFix[i].backgroundColor = UIColor.systemGray6
  1296. self.listRemoteViewFix[i].contentMode = .scaleAspectFit
  1297. }
  1298. let labelName = UILabel()
  1299. self.containerLabelName[i].addSubview(labelName)
  1300. 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)
  1301. labelName.text = namePerson
  1302. labelName.textAlignment = .center
  1303. labelName.textColor = .white
  1304. }
  1305. self.scrollRemoteView.contentSize.height = CGFloat(170 * 2)
  1306. if self.buttonWB.isEnabled {
  1307. self.buttonWB.isEnabled = false
  1308. }
  1309. if self.buttonRotate.isEnabled {
  1310. self.buttonRotate.isEnabled = false
  1311. if self.wbVC != nil && self.wbVC!.view.isDescendant(of: self.view){
  1312. self.wbVC!.view.removeFromSuperview()
  1313. self.buttonDecline.isHidden = false
  1314. self.buttonSpeaker.isHidden = false
  1315. self.buttonAddParticipant.isHidden = false
  1316. self.buttonRotate.isHidden = false
  1317. self.buttonMuted.isHidden = false
  1318. }
  1319. }
  1320. } else if self.dataPerson.count > 1 {
  1321. if self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[1]}) == nil {
  1322. self.getDataProfile(fPin: String(arrayMessage[1]))
  1323. let i = self.dataPerson.count - 1
  1324. self.scrollRemoteView.addSubview(self.listRemoteViewFix[i])
  1325. self.listRemoteViewFix[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 160)
  1326. self.listRemoteViewFix[i].backgroundColor = .clear
  1327. self.listRemoteViewFix[i].makeRoundedView(radius: 8.0)
  1328. self.scrollRemoteView.addSubview(self.containerLabelName[i])
  1329. self.containerLabelName[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 30)
  1330. self.containerLabelName[i].backgroundColor = .orangeBNI.withAlphaComponent(0.5)
  1331. self.containerLabelName[i].makeRoundedView(radius: 8.0)
  1332. if arrayMessage[5] == "2" {
  1333. self.listRemoteViewFix[i].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi)/2)
  1334. } else {
  1335. self.listRemoteViewFix[i].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (-CGFloat.pi)/2)
  1336. }
  1337. let pictureImage = self.dataPerson[self.dataPerson.count - 1]["picture"] ?? ""
  1338. let namePerson = self.dataPerson[self.dataPerson.count - 1]["name"] ?? ""
  1339. if (!pictureImage!.isEmpty) {
  1340. self.listRemoteViewFix[i].setImage(name: pictureImage!)
  1341. self.listRemoteViewFix[i].contentMode = .scaleAspectFill
  1342. } else {
  1343. self.listRemoteViewFix[i].image = UIImage(systemName: "person")
  1344. self.listRemoteViewFix[i].backgroundColor = UIColor.systemGray6
  1345. self.listRemoteViewFix[i].contentMode = .scaleAspectFit
  1346. }
  1347. self.scrollRemoteView.contentSize.height = CGFloat(170 * (i + 1))
  1348. let labelName = UILabel()
  1349. self.containerLabelName[i].addSubview(labelName)
  1350. 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)
  1351. labelName.text = namePerson
  1352. labelName.textAlignment = .center
  1353. labelName.textColor = .white
  1354. }
  1355. }
  1356. if let user = User.getData(pin: String(arrayMessage[1])) {
  1357. if !self.users.contains(user) {
  1358. user.isConnected = true
  1359. self.users.append(user)
  1360. } else if let userEx = self.users.firstIndex(where: { $0.pin == String(arrayMessage[1]) }) {
  1361. self.users[userEx].isConnected = true
  1362. }
  1363. }
  1364. if arrayMessage[5] == "2" && self.dataPerson.count == 1 {
  1365. DispatchQueue.main.async {
  1366. self.zoomView.transform = CGAffineTransform.init(scaleX: -1.9, y: 2.0).rotated(by: (CGFloat.pi)/2)
  1367. self.zoomView.contentMode = .scaleAspectFit
  1368. }
  1369. }
  1370. else if self.dataPerson.count == 1 {
  1371. DispatchQueue.main.async {
  1372. self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 2.0).rotated(by: (-CGFloat.pi)/2)
  1373. self.zoomView.contentMode = .scaleAspectFit
  1374. }
  1375. } else if self.dataPerson.count > 1 {
  1376. DispatchQueue.main.async {
  1377. for i in 0..<self.dataPerson.count {
  1378. self.listRemoteViewFix[i].image = nil
  1379. if self.dataPerson[i]["user_type"] == "2" || arrayMessage[5] == "2" {
  1380. self.listRemoteViewFix[i].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi)/2)
  1381. } else {
  1382. self.listRemoteViewFix[i].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (-CGFloat.pi)/2)
  1383. }
  1384. }
  1385. }
  1386. }
  1387. }
  1388. DispatchQueue.main.async {
  1389. if self.name.isDescendant(of: self.view) {
  1390. self.didTapAcceptCallButton()
  1391. if QmeraVideoViewController.bSpeakerPhone {
  1392. DispatchQueue.main.async {
  1393. QmeraVideoViewController.bSpeakerPhone = false
  1394. self.setSpeaker()
  1395. }
  1396. }
  1397. }
  1398. let indexPerson = self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[1]})
  1399. if indexPerson != nil {
  1400. self.dataPerson[indexPerson!]["user_type"] = String(arrayMessage[5])
  1401. }
  1402. }
  1403. } else if (state == Nexilis.VIDEO_CALL_END || state == Nexilis.AUDIO_CALL_END) {
  1404. DispatchQueue.main.async {
  1405. Nexilis.stopRingtoneCall()
  1406. Nexilis.stopRingbacktoneCall()
  1407. }
  1408. let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
  1409. if !onGoingCC.isEmpty {
  1410. let requester = onGoingCC.components(separatedBy: ",")[0]
  1411. let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
  1412. if arrayMessage[0] == requester || arrayMessage[0] == officer {
  1413. DispatchQueue.main.async {
  1414. let controller = self.presentedViewController
  1415. if controller != nil {
  1416. controller!.dismiss(animated: true)
  1417. }
  1418. if !self.showNotifCCEnd{
  1419. let imageView = UIImageView(image: UIImage(systemName: "info.circle"))
  1420. imageView.tintColor = .white
  1421. 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)
  1422. banner.show()
  1423. self.showNotifCCEnd = true
  1424. }
  1425. if self.stackViewToolbar.isDescendant(of: self.view){
  1426. self.stackViewToolbar.removeFromSuperview()
  1427. }
  1428. if self.stackViewToolbar2.isDescendant(of: self.view){
  1429. self.stackViewToolbar2.removeFromSuperview()
  1430. }
  1431. if self.buttonWB.isDescendant(of: self.view){
  1432. self.buttonWB.removeFromSuperview()
  1433. }
  1434. if self.buttonChat.isDescendant(of: self.view){
  1435. self.buttonChat.removeFromSuperview()
  1436. }
  1437. if self.buttonDecline.isDescendant(of: self.view) {
  1438. self.buttonDecline.removeFromSuperview()
  1439. }
  1440. if self.buttonAccept.isDescendant(of: self.view) {
  1441. self.buttonAccept.removeFromSuperview()
  1442. }
  1443. if self.buttonRotate.isDescendant(of: self.view) {
  1444. self.buttonRotate.removeFromSuperview()
  1445. }
  1446. if self.buttonMuted.isDescendant(of: self.view) {
  1447. self.buttonMuted.removeFromSuperview()
  1448. }
  1449. if self.wbVC != nil{
  1450. self.wbVC!.close?()
  1451. }
  1452. self.wbTimer.invalidate()
  1453. _ = Nexilis.getWhiteboardDelegate()?.terminate()
  1454. }
  1455. QmeraVideoViewController.isLoop = false
  1456. QmeraVideoViewController.bSpeakerPhone = false
  1457. do { try AVAudioSession.sharedInstance().setActive(false) } catch {}
  1458. Nexilis.callAPNActivated = false
  1459. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  1460. self.endAllCall()
  1461. self.dismiss(animated: true, completion: nil)
  1462. }
  1463. return
  1464. }
  1465. }
  1466. DispatchQueue.main.async {
  1467. if (self.dataPerson.count == 1) {
  1468. self.makeStateCall()
  1469. // if self.labelIncomingOutgoing.isDescendant(of: self.view) {
  1470. // self.labelIncomingOutgoing.text = "Video call is over".localized()
  1471. // }
  1472. if self.stackViewToolbar.isDescendant(of: self.view){
  1473. self.stackViewToolbar.removeFromSuperview()
  1474. }
  1475. if self.stackViewToolbar2.isDescendant(of: self.view){
  1476. self.stackViewToolbar2.removeFromSuperview()
  1477. }
  1478. if self.buttonWB.isDescendant(of: self.view){
  1479. self.buttonWB.removeFromSuperview()
  1480. }
  1481. if self.buttonChat.isDescendant(of: self.view){
  1482. self.buttonChat.removeFromSuperview()
  1483. }
  1484. if self.buttonDecline.isDescendant(of: self.view) {
  1485. self.buttonDecline.removeFromSuperview()
  1486. }
  1487. if self.buttonAccept.isDescendant(of: self.view) {
  1488. self.buttonAccept.removeFromSuperview()
  1489. }
  1490. if self.buttonRotate.isDescendant(of: self.view) {
  1491. self.buttonRotate.removeFromSuperview()
  1492. }
  1493. if self.buttonMuted.isDescendant(of: self.view) {
  1494. self.buttonMuted.removeFromSuperview()
  1495. }
  1496. if self.wbVC != nil{
  1497. self.wbVC!.close?()
  1498. }
  1499. self.wbTimer.invalidate()
  1500. self.vcTimer.invalidate()
  1501. // self.labelTimerVC.text = "Video call is over".localized()
  1502. _ = Nexilis.getWhiteboardDelegate()?.terminate()
  1503. let controller = self.presentedViewController
  1504. if controller != nil {
  1505. controller!.dismiss(animated: true)
  1506. }
  1507. QmeraVideoViewController.isLoop = false
  1508. QmeraVideoViewController.bSpeakerPhone = false
  1509. do { try AVAudioSession.sharedInstance().setActive(false) } catch {}
  1510. Nexilis.callAPNActivated = false
  1511. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  1512. self.endAllCall()
  1513. if self.isInisiator && !self.isPresent {
  1514. self.navigationController?.popViewController(animated: true)
  1515. } else {
  1516. self.dismiss(animated: true, completion: nil)
  1517. }
  1518. }
  1519. } else {
  1520. let indexPerson = self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[0]})
  1521. if indexPerson != nil {
  1522. if (self.dataPerson.count == 2) {
  1523. self.containerLabelName.forEach({ $0.subviews.forEach({ $0.removeFromSuperview() }) })
  1524. self.scrollRemoteView.subviews.forEach({ $0.removeFromSuperview() })
  1525. if !self.buttonWB.isEnabled {
  1526. self.buttonWB.isEnabled = true
  1527. }
  1528. if !self.buttonRotate.isEnabled {
  1529. self.buttonRotate.isEnabled = true
  1530. }
  1531. } else {
  1532. self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
  1533. self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
  1534. self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
  1535. self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
  1536. if indexPerson! + 1 <= self.listRemoteViewFix.count {
  1537. let iLoop = (self.listRemoteViewFix.count - 1) - (indexPerson! + 1)
  1538. if iLoop >= 0 {
  1539. for i in 0...iLoop {
  1540. let viewAfterRemote = self.listRemoteViewFix[(indexPerson! + i) + 1]
  1541. let viewAfterName = self.containerLabelName[(indexPerson! + i) + 1]
  1542. viewAfterRemote.frame.origin.y = viewAfterRemote.frame.origin.y - 170
  1543. viewAfterName.frame.origin.y = viewAfterName.frame.origin.y - 170
  1544. UIView.animate(withDuration: 0.35, animations: {
  1545. self.scrollRemoteView.layoutIfNeeded()
  1546. })
  1547. }
  1548. }
  1549. }
  1550. }
  1551. self.dataPerson.remove(at: indexPerson!)
  1552. }
  1553. let pin = "\(arrayMessage[0])"
  1554. if let index = self.users.firstIndex(where: { $0.pin == pin }) {
  1555. self.users.remove(at: index)
  1556. }
  1557. if self.dataPerson.count == 1 {
  1558. self.transformZoomAfterNewUserMore2 = false
  1559. self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 2.0).rotated(by: (CGFloat.pi)/2)
  1560. if !self.users[0].isConnected {
  1561. self.resetViewToOutgoing()
  1562. }
  1563. }
  1564. }
  1565. }
  1566. } else if (state == Nexilis.OFFLINE) {
  1567. DispatchQueue.main.async {
  1568. Nexilis.stopRingtoneCall()
  1569. Nexilis.stopRingbacktoneCall()
  1570. }
  1571. let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
  1572. DispatchQueue.main.async {
  1573. if (self.dataPerson.count == 1) {
  1574. if self.labelIncomingOutgoing.isDescendant(of: self.view) {
  1575. self.labelIncomingOutgoing.text = "Offline".localized()
  1576. }
  1577. if self.buttonDecline.isDescendant(of: self.view) {
  1578. self.buttonDecline.removeFromSuperview()
  1579. }
  1580. if self.buttonAccept.isDescendant(of: self.view) {
  1581. self.buttonAccept.removeFromSuperview()
  1582. }
  1583. } else {
  1584. let indexPerson = self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[0]})
  1585. if indexPerson != nil {
  1586. if (self.dataPerson.count == 2) {
  1587. self.containerLabelName.forEach({ $0.subviews.forEach({ $0.removeFromSuperview() }) })
  1588. self.scrollRemoteView.subviews.forEach({ $0.removeFromSuperview() })
  1589. if !self.buttonWB.isEnabled {
  1590. self.buttonWB.isEnabled = true
  1591. }
  1592. if !self.buttonRotate.isEnabled {
  1593. self.buttonRotate.isEnabled = true
  1594. }
  1595. } else {
  1596. self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
  1597. self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
  1598. self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
  1599. self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
  1600. if indexPerson! + 1 <= self.listRemoteViewFix.count {
  1601. let viewAfterRemote = self.listRemoteViewFix[indexPerson! + 1]
  1602. let viewAfterName = self.containerLabelName[indexPerson! + 1]
  1603. viewAfterRemote.frame.origin.y = viewAfterRemote.frame.origin.y - 170
  1604. viewAfterName.frame.origin.y = viewAfterName.frame.origin.y - 170
  1605. UIView.animate(withDuration: 0.35, animations: {
  1606. self.scrollRemoteView.layoutIfNeeded()
  1607. })
  1608. }
  1609. }
  1610. }
  1611. if !onGoingCC.isEmpty {
  1612. if let pin = arrayMessage.first, let index = self.users.firstIndex(of: User(pin: String(pin))) {
  1613. self.users.remove(at: index)
  1614. if !onGoingCC.isEmpty && self.users.count != 0 {
  1615. DispatchQueue.main.async {
  1616. var members = ""
  1617. for user in self.users {
  1618. if members.isEmpty {
  1619. members = "\(user.pin)"
  1620. } else {
  1621. members = ",\(user.pin)"
  1622. }
  1623. }
  1624. SecureUserDefaults.shared.set("\(members)", forKey: "membersCC")
  1625. }
  1626. }
  1627. }
  1628. }
  1629. self.dataPerson.remove(at: indexPerson!)
  1630. }
  1631. }
  1632. if (self.dataPerson.count == 1) {
  1633. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  1634. self.endAllCall()
  1635. if self.isInisiator && onGoingCC.isEmpty && !self.isPresent {
  1636. self.navigationController?.popViewController(animated: true)
  1637. } else {
  1638. self.dismiss(animated: true, completion: nil)
  1639. }
  1640. }
  1641. }
  1642. } else if (state == Nexilis.BUSY) {
  1643. DispatchQueue.main.async {
  1644. Nexilis.stopRingtoneCall()
  1645. Nexilis.stopRingbacktoneCall()
  1646. }
  1647. let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
  1648. DispatchQueue.main.async { [self] in
  1649. if (self.dataPerson.count == 1) {
  1650. self.makeStateCall()
  1651. if self.labelIncomingOutgoing.isDescendant(of: self.view) {
  1652. self.labelIncomingOutgoing.text = "Busy".localized()
  1653. }
  1654. if self.buttonDecline.isDescendant(of: self.view) {
  1655. self.buttonDecline.removeFromSuperview()
  1656. }
  1657. if self.buttonAccept.isDescendant(of: self.view) {
  1658. self.buttonAccept.removeFromSuperview()
  1659. }
  1660. } else {
  1661. let indexPerson = self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[0]})
  1662. if indexPerson != nil {
  1663. if (self.dataPerson.count == 2) {
  1664. self.containerLabelName.forEach({ $0.subviews.forEach({ $0.removeFromSuperview() }) })
  1665. self.scrollRemoteView.subviews.forEach({ $0.removeFromSuperview() })
  1666. if !self.buttonWB.isEnabled {
  1667. self.buttonWB.isEnabled = true
  1668. }
  1669. if !self.buttonRotate.isEnabled {
  1670. self.buttonRotate.isEnabled = true
  1671. }
  1672. } else {
  1673. self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
  1674. self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
  1675. self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
  1676. self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
  1677. if indexPerson! + 1 <= self.listRemoteViewFix.count {
  1678. let viewAfterRemote = self.listRemoteViewFix[indexPerson! + 1]
  1679. let viewAfterName = self.containerLabelName[indexPerson! + 1]
  1680. viewAfterRemote.frame.origin.y = viewAfterRemote.frame.origin.y - 170
  1681. viewAfterName.frame.origin.y = viewAfterName.frame.origin.y - 170
  1682. UIView.animate(withDuration: 0.35, animations: {
  1683. self.scrollRemoteView.layoutIfNeeded()
  1684. })
  1685. }
  1686. }
  1687. }
  1688. if !onGoingCC.isEmpty {
  1689. if let pin = arrayMessage.first, let index = self.users.firstIndex(of: User(pin: String(pin))) {
  1690. self.users.remove(at: index)
  1691. if !onGoingCC.isEmpty && users.count != 0 {
  1692. DispatchQueue.main.async {
  1693. var members = ""
  1694. for user in self.users {
  1695. if members.isEmpty {
  1696. members = "\(user.pin)"
  1697. } else {
  1698. members = ",\(user.pin)"
  1699. }
  1700. }
  1701. SecureUserDefaults.shared.set("\(members)", forKey: "membersCC")
  1702. }
  1703. }
  1704. }
  1705. }
  1706. if let indexP = indexPerson {
  1707. self.dataPerson.remove(at: indexP)
  1708. }
  1709. }
  1710. }
  1711. if (self.dataPerson.count == 1) {
  1712. DispatchQueue.main.async {
  1713. Nexilis.playBusyCall()
  1714. }
  1715. DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
  1716. Nexilis.stopBusyCall()
  1717. self.endAllCall()
  1718. if self.isInisiator && onGoingCC.isEmpty && !self.isPresent {
  1719. self.navigationController?.popViewController(animated: true)
  1720. } else {
  1721. self.dismiss(animated: true, completion: nil)
  1722. }
  1723. }
  1724. }
  1725. } else if (state == Nexilis.VIDEO_CALL_RINGING) {
  1726. DispatchQueue.main.async {
  1727. if (self.dataPerson.count > 1) {
  1728. if (self.dataPerson.count == 2) {
  1729. for i in 0...1{
  1730. self.scrollRemoteView.addSubview(self.listRemoteViewFix[i])
  1731. self.listRemoteViewFix[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 160)
  1732. self.listRemoteViewFix[i].backgroundColor = .white
  1733. self.listRemoteViewFix[i].makeRoundedView(radius: 8.0)
  1734. self.scrollRemoteView.addSubview(self.containerLabelName[i])
  1735. self.containerLabelName[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 30)
  1736. self.containerLabelName[i].backgroundColor = .orangeBNI.withAlphaComponent(0.5)
  1737. self.containerLabelName[i].makeRoundedView(radius: 8.0)
  1738. let pictureImage = self.dataPerson[i]["picture"] ?? ""
  1739. let namePerson = self.dataPerson[i]["name"] ?? ""
  1740. if (!pictureImage!.isEmpty) {
  1741. self.listRemoteViewFix[i].setImage(name: pictureImage!)
  1742. self.listRemoteViewFix[i].contentMode = .scaleAspectFill
  1743. } else {
  1744. self.listRemoteViewFix[i].image = UIImage(systemName: "person")
  1745. self.listRemoteViewFix[i].backgroundColor = UIColor.systemGray6
  1746. self.listRemoteViewFix[i].contentMode = .scaleAspectFit
  1747. }
  1748. let labelName = UILabel()
  1749. self.containerLabelName[i].addSubview(labelName)
  1750. 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)
  1751. labelName.text = namePerson
  1752. labelName.textAlignment = .center
  1753. labelName.textColor = .white
  1754. }
  1755. self.scrollRemoteView.contentSize.height = CGFloat(170 * 2)
  1756. } else {
  1757. let i = self.dataPerson.count - 1
  1758. self.scrollRemoteView.addSubview(self.listRemoteViewFix[i])
  1759. self.listRemoteViewFix[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 160)
  1760. self.listRemoteViewFix[i].backgroundColor = .white
  1761. self.listRemoteViewFix[i].makeRoundedView(radius: 8.0)
  1762. self.scrollRemoteView.addSubview(self.containerLabelName[i])
  1763. self.containerLabelName[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 30)
  1764. self.containerLabelName[i].backgroundColor = .orangeBNI.withAlphaComponent(0.5)
  1765. self.containerLabelName[i].makeRoundedView(radius: 8.0)
  1766. let pictureImage = self.dataPerson[self.dataPerson.count - 1]["picture"] ?? ""
  1767. let namePerson = self.dataPerson[self.dataPerson.count - 1]["name"] ?? ""
  1768. if (!pictureImage!.isEmpty) {
  1769. self.listRemoteViewFix[i].setImage(name: pictureImage!)
  1770. self.listRemoteViewFix[i].contentMode = .scaleAspectFill
  1771. } else {
  1772. self.listRemoteViewFix[i].image = UIImage(systemName: "person")
  1773. self.listRemoteViewFix[i].backgroundColor = UIColor.systemGray6
  1774. self.listRemoteViewFix[i].contentMode = .scaleAspectFit
  1775. }
  1776. self.scrollRemoteView.contentSize.height = CGFloat(170 * (i + 1))
  1777. let labelName = UILabel()
  1778. self.containerLabelName[i].addSubview(labelName)
  1779. 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)
  1780. labelName.text = namePerson
  1781. labelName.textAlignment = .center
  1782. labelName.textColor = .white
  1783. }
  1784. if self.buttonWB.isEnabled {
  1785. self.buttonWB.isEnabled = false
  1786. }
  1787. if self.buttonRotate.isEnabled {
  1788. self.buttonRotate.isEnabled = false
  1789. }
  1790. } else {
  1791. if self.dataPerson.count == 1 && !self.autoAcceptAPN {
  1792. DispatchQueue.main.async {
  1793. self.labelIncomingOutgoing.text = "Waiting for answer".localized()
  1794. }
  1795. }
  1796. }
  1797. }
  1798. }
  1799. }
  1800. }
  1801. extension QmeraVideoViewController : WhiteboardReceiver {
  1802. func incomingWB(roomId: String) {
  1803. //print("incoming wb")
  1804. self.wbTimer.invalidate()
  1805. if(wbRoomId.isEmpty){
  1806. //print("wbroom empty")
  1807. DispatchQueue.main.async {
  1808. self.wbTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.runTimer), userInfo: nil, repeats: true)
  1809. }
  1810. let me = User.getMyPin()!
  1811. var destString = ""
  1812. for d in dataPerson{
  1813. if d["f_pin"]!! == roomId.components(separatedBy: "wbvc")[0] {
  1814. continue
  1815. }
  1816. if destString.isEmpty{
  1817. destString = d["f_pin"]!!
  1818. } else {
  1819. destString = destString + ",\(d["f_pin"]!!)"
  1820. }
  1821. }
  1822. if destString.isEmpty {
  1823. SecureUserDefaults.shared.set("\(roomId.components(separatedBy: "wbvc")[0]),\(me)", forKey: "wb_vc")
  1824. } else {
  1825. SecureUserDefaults.shared.set("\(roomId.components(separatedBy: "wbvc")[0]),\(me),\(destString)", forKey: "wb_vc")
  1826. }
  1827. wbRoomId = roomId
  1828. }
  1829. }
  1830. func cancel(roomId: String) {
  1831. DispatchQueue.main.async {
  1832. self.wbTimer.invalidate()
  1833. self.wbBlink = false
  1834. self.buttonWB.backgroundColor = .lightGray
  1835. self.buttonWB.setNeedsDisplay()
  1836. }
  1837. wbRoomId = ""
  1838. }
  1839. @objc func runTimer(){
  1840. DispatchQueue.main.async {
  1841. if !self.buttonWB.isEnabled {
  1842. return
  1843. }
  1844. self.wbBlink = !self.wbBlink
  1845. if(self.wbBlink){
  1846. self.buttonWB.backgroundColor = .green
  1847. }
  1848. else {
  1849. //print("set wb blink off")
  1850. self.buttonWB.backgroundColor = .lightGray
  1851. }
  1852. self.buttonWB.setNeedsDisplay()
  1853. }
  1854. }
  1855. }