QmeraAudioViewController.swift 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. //
  2. // OutgoingViewController.swift
  3. // Qmera
  4. //
  5. // Created by Yayan Dwi on 07/10/21.
  6. //
  7. import UIKit
  8. import AVFoundation
  9. import nuSDKService
  10. import NotificationBannerSwift
  11. class QmeraAudioViewController: UIViewController {
  12. let stackViewToolbar2 = UIStackView()
  13. var onScreenConstraintWB = [NSLayoutConstraint]()
  14. let buttonWB = UIButton()
  15. let buttonChat = UIButton()
  16. var wbVC : WhiteboardViewController?
  17. var wbTimer = Timer()
  18. var wbBlink = false
  19. var wbRoomId = ""
  20. var callFCM = false
  21. let buttonSize: CGFloat = 70
  22. lazy var data: String = "" {
  23. didSet {
  24. getUserData { user in
  25. self.user = user
  26. }
  27. }
  28. }
  29. var user: User?
  30. var isAddCall = ""
  31. private var isEndByMe = false
  32. private var users: [User] = [] {
  33. didSet {
  34. DispatchQueue.main.async {
  35. if oldValue.count > self.users.count { // remove
  36. let remove = oldValue.filter { !self.users.contains($0) }
  37. remove.forEach { user in
  38. if let subviews = self.profiles.subviews as? [ProfileView] {
  39. subviews.forEach { p in
  40. if p.user == user {
  41. self.profiles.removeArrangeSubview(view: p)
  42. }
  43. }
  44. }
  45. }
  46. } else {
  47. if let user = self.users.last {
  48. let profile = ProfileView(image: UIImage(systemName: "person.circle.fill"))
  49. profile.user = user
  50. self.profiles.addArrangedSubview(view: profile)
  51. }
  52. }
  53. self.name.text = self.users.map { $0.fullName }.joined(separator: ", ")
  54. }
  55. }
  56. }
  57. var isOutgoing: Bool = true
  58. var isOnGoing: Bool = false
  59. var ticketId: String = ""
  60. private var timer: Timer?
  61. private var firstCall: Bool = true
  62. private var isSpeaker: Bool = false
  63. private var isMuted: Bool = false
  64. var listRemoteViewFix: [UIImageView] = [
  65. UIImageView(),
  66. UIImageView(),
  67. UIImageView(),
  68. UIImageView(),
  69. UIImageView()
  70. ]
  71. let zoomView = UIImageView()
  72. let cameraView = UIImageView()
  73. let status: UILabel = {
  74. let label = UILabel()
  75. label.text = "Calling..."
  76. label.font = UIFont.systemFont(ofSize: 14)
  77. label.textColor = .white
  78. label.textAlignment = .center
  79. return label
  80. }()
  81. let profiles: GroupView = {
  82. let groupView = GroupView()
  83. groupView.spacing = 50
  84. groupView.maxUser = 3
  85. return groupView
  86. }()
  87. let name: UILabel = {
  88. let label = UILabel()
  89. label.text = "uwitan"
  90. label.font = UIFont.systemFont(ofSize: 14)
  91. label.textColor = .white
  92. label.textAlignment = .center
  93. return label
  94. }()
  95. let end: UIButton = {
  96. let button = UIButton()
  97. button.setImage(UIImage(systemName: "phone.down"), for: .normal)
  98. button.imageView?.contentMode = .scaleAspectFit
  99. button.imageView?.tintColor = .white
  100. button.setBackgroundColor(.red, for: .normal)
  101. button.setBackgroundColor(.white, for: .highlighted)
  102. button.contentVerticalAlignment = .fill
  103. button.contentHorizontalAlignment = .fill
  104. button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
  105. return button
  106. }()
  107. let reject: UIButton = {
  108. let button = UIButton()
  109. let image = UIImage(systemName: "xmark")
  110. button.setImage(image, for: .normal)
  111. let selectedImage = image?.withTintColor(.mainColor)
  112. button.setImage(selectedImage, for: .selected)
  113. button.imageView?.contentMode = .scaleAspectFit
  114. button.imageView?.tintColor = .white
  115. button.setBackgroundColor(.red, for: .normal)
  116. button.setBackgroundColor(.white, for: .highlighted)
  117. button.contentVerticalAlignment = .fill
  118. button.contentHorizontalAlignment = .fill
  119. button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
  120. return button
  121. }()
  122. let accept: UIButton = {
  123. let button = UIButton()
  124. let image = UIImage(systemName: "checkmark")
  125. button.setImage(image, for: .normal)
  126. button.imageView?.contentMode = .scaleAspectFit
  127. button.imageView?.tintColor = .white
  128. button.setBackgroundColor(.greenColor, for: .normal)
  129. button.setBackgroundColor(.white, for: .highlighted)
  130. button.contentVerticalAlignment = .fill
  131. button.contentHorizontalAlignment = .fill
  132. button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
  133. return button
  134. }()
  135. let invite: UIButton = {
  136. let button = UIButton()
  137. let image = UIImage(systemName: "person.badge.plus")
  138. button.setImage(image, for: .normal)
  139. button.imageView?.contentMode = .scaleAspectFit
  140. button.imageView?.tintColor = .mainColor
  141. button.setBackgroundColor(.white, for: .normal)
  142. button.setBackgroundColor(.mainColor, for: .highlighted)
  143. button.contentVerticalAlignment = .fill
  144. button.contentHorizontalAlignment = .fill
  145. button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
  146. return button
  147. }()
  148. let speaker: UIButton = {
  149. let button = UIButton()
  150. button.setImage(UIImage(systemName: "speaker.slash")?.withTintColor(.mainColor, renderingMode: .alwaysOriginal), for: .normal)
  151. button.setImage(UIImage(systemName: "speaker.wave.3")?.withTintColor(.white, renderingMode: .alwaysOriginal), for: .selected)
  152. button.imageView?.contentMode = .scaleAspectFit
  153. button.setBackgroundColor(.white, for: .normal)
  154. button.setBackgroundColor(.mainColor, for: .highlighted)
  155. button.setBackgroundColor(.mainColor, for: .selected)
  156. button.contentVerticalAlignment = .fill
  157. button.contentHorizontalAlignment = .fill
  158. button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
  159. return button
  160. }()
  161. let mic: UIButton = {
  162. let button = UIButton()
  163. button.setImage(UIImage(systemName: "mic")?.withTintColor(.mainColor, renderingMode: .alwaysOriginal), for: .normal)
  164. button.setImage(UIImage(systemName: "mic.slash")?.withTintColor(.white, renderingMode: .alwaysOriginal), for: .selected)
  165. button.imageView?.contentMode = .scaleAspectFit
  166. button.setBackgroundColor(.white, for: .normal)
  167. button.setBackgroundColor(.mainColor, for: .highlighted)
  168. button.setBackgroundColor(.mainColor, for: .selected)
  169. button.contentVerticalAlignment = .fill
  170. button.contentHorizontalAlignment = .fill
  171. button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
  172. return button
  173. }()
  174. let stack: UIStackView = {
  175. let stackView = UIStackView()
  176. stackView.axis = .horizontal
  177. stackView.distribution = .fillEqually
  178. return stackView
  179. }()
  180. let stack2: UIStackView = {
  181. let stackView = UIStackView()
  182. stackView.axis = .horizontal
  183. stackView.distribution = .fillEqually
  184. return stackView
  185. }()
  186. let poweredByView: UIStackView = {
  187. let stackView = UIStackView()
  188. stackView.axis = .horizontal
  189. stackView.spacing = 5
  190. return stackView
  191. }()
  192. let poweredByLabel: UILabel = {
  193. let label = UILabel()
  194. label.text = "Powered by Nexilis".localized()
  195. return label
  196. }()
  197. let qmeraLogo: UIButton = {
  198. let image = UIImage(named: "Q-Button-PNG", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  199. let button = UIButton()
  200. button.setImage(image, for: .normal)
  201. button.imageView?.contentMode = .scaleAspectFit
  202. button.imageEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
  203. button.contentVerticalAlignment = .fill
  204. button.contentHorizontalAlignment = .fill
  205. // button.frame.size.width = 30
  206. // button.frame.size.height = 30
  207. return button
  208. }()
  209. let nexilisLogo: UIButton = {
  210. let image = UIImage(named: "pb_powered_button", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  211. let button = UIButton()
  212. button.setImage(image, for: .normal)
  213. button.imageView?.contentMode = .scaleAspectFit
  214. button.imageEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
  215. button.contentVerticalAlignment = .fill
  216. button.contentHorizontalAlignment = .fill
  217. // button.frame.size.width = 30
  218. // button.frame.size.height = 30
  219. return button
  220. }()
  221. override func viewWillDisappear(_ animated: Bool) {
  222. UIDevice.current.isProximityMonitoringEnabled = false
  223. NotificationCenter.default.removeObserver(self)
  224. Nexilis.floatingButton.isHidden = false
  225. }
  226. deinit {
  227. UIDevice.current.isProximityMonitoringEnabled = false
  228. NotificationCenter.default.removeObserver(self)
  229. Nexilis.floatingButton.isHidden = false
  230. }
  231. override func viewDidAppear(_ animated: Bool) {
  232. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "onShowAC"), object: nil, userInfo: nil)
  233. }
  234. override func viewDidLoad() {
  235. super.viewDidLoad()
  236. Nexilis.floatingButton.isHidden = true
  237. let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .systemUltraThinMaterialDark))
  238. effectView.frame = view.frame
  239. view.insertSubview(effectView, at: 0)
  240. view.addSubview(status)
  241. view.addSubview(profiles)
  242. view.addSubview(name)
  243. status.anchor(left: view.leftAnchor, bottom: profiles.topAnchor, right: view.rightAnchor, paddingBottom: 30, centerX: view.centerXAnchor)
  244. profiles.anchor(centerX: view.centerXAnchor, centerY: view.centerYAnchor, width: 150, height: 150)
  245. name.anchor(top: profiles.bottomAnchor, left: view.leftAnchor, right: view.rightAnchor, paddingTop: 5, paddingLeft: 20, paddingRight: 20, centerX: view.centerXAnchor)
  246. definesPresentationContext = true
  247. if isOutgoing {
  248. outgoingView()
  249. } else if isOnGoing {
  250. ongoingView()
  251. } else {
  252. incomingView()
  253. }
  254. UIDevice.current.isProximityMonitoringEnabled = true
  255. NotificationCenter.default.addObserver(self, selector: #selector(onStatusCall(_:)), name: NSNotification.Name(rawValue: Nexilis.listenerStatusCall), object: nil)
  256. NotificationCenter.default.addObserver(self, selector: #selector(onReceiveMessage(notification:)), name: NSNotification.Name(rawValue: Nexilis.listenerReceiveChat), object: nil)
  257. NotificationCenter.default.addObserver(self, selector: #selector(onCallFCM(notification:)), name: NSNotification.Name(rawValue: Nexilis.callFCM), object: nil)
  258. if let u = self.user {
  259. self.users.append(u)
  260. if isOutgoing && ticketId.isEmpty {
  261. // if onGoingCC.isEmpty {
  262. // Nexilis.shared.callManager.startCall(handle: u.pin)
  263. // } else {
  264. // API.initiateCCall(sParty: u.pin)
  265. // }
  266. if callFCM {
  267. DispatchQueue.global().async {
  268. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getCalling(fPin: u.pin, type: "1"), timeout: 30 * 1000) {
  269. if response.isOk() {
  270. } else {
  271. DispatchQueue.main.async {
  272. self.status.text = "Busy"
  273. self.end.isEnabled = false
  274. }
  275. }
  276. } else {
  277. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  278. imageView.tintColor = .white
  279. 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)
  280. banner.show()
  281. }
  282. }
  283. } else {
  284. Nexilis.ringbacktonePlayer?.play()
  285. API.initiateCCall(sParty: u.pin)
  286. }
  287. } else if !ticketId.isEmpty {
  288. if isOutgoing {
  289. API.ccs(sTicketID: ticketId, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView, bCameraOn: false)
  290. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getIncomingCallCS(f_pin_opposite: u.pin), timeout: 30 * 1000){
  291. if response.mBodies[CoreMessage_TMessageKey.ERRCOD] != "01" {
  292. self.didEnd(sender: true)
  293. }
  294. }
  295. } else {
  296. API.csa(sTicketID: ticketId, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView, bCameraOn: false)
  297. }
  298. }
  299. }
  300. }
  301. @objc func onCallFCM(notification: NSNotification) {
  302. DispatchQueue.main.async {
  303. let data:[AnyHashable : Any] = notification.userInfo!
  304. if let l_pin = data["l_pin"] as? String {
  305. Nexilis.ringbacktonePlayer?.play()
  306. API.initiateCCall(sParty: l_pin)
  307. }
  308. }
  309. }
  310. override func viewWillLayoutSubviews() {
  311. super.viewWillLayoutSubviews()
  312. end.circle()
  313. reject.circle()
  314. accept.circle()
  315. invite.circle()
  316. speaker.circle()
  317. mic.circle()
  318. }
  319. private func getUserData(completion: @escaping (User?) -> ()) {
  320. if let user = self.user {
  321. completion(user)
  322. return
  323. }
  324. var user: User?
  325. DispatchQueue.global().async {
  326. Database.shared.database?.inTransaction({ fmdb, rollback in
  327. do {
  328. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select f_pin, first_name, last_name, image_id from BUDDY where f_pin = '\(self.data)'"), cursor.next() {
  329. user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  330. firstName: cursor.string(forColumnIndex: 1) ?? "",
  331. lastName: cursor.string(forColumnIndex: 2) ?? "",
  332. thumb: cursor.string(forColumnIndex: 3) ?? "")
  333. cursor.close()
  334. }
  335. } catch {
  336. rollback.pointee = true
  337. print("Access database error: \(error.localizedDescription)")
  338. }
  339. })
  340. }
  341. completion(user)
  342. }
  343. private func outgoingView() {
  344. view.addSubview(end)
  345. end.anchor(bottom: view.bottomAnchor, paddingBottom: 60, centerX: view.centerXAnchor, width: buttonSize, height: buttonSize)
  346. end.addTarget(self, action: #selector(didPressEnd(sender:)), for: .touchUpInside)
  347. }
  348. private func incomingView() {
  349. status.text = "Incoming..."
  350. stack.spacing = buttonSize
  351. view.addSubview(stack)
  352. stack.anchor(bottom: view.bottomAnchor, paddingBottom: 60, centerX: view.centerXAnchor, width: buttonSize * 3, height: buttonSize)
  353. stack.addArrangedSubview(reject)
  354. stack.addArrangedSubview(accept)
  355. reject.addTarget(self, action: #selector(didReject(sender:)), for: .touchUpInside)
  356. accept.addTarget(self, action: #selector(didAccept(sender:)), for: .touchUpInside)
  357. }
  358. private func ongoingView() {
  359. status.text = "Connecting..."
  360. stack.spacing = buttonSize / 2
  361. stack2.spacing = buttonSize / 2
  362. view.addSubview(stack)
  363. view.addSubview(stack2)
  364. stack.anchor(bottom: view.bottomAnchor, paddingBottom: 60, centerX: view.centerXAnchor, width: buttonSize * 4, height: buttonSize)
  365. stack2.anchor(bottom: stack.topAnchor, paddingBottom: 10, centerX: view.centerXAnchor, width: buttonSize, height: buttonSize)
  366. stack.addArrangedSubview(invite)
  367. stack.addArrangedSubview(end)
  368. stack.addArrangedSubview(speaker)
  369. stack2.addArrangedSubview(mic)
  370. invite.addTarget(self, action: #selector(didInvite(sender:)), for: .touchUpInside)
  371. end.addTarget(self, action: #selector(didPressEnd(sender:)), for: .touchUpInside)
  372. speaker.addTarget(self, action: #selector(didSpeaker(sender:)), for: .touchUpInside)
  373. mic.addTarget(self, action: #selector(didMute(sender:)), for: .touchUpInside)
  374. if !ticketId.isEmpty {
  375. self.view.addSubview(self.stackViewToolbar2)
  376. self.stackViewToolbar2.translatesAutoresizingMaskIntoConstraints = false
  377. NSLayoutConstraint.activate([
  378. self.stackViewToolbar2.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
  379. self.stackViewToolbar2.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 10.0)
  380. ])
  381. self.stackViewToolbar2.axis = .vertical
  382. self.stackViewToolbar2.distribution = .equalSpacing
  383. self.stackViewToolbar2.alignment = .center
  384. self.stackViewToolbar2.spacing = 5
  385. view.addSubview(buttonWB)
  386. buttonWB.translatesAutoresizingMaskIntoConstraints = false
  387. buttonWB.frame.size = CGSize(width: 40.0, height: 40.0)
  388. NSLayoutConstraint.activate([
  389. buttonWB.widthAnchor.constraint(equalToConstant: 40.0),
  390. buttonWB.heightAnchor.constraint(equalToConstant: 40.0)
  391. ])
  392. buttonWB.backgroundColor = .lightGray
  393. buttonWB.setImage(UIImage(systemName: "ipad.landscape", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .medium, scale: .default)), for: .normal)
  394. buttonWB.circle()
  395. buttonWB.tintColor = .black
  396. buttonWB.addTarget(self, action: #selector(didTapWBButton), for: .touchUpInside)
  397. view.addSubview(buttonChat)
  398. buttonChat.translatesAutoresizingMaskIntoConstraints = false
  399. buttonChat.frame.size = CGSize(width: 40.0, height: 40.0)
  400. NSLayoutConstraint.activate([
  401. buttonChat.widthAnchor.constraint(equalToConstant: 40.0),
  402. buttonChat.heightAnchor.constraint(equalToConstant: 40.0)
  403. ])
  404. buttonChat.backgroundColor = .lightGray
  405. buttonChat.setImage(UIImage(systemName: "bubble.right", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .medium, scale: .default)), for: .normal)
  406. buttonChat.circle()
  407. buttonChat.tintColor = .black
  408. buttonChat.addTarget(self, action: #selector(didTapChatButton), for: .touchUpInside)
  409. }
  410. self.view.addSubview(poweredByView)
  411. self.poweredByView.translatesAutoresizingMaskIntoConstraints = false
  412. let constraintRightPowered = self.poweredByView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -10.0)
  413. let constraintBottomPowered = self.poweredByView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10.0)
  414. NSLayoutConstraint.activate([
  415. constraintRightPowered,
  416. constraintBottomPowered,
  417. nexilisLogo.widthAnchor.constraint(equalToConstant: 30.0),
  418. nexilisLogo.heightAnchor.constraint(equalToConstant: 30.0)
  419. ])
  420. poweredByView.addArrangedSubview(poweredByLabel)
  421. poweredByView.addArrangedSubview(nexilisLogo)
  422. stackViewToolbar2.addArrangedSubview(buttonWB)
  423. stackViewToolbar2.addArrangedSubview(buttonChat)
  424. }
  425. // MARK: - Action
  426. @objc func didTapChatButton(){
  427. let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
  428. let members: String = SecureUserDefaults.shared.value(forKey: "membersCC") ?? ""
  429. let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
  430. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  431. editorPersonalVC.hidesBottomBarWhenPushed = true
  432. editorPersonalVC.unique_l_pin = officer
  433. editorPersonalVC.fromNotification = true
  434. editorPersonalVC.isContactCenter = true
  435. editorPersonalVC.fPinContacCenter = members
  436. editorPersonalVC.complaintId = ticketId
  437. editorPersonalVC.onGoingCC = true
  438. editorPersonalVC.isRequestContactCenter = false
  439. editorPersonalVC.users = users
  440. editorPersonalVC.fromVCAC = true
  441. let navigationController = CustomNavigationController(rootViewController: editorPersonalVC)
  442. navigationController.modalPresentationStyle = .overCurrentContext
  443. navigationController.navigationBar.tintColor = .white
  444. navigationController.navigationBar.barTintColor = self.traitCollection.userInterfaceStyle == .dark ? .blackDarkMode : .mainColor
  445. navigationController.navigationBar.isTranslucent = false
  446. navigationController.navigationBar.overrideUserInterfaceStyle = .dark
  447. navigationController.navigationBar.barStyle = .black
  448. let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]
  449. UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, for: .normal)
  450. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  451. navigationController.navigationBar.titleTextAttributes = textAttributes
  452. if UIApplication.shared.visibleViewController?.navigationController != nil {
  453. UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
  454. } else {
  455. UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
  456. }
  457. }
  458. @objc func didTapWBButton(){
  459. if(wbVC == nil){
  460. wbVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "wbVC") as? WhiteboardViewController
  461. if(wbRoomId.isEmpty){
  462. let me = User.getMyPin()!
  463. let tid = CoreMessage_TMessageUtil.getTID()
  464. wbRoomId = "\(me)wbvc\(tid)"
  465. wbVC!.roomId = wbRoomId
  466. var destinations = [String]()
  467. var destString = ""
  468. for d in users{
  469. destinations.append(d.pin)
  470. if destString.isEmpty{
  471. destString = d.pin
  472. } else {
  473. destString = destString + ",\(d.pin)"
  474. }
  475. }
  476. wbVC!.destinations = destinations
  477. wbVC!.sendInit()
  478. SecureUserDefaults.shared.set("\(me),\(destString)", forKey: "wb_vc")
  479. }
  480. else {
  481. self.wbTimer.invalidate()
  482. self.buttonWB.backgroundColor = .lightGray
  483. wbVC!.roomId = wbRoomId
  484. wbVC!.sendJoin()
  485. }
  486. }
  487. wbVC!.close = {
  488. DispatchQueue.main.async {
  489. if self.wbVC!.view.isDescendant(of: self.view){
  490. self.wbVC!.view.removeFromSuperview()
  491. }
  492. // self.buttonDecline.isHidden = false
  493. // self.buttonSpeaker.isHidden = false
  494. // self.buttonAddParticipant.isHidden = false
  495. // self.buttonRotate.isHidden = false
  496. // if(!self.wbRoomId.isEmpty){
  497. // DispatchQueue.main.async {
  498. // self.wbTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.runTimer), userInfo: nil, repeats: true)
  499. // }
  500. // }
  501. }
  502. }
  503. // self.buttonDecline.isHidden = true
  504. // self.buttonSpeaker.isHidden = true
  505. // self.buttonAddParticipant.isHidden = true
  506. // self.buttonRotate.isHidden = true
  507. addChild(wbVC!)
  508. wbVC!.view.translatesAutoresizingMaskIntoConstraints = false
  509. view.addSubview(wbVC!.view)
  510. onScreenConstraintWB = [
  511. wbVC!.view.topAnchor.constraint(equalTo: self.view.topAnchor),
  512. wbVC!.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
  513. wbVC!.view.rightAnchor.constraint(equalTo: self.view.rightAnchor),
  514. wbVC!.view.leftAnchor.constraint(equalTo: self.view.leftAnchor),
  515. ]
  516. NSLayoutConstraint.activate(onScreenConstraintWB)
  517. // Notify the child view controller that the move is complete.
  518. wbVC!.didMove(toParent: self)
  519. // self.navigationController?.setNavigationBarHidden(false, animated: true)
  520. // controller.modalPresentationStyle = .overCurrentContext
  521. // self.navigationController?.present(controller, animated: true)
  522. }
  523. @objc func didSpeaker(sender: Any?) {
  524. isSpeaker = !isSpeaker
  525. speaker.isSelected = isSpeaker
  526. if isSpeaker {
  527. UIDevice.current.isProximityMonitoringEnabled = false
  528. } else {
  529. UIDevice.current.isProximityMonitoringEnabled = true
  530. }
  531. Nexilis.setSpeaker(isSpeaker)
  532. }
  533. @objc func didMute(sender: Any?) {
  534. isMuted = !isMuted
  535. mic.isSelected = isMuted
  536. API.mmc(int: 1, boolean: isMuted)
  537. }
  538. @objc func didInvite(sender: Any?) {
  539. let controller = QmeraCallContactViewController()
  540. controller.isDismiss = { user in
  541. let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
  542. if !onGoingCC.isEmpty {
  543. DispatchQueue.global().async {
  544. _ = Nexilis.write(message: CoreMessage_TMessageBank.getCCRoomInvite(l_pin: user.pin, ticket_id: onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2], channel: "1"))
  545. }
  546. DispatchQueue.main.async {
  547. self.isAddCall = user.pin
  548. }
  549. } else {
  550. self.users.append(user)
  551. // Start Calling
  552. // Nexilis.shared.callManager.startCall(handle: user.pin)
  553. API.initiateCCall(sParty: user.pin)
  554. }
  555. }
  556. controller.selectedUser.append(contentsOf: users)
  557. present(CustomNavigationController(rootViewController: controller), animated: true, completion: nil)
  558. }
  559. @objc func didPressEnd(sender: Any?) {
  560. let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
  561. if !onGoingCC.isEmpty {
  562. self.isEndByMe = true
  563. self.didEnd(sender: nil)
  564. return
  565. }
  566. let alert = LibAlertController(title: "End Audio Call".localized(), message: "Are you sure you want to end audio call?".localized(), preferredStyle: .alert)
  567. alert.addAction(UIAlertAction(title: "No".localized(), style: UIAlertAction.Style.default, handler: nil))
  568. alert.addAction(UIAlertAction(title: "Yes".localized(), style: UIAlertAction.Style.default, handler: {(_) in
  569. DispatchQueue.main.async {
  570. self.timer?.invalidate()
  571. self.timer = nil
  572. self.status.text = "Audio Call Ended".localized()
  573. self.end.isEnabled = false
  574. self.invite.isEnabled = false
  575. self.speaker.isEnabled = false
  576. self.mic.isEnabled = false
  577. }
  578. self.isEndByMe = true
  579. self.didEnd(sender: nil)
  580. }))
  581. self.present(alert, animated: true, completion: nil)
  582. }
  583. @objc func didEnd(sender: Any?) {
  584. poweredByView.isHidden = true
  585. let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
  586. if !onGoingCC.isEmpty {
  587. if sender != nil && sender is Bool {
  588. let controller = self.presentedViewController
  589. if controller != nil {
  590. controller!.dismiss(animated: true)
  591. }
  592. self.dismiss(animated: false, completion: nil)
  593. let requester = onGoingCC.components(separatedBy: ",")[0]
  594. let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
  595. let complaintId = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2]
  596. let startTimeCC: String = SecureUserDefaults.shared.value(forKey: "startTimeCC") ?? ""
  597. DispatchQueue.global().async {
  598. if sender as! Bool == true {
  599. let date = "\(Date().currentTimeMillis())"
  600. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  601. do {
  602. _ = try Database.shared.insertRecord(fmdb: fmdb, table: "CALL_CENTER_HISTORY", cvalues: [
  603. "type" : "1",
  604. "title" : "Contact Center".localized(),
  605. "time" : startTimeCC,
  606. "f_pin" : officer,
  607. "data" : complaintId,
  608. "time_end" : date,
  609. "complaint_id" : complaintId,
  610. "members" : "",
  611. "requester": requester
  612. ], replace: true)
  613. } catch {
  614. rollback.pointee = true
  615. print("Access database error: \(error.localizedDescription)")
  616. }
  617. })
  618. }
  619. SecureUserDefaults.shared.removeValue(forKey: "onGoingCC")
  620. SecureUserDefaults.shared.removeValue(forKey: "membersCC")
  621. SecureUserDefaults.shared.removeValue(forKey: "startTimeCC")
  622. SecureUserDefaults.shared.removeValue(forKey: "waitingRequestCC")
  623. }
  624. return
  625. }
  626. 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)
  627. alert.addAction(UIAlertAction(title: "No".localized(), style: UIAlertAction.Style.default, handler: nil))
  628. alert.addAction(UIAlertAction(title: "Yes".localized(), style: UIAlertAction.Style.default, handler: {(_) in
  629. self.dismiss(animated: false, completion: nil)
  630. let requester = onGoingCC.components(separatedBy: ",")[0]
  631. let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
  632. let complaintId = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2]
  633. let idMe = User.getMyPin()!
  634. let startTimeCC: String = SecureUserDefaults.shared.value(forKey: "startTimeCC") ?? ""
  635. DispatchQueue.global().async {
  636. let date = "\(Date().currentTimeMillis())"
  637. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  638. do {
  639. _ = try Database.shared.insertRecord(fmdb: fmdb, table: "CALL_CENTER_HISTORY", cvalues: [
  640. "type" : "1",
  641. "title" : "Contact Center".localized(),
  642. "time" : startTimeCC,
  643. "f_pin" : officer,
  644. "data" : complaintId,
  645. "time_end" : date,
  646. "complaint_id" : complaintId,
  647. "members" : "",
  648. "requester": requester
  649. ], replace: true)
  650. } catch {
  651. rollback.pointee = true
  652. print("Access database error: \(error.localizedDescription)")
  653. }
  654. })
  655. if officer == idMe {
  656. _ = Nexilis.write(message: CoreMessage_TMessageBank.endCallCenter(complaint_id: complaintId, l_pin: requester))
  657. } else {
  658. if requester == idMe {
  659. _ = Nexilis.write(message: CoreMessage_TMessageBank.endCallCenter(complaint_id: complaintId, l_pin: officer))
  660. } else {
  661. _ = Nexilis.write(message: CoreMessage_TMessageBank.leaveCCRoomInvite(ticket_id: complaintId))
  662. }
  663. }
  664. SecureUserDefaults.shared.removeValue(forKey: "onGoingCC")
  665. SecureUserDefaults.shared.removeValue(forKey: "membersCC")
  666. SecureUserDefaults.shared.removeValue(forKey: "startTimeCC")
  667. SecureUserDefaults.shared.removeValue(forKey: "waitingRequestCC")
  668. }
  669. // if let user = self.user, let call = Nexilis.shared.callManager.call(with: user.pin) {
  670. // Nexilis.shared.callManager.end(call: call)
  671. // } else {
  672. API.terminateCall(sParty: nil)
  673. // }
  674. }))
  675. self.present(alert, animated: true, completion: nil)
  676. } else {
  677. let controller = self.presentedViewController
  678. if controller != nil {
  679. controller!.dismiss(animated: true)
  680. }
  681. if isEndByMe {
  682. // for i in 0..<Nexilis.shared.callManager.calls.count {
  683. // Nexilis.shared.callManager.end(call: Nexilis.shared.callManager.calls[i])
  684. // }
  685. API.terminateCall(sParty: nil)
  686. DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
  687. self.dismiss(animated: false, completion: nil)
  688. }
  689. } else {
  690. // if let user = self.user, let call = Nexilis.shared.callManager.call(with: user.pin) {
  691. // Nexilis.shared.callManager.end(call: call)
  692. // } else {
  693. API.terminateCall(sParty: nil)
  694. // }
  695. self.dismiss(animated: false, completion: nil)
  696. }
  697. }
  698. }
  699. @objc func didReject(sender: Any?) {
  700. if isOutgoing {
  701. Nexilis.ringbacktonePlayer?.stop()
  702. } else {
  703. Nexilis.ringtonePlayer?.stop()
  704. }
  705. didEnd(sender: sender)
  706. }
  707. @objc func didAccept(sender: Any?) {
  708. if isOutgoing {
  709. Nexilis.ringbacktonePlayer?.stop()
  710. } else {
  711. Nexilis.ringtonePlayer?.stop()
  712. }
  713. NSLayoutConstraint.deactivate(stack.constraints)
  714. stack.subviews.forEach { subview in
  715. subview.removeFromSuperview()
  716. }
  717. ongoingView()
  718. UIView.animate(withDuration: 0.3, animations: {
  719. self.view.layoutIfNeeded()
  720. })
  721. API.receiveCCall(sParty: user?.pin)
  722. }
  723. // MARK: - Communication
  724. @objc func onReceiveMessage(notification: NSNotification) {
  725. DispatchQueue.main.async {
  726. let data:[AnyHashable : Any] = notification.userInfo!
  727. if let dataMessage = data["message"] as? TMessage {
  728. if (dataMessage.getCode() == CoreMessage_TMessageCode.PUSH_MEMBER_ROOM_CONTACT_CENTER) {
  729. let data = dataMessage.getBody(key: CoreMessage_TMessageKey.DATA)
  730. if !data.isEmpty {
  731. if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions()) as? [AnyObject] {
  732. var members = ""
  733. let idMe = User.getMyPin()!
  734. for json in jsonArray {
  735. if "\(json)" != idMe {
  736. if members.isEmpty {
  737. members = "\(json)"
  738. } else {
  739. members += ",\(json)"
  740. }
  741. }
  742. }
  743. SecureUserDefaults.shared.set(members, forKey: "inEditorPersonal")
  744. SecureUserDefaults.shared.set("\(members)", forKey: "membersCC")
  745. }
  746. }
  747. self.users.append(User.getData(pin: dataMessage.getPIN())!)
  748. }
  749. }
  750. }
  751. }
  752. @objc func onStatusCall(_ notification: NSNotification) {
  753. if let data = notification.userInfo,
  754. let state = data["state"] as? Int,
  755. let message = data["message"] as? String
  756. {
  757. let arrayMessage = message.split(separator: ",")
  758. if state == Nexilis.AUDIO_VIDEO_CALL_MUTED {
  759. DispatchQueue.main.async { [self] in
  760. if let pin = arrayMessage.first, let index = users.firstIndex(of: User(pin: String(pin))) {
  761. if arrayMessage[1] == "1" {
  762. users[index].isMuted = true
  763. if let profile = profiles.subviews[index] as? ProfileView {
  764. profile.imageMuted.isHidden = false
  765. }
  766. } else {
  767. users[index].isMuted = false
  768. if let profile = profiles.subviews[index] as? ProfileView {
  769. profile.imageMuted.isHidden = true
  770. }
  771. }
  772. }
  773. }
  774. } else if state == Nexilis.AUDIO_CALL_RINGING || (!ticketId.isEmpty && state == Nexilis.VIDEO_CALL_RINGING) {
  775. if users.count == 1 {
  776. DispatchQueue.main.async {
  777. self.status.text = "Ringing..."
  778. }
  779. }
  780. } else if state == Nexilis.AUDIO_CALL_OFFHOOK || (!ticketId.isEmpty && state == Nexilis.VIDEO_CALL_OFFHOOK) {
  781. if isOutgoing {
  782. Nexilis.ringbacktonePlayer?.stop()
  783. }
  784. if users.count == 1 && firstCall {
  785. DispatchQueue.main.async {
  786. if !self.ticketId.isEmpty {
  787. NSLayoutConstraint.deactivate(self.stack.constraints)
  788. self.stack.subviews.forEach { subview in
  789. subview.removeFromSuperview()
  790. }
  791. UIView.animate(withDuration: 0.3, animations: {
  792. self.view.layoutIfNeeded()
  793. })
  794. }
  795. self.ongoingView()
  796. let connectDate = Date()
  797. self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
  798. let format = Utils.callDurationFormatter.string(from: Date().timeIntervalSince(connectDate))
  799. self.status.text = format
  800. }
  801. self.timer?.fire()
  802. self.firstCall = false
  803. }
  804. }
  805. if (!isOutgoing || !firstCall), users.count >= 1, let user = User.getData(pin: String(arrayMessage[1])), !users.contains(user) {
  806. self.users.append(user)
  807. }
  808. } else if state == Nexilis.AUDIO_CALL_END || (!ticketId.isEmpty && state == Nexilis.VIDEO_CALL_END) {
  809. if isOutgoing {
  810. Nexilis.ringbacktonePlayer?.stop()
  811. } else {
  812. Nexilis.ringtonePlayer?.stop()
  813. }
  814. let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
  815. if let pin = arrayMessage.first, let index = users.firstIndex(of: User(pin: String(pin))) {
  816. users.remove(at: index)
  817. if !onGoingCC.isEmpty && users.count != 0 {
  818. let requester = onGoingCC.components(separatedBy: ",")[0]
  819. let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
  820. if pin == requester || pin == officer {
  821. DispatchQueue.main.async {
  822. if !self.end.isEnabled {
  823. return
  824. }
  825. if self.buttonWB.isDescendant(of: self.view){
  826. self.buttonWB.removeFromSuperview()
  827. }
  828. if self.buttonChat.isDescendant(of: self.view){
  829. self.buttonChat.removeFromSuperview()
  830. }
  831. if self.viewIfLoaded?.window != nil {
  832. let imageView = UIImageView(image: UIImage(systemName: "info.circle"))
  833. imageView.tintColor = .white
  834. 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)
  835. banner.show()
  836. }
  837. self.timer?.invalidate()
  838. self.timer = nil
  839. self.status.text = "Call Center Session has ended..."
  840. self.end.isEnabled = false
  841. }
  842. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  843. self.didEnd(sender: true)
  844. }
  845. return
  846. }
  847. } else if !onGoingCC.isEmpty && users.count == 0 {
  848. DispatchQueue.main.async {
  849. if !self.end.isEnabled {
  850. return
  851. }
  852. if self.buttonWB.isDescendant(of: self.view){
  853. self.buttonWB.removeFromSuperview()
  854. }
  855. if self.buttonChat.isDescendant(of: self.view){
  856. self.buttonChat.removeFromSuperview()
  857. }
  858. if self.viewIfLoaded?.window != nil {
  859. let imageView = UIImageView(image: UIImage(systemName: "info.circle"))
  860. imageView.tintColor = .white
  861. 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)
  862. banner.show()
  863. }
  864. self.timer?.invalidate()
  865. self.timer = nil
  866. self.status.text = "Call Center Session has ended..."
  867. self.end.isEnabled = false
  868. }
  869. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  870. self.didEnd(sender: true)
  871. }
  872. return
  873. } else if users.count == 0 {
  874. DispatchQueue.main.async {
  875. self.timer?.invalidate()
  876. self.timer = nil
  877. self.status.text = "Audio Call Ended".localized()
  878. self.end.isEnabled = false
  879. self.invite.isEnabled = false
  880. self.speaker.isEnabled = false
  881. self.mic.isEnabled = false
  882. let controller = self.presentedViewController
  883. if controller != nil {
  884. controller!.dismiss(animated: true)
  885. }
  886. }
  887. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  888. self.didEnd(sender: true)
  889. }
  890. return
  891. }
  892. DispatchQueue.main.async{ [self] in
  893. if users.count == 1 && !buttonWB.isEnabled {
  894. buttonWB.isEnabled = true
  895. }
  896. }
  897. }
  898. // if users.count == 0 {
  899. // DispatchQueue.main.async {
  900. // self.dismiss(animated: false, completion: nil)
  901. // }
  902. // }
  903. } else if state == Nexilis.OFFLINE { // Offline
  904. if isOutgoing {
  905. Nexilis.ringbacktonePlayer?.stop()
  906. } else {
  907. Nexilis.ringtonePlayer?.stop()
  908. }
  909. let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
  910. if let pin = arrayMessage.first, let index = users.firstIndex(of: User(pin: String(pin))) {
  911. users.remove(at: index)
  912. if !onGoingCC.isEmpty && users.count != 0 {
  913. DispatchQueue.main.async {
  914. var members = ""
  915. for user in self.users {
  916. if members.isEmpty {
  917. members = "\(user.pin)"
  918. } else {
  919. members = ",\(user.pin)"
  920. }
  921. }
  922. SecureUserDefaults.shared.set("\(members)", forKey: "membersCC")
  923. }
  924. }
  925. DispatchQueue.main.async { [self] in
  926. if users.count == 1 && !buttonWB.isEnabled {
  927. buttonWB.isEnabled = true
  928. }
  929. }
  930. }
  931. if users.count == 0 {
  932. DispatchQueue.main.async {
  933. self.status.text = "Offline..."
  934. self.end.isEnabled = false
  935. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  936. self.didEnd(sender: false)
  937. }
  938. }
  939. }
  940. } else if state == Nexilis.BUSY { // Busy
  941. if isOutgoing {
  942. Nexilis.ringbacktonePlayer?.stop()
  943. } else {
  944. Nexilis.ringtonePlayer?.stop()
  945. }
  946. let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
  947. if let pin = arrayMessage.first, let index = users.firstIndex(of: User(pin: String(pin))) {
  948. users.remove(at: index)
  949. if !onGoingCC.isEmpty && users.count != 0 {
  950. DispatchQueue.main.async {
  951. var members = ""
  952. for user in self.users {
  953. if members.isEmpty {
  954. members = "\(user.pin)"
  955. } else {
  956. members = ",\(user.pin)"
  957. }
  958. }
  959. SecureUserDefaults.shared.set("\(members)", forKey: "membersCC")
  960. }
  961. }
  962. DispatchQueue.main.async { [self] in
  963. if users.count == 1 && !buttonWB.isEnabled {
  964. buttonWB.isEnabled = true
  965. }
  966. }
  967. }
  968. if users.count == 0 {
  969. DispatchQueue.main.async {
  970. self.status.text = "Busy..."
  971. self.end.isEnabled = false
  972. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  973. self.didEnd(sender: false)
  974. }
  975. }
  976. }
  977. }
  978. }
  979. }
  980. }
  981. extension QmeraAudioViewController : WhiteboardReceiver {
  982. func incomingWB(roomId: String) {
  983. //print("incoming wb")
  984. self.wbTimer.invalidate()
  985. if(wbRoomId.isEmpty){
  986. //print("wbroom empty")
  987. DispatchQueue.main.async {
  988. self.wbTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.runTimer), userInfo: nil, repeats: true)
  989. }
  990. let me = User.getMyPin()!
  991. var destString = ""
  992. for d in users{
  993. if d.pin == roomId.components(separatedBy: "wbvc")[0] {
  994. continue
  995. }
  996. if destString.isEmpty{
  997. destString = d.pin
  998. } else {
  999. destString = destString + ",\(d.pin)"
  1000. }
  1001. }
  1002. if destString.isEmpty {
  1003. SecureUserDefaults.shared.set("\(roomId.components(separatedBy: "wbvc")[0]),\(me)", forKey: "wb_vc")
  1004. } else {
  1005. SecureUserDefaults.shared.set("\(roomId.components(separatedBy: "wbvc")[0]),\(me),\(destString)", forKey: "wb_vc")
  1006. }
  1007. wbRoomId = roomId
  1008. }
  1009. }
  1010. func cancel(roomId: String) {
  1011. DispatchQueue.main.async {
  1012. self.wbTimer.invalidate()
  1013. self.wbBlink = false
  1014. self.buttonWB.backgroundColor = .lightGray
  1015. self.buttonWB.setNeedsDisplay()
  1016. }
  1017. wbRoomId = ""
  1018. }
  1019. @objc func runTimer(){
  1020. DispatchQueue.main.async {
  1021. self.wbBlink = !self.wbBlink
  1022. if(self.wbBlink){
  1023. //print("set wb blink on")
  1024. self.buttonWB.backgroundColor = .green
  1025. }
  1026. else {
  1027. //print("set wb blink off")
  1028. self.buttonWB.backgroundColor = .lightGray
  1029. }
  1030. self.buttonWB.setNeedsDisplay()
  1031. }
  1032. }
  1033. }