QmeraAudioViewController.swift 52 KB

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