QmeraAudioViewController.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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 buttonSize: CGFloat = 70
  13. lazy var data: String = "" {
  14. didSet {
  15. getUserData { user in
  16. self.user = user
  17. }
  18. }
  19. }
  20. var user: User?
  21. var isAddCall = ""
  22. private var users: [User] = [] {
  23. didSet {
  24. DispatchQueue.main.async {
  25. if oldValue.count > self.users.count { // remove
  26. let remove = oldValue.filter { !self.users.contains($0) }
  27. remove.forEach { user in
  28. if let subviews = self.profiles.subviews as? [ProfileView] {
  29. subviews.forEach { p in
  30. if p.user == user {
  31. self.profiles.removeArrangeSubview(view: p)
  32. }
  33. }
  34. }
  35. }
  36. } else {
  37. if let user = self.users.last {
  38. let profile = ProfileView(image: UIImage(systemName: "person.circle.fill"))
  39. profile.user = user
  40. self.profiles.addArrangedSubview(view: profile)
  41. }
  42. }
  43. self.name.text = self.users.map { $0.fullName }.joined(separator: ", ")
  44. }
  45. }
  46. }
  47. var isOutgoing: Bool = true
  48. var isOnGoing: Bool = false
  49. private var timer: Timer?
  50. private var firstCall: Bool = true
  51. private var isSpeaker: Bool = false
  52. let status: UILabel = {
  53. let label = UILabel()
  54. label.text = "Calling..."
  55. label.font = UIFont.systemFont(ofSize: 14)
  56. label.textColor = .white
  57. label.textAlignment = .center
  58. return label
  59. }()
  60. let profiles: GroupView = {
  61. let groupView = GroupView()
  62. groupView.spacing = 50
  63. groupView.maxUser = 3
  64. return groupView
  65. }()
  66. let name: UILabel = {
  67. let label = UILabel()
  68. label.text = "uwitan"
  69. label.font = UIFont.systemFont(ofSize: 14)
  70. label.textColor = .white
  71. label.textAlignment = .center
  72. return label
  73. }()
  74. let end: UIButton = {
  75. let button = UIButton()
  76. button.setImage(UIImage(systemName: "phone.down"), for: .normal)
  77. button.imageView?.contentMode = .scaleAspectFit
  78. button.imageView?.tintColor = .white
  79. button.setBackgroundColor(.red, for: .normal)
  80. button.setBackgroundColor(.white, for: .highlighted)
  81. button.contentVerticalAlignment = .fill
  82. button.contentHorizontalAlignment = .fill
  83. button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
  84. return button
  85. }()
  86. let reject: UIButton = {
  87. let button = UIButton()
  88. let image = UIImage(systemName: "xmark")
  89. button.setImage(image, for: .normal)
  90. let selectedImage = image?.withTintColor(.mainColor)
  91. button.setImage(selectedImage, for: .selected)
  92. button.imageView?.contentMode = .scaleAspectFit
  93. button.imageView?.tintColor = .white
  94. button.setBackgroundColor(.red, for: .normal)
  95. button.setBackgroundColor(.white, for: .highlighted)
  96. button.contentVerticalAlignment = .fill
  97. button.contentHorizontalAlignment = .fill
  98. button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
  99. return button
  100. }()
  101. let accept: UIButton = {
  102. let button = UIButton()
  103. let image = UIImage(systemName: "checkmark")
  104. button.setImage(image, for: .normal)
  105. button.imageView?.contentMode = .scaleAspectFit
  106. button.imageView?.tintColor = .white
  107. button.setBackgroundColor(.greenColor, for: .normal)
  108. button.setBackgroundColor(.white, for: .highlighted)
  109. button.contentVerticalAlignment = .fill
  110. button.contentHorizontalAlignment = .fill
  111. button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
  112. return button
  113. }()
  114. let invite: UIButton = {
  115. let button = UIButton()
  116. let image = UIImage(systemName: "person.badge.plus")
  117. button.setImage(image, for: .normal)
  118. button.imageView?.contentMode = .scaleAspectFit
  119. button.imageView?.tintColor = .mainColor
  120. button.setBackgroundColor(.white, for: .normal)
  121. button.setBackgroundColor(.mainColor, for: .highlighted)
  122. button.contentVerticalAlignment = .fill
  123. button.contentHorizontalAlignment = .fill
  124. button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
  125. return button
  126. }()
  127. let speaker: UIButton = {
  128. let button = UIButton()
  129. button.setImage(UIImage(systemName: "speaker.slash")?.withTintColor(.mainColor, renderingMode: .alwaysOriginal), for: .normal)
  130. button.setImage(UIImage(systemName: "speaker.wave.3")?.withTintColor(.white, renderingMode: .alwaysOriginal), for: .selected)
  131. button.imageView?.contentMode = .scaleAspectFit
  132. button.setBackgroundColor(.white, for: .normal)
  133. button.setBackgroundColor(.mainColor, for: .highlighted)
  134. button.setBackgroundColor(.mainColor, for: .selected)
  135. button.contentVerticalAlignment = .fill
  136. button.contentHorizontalAlignment = .fill
  137. button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
  138. return button
  139. }()
  140. let stack: UIStackView = {
  141. let stackView = UIStackView()
  142. stackView.axis = .horizontal
  143. stackView.distribution = .fillEqually
  144. return stackView
  145. }()
  146. let poweredByView: UIStackView = {
  147. let stackView = UIStackView()
  148. stackView.axis = .horizontal
  149. stackView.spacing = 5
  150. return stackView
  151. }()
  152. let poweredByLabel: UILabel = {
  153. let label = UILabel()
  154. label.text = "Powered by Nexilis"
  155. return label
  156. }()
  157. let qmeraLogo: UIButton = {
  158. let image = UIImage(named: "Q-Button-PNG", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  159. let button = UIButton()
  160. button.setImage(image, for: .normal)
  161. button.imageView?.contentMode = .scaleAspectFit
  162. button.imageEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
  163. button.contentVerticalAlignment = .fill
  164. button.contentHorizontalAlignment = .fill
  165. button.frame.size.width = 30
  166. button.frame.size.height = 30
  167. return button
  168. }()
  169. let nexilisLogo: UIButton = {
  170. let image = UIImage(named: "pb_powered_button", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  171. let button = UIButton()
  172. button.setImage(image, for: .normal)
  173. button.imageView?.contentMode = .scaleAspectFit
  174. button.imageEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
  175. button.contentVerticalAlignment = .fill
  176. button.contentHorizontalAlignment = .fill
  177. button.frame.size.width = 30
  178. button.frame.size.height = 30
  179. return button
  180. }()
  181. override func viewWillDisappear(_ animated: Bool) {
  182. UIDevice.current.isProximityMonitoringEnabled = false
  183. NotificationCenter.default.removeObserver(self)
  184. }
  185. deinit {
  186. UIDevice.current.isProximityMonitoringEnabled = false
  187. NotificationCenter.default.removeObserver(self)
  188. }
  189. override func viewDidAppear(_ animated: Bool) {
  190. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "onShowAC"), object: nil, userInfo: nil)
  191. }
  192. override func viewDidLoad() {
  193. super.viewDidLoad()
  194. let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .systemUltraThinMaterialDark))
  195. effectView.frame = view.frame
  196. view.insertSubview(effectView, at: 0)
  197. view.addSubview(status)
  198. view.addSubview(profiles)
  199. view.addSubview(name)
  200. status.anchor(left: view.leftAnchor, bottom: profiles.topAnchor, right: view.rightAnchor, paddingBottom: 30, centerX: view.centerXAnchor)
  201. profiles.anchor(centerX: view.centerXAnchor, centerY: view.centerYAnchor, width: 150, height: 150)
  202. name.anchor(top: profiles.bottomAnchor, left: view.leftAnchor, right: view.rightAnchor, paddingTop: 5, paddingLeft: 20, paddingRight: 20, centerX: view.centerXAnchor)
  203. definesPresentationContext = true
  204. NotificationCenter.default.addObserver(self, selector: #selector(onStatusCall(_:)), name: NSNotification.Name(rawValue: "onStatusCall"), object: nil)
  205. NotificationCenter.default.addObserver(self, selector: #selector(onReceiveMessage(notification:)), name: NSNotification.Name(rawValue: "onReceiveChat"), object: nil)
  206. if let u = self.user {
  207. self.users.append(u)
  208. if isOutgoing {
  209. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  210. if onGoingCC.isEmpty {
  211. Nexilis.shared.callManager.startCall(handle: u.pin)
  212. } else {
  213. do {
  214. try AVAudioSession.sharedInstance().setCategory(.playAndRecord)
  215. try AVAudioSession.sharedInstance().setMode(.voiceChat)
  216. try AVAudioSession.sharedInstance().overrideOutputAudioPort(.none)
  217. } catch {
  218. }
  219. API.initiateCCall(sParty: u.pin)
  220. }
  221. }
  222. }
  223. if isOutgoing {
  224. outgoingView()
  225. } else if isOnGoing {
  226. ongoingView()
  227. } else {
  228. incomingView()
  229. }
  230. UIDevice.current.isProximityMonitoringEnabled = true
  231. }
  232. override func viewWillLayoutSubviews() {
  233. super.viewWillLayoutSubviews()
  234. end.circle()
  235. reject.circle()
  236. accept.circle()
  237. invite.circle()
  238. speaker.circle()
  239. }
  240. private func getUserData(completion: @escaping (User?) -> ()) {
  241. if let user = self.user {
  242. completion(user)
  243. return
  244. }
  245. var user: User?
  246. DispatchQueue.global().async {
  247. Database.shared.database?.inTransaction({ fmdb, rollback in
  248. 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() {
  249. user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  250. firstName: cursor.string(forColumnIndex: 1) ?? "",
  251. lastName: cursor.string(forColumnIndex: 2) ?? "",
  252. thumb: cursor.string(forColumnIndex: 3) ?? "")
  253. cursor.close()
  254. }
  255. })
  256. }
  257. completion(user)
  258. }
  259. private func outgoingView() {
  260. view.addSubview(end)
  261. end.anchor(bottom: view.bottomAnchor, paddingBottom: 60, centerX: view.centerXAnchor, width: buttonSize, height: buttonSize)
  262. end.addTarget(self, action: #selector(didEnd(sender:)), for: .touchUpInside)
  263. }
  264. private func incomingView() {
  265. status.text = "Incoming..."
  266. stack.spacing = buttonSize
  267. view.addSubview(stack)
  268. stack.anchor(bottom: view.bottomAnchor, paddingBottom: 60, centerX: view.centerXAnchor, width: buttonSize * 3, height: buttonSize)
  269. stack.addArrangedSubview(reject)
  270. stack.addArrangedSubview(accept)
  271. reject.addTarget(self, action: #selector(didReject(sender:)), for: .touchUpInside)
  272. accept.addTarget(self, action: #selector(didAccept(sender:)), for: .touchUpInside)
  273. }
  274. private func ongoingView() {
  275. status.text = "Connecting..."
  276. stack.spacing = buttonSize / 2
  277. view.addSubview(stack)
  278. stack.anchor(bottom: view.bottomAnchor, paddingBottom: 60, centerX: view.centerXAnchor, width: buttonSize * 4, height: buttonSize)
  279. stack.addArrangedSubview(invite)
  280. stack.addArrangedSubview(end)
  281. stack.addArrangedSubview(speaker)
  282. invite.addTarget(self, action: #selector(didInvite(sender:)), for: .touchUpInside)
  283. end.addTarget(self, action: #selector(didEnd(sender:)), for: .touchUpInside)
  284. speaker.addTarget(self, action: #selector(didSpeaker(sender:)), for: .touchUpInside)
  285. self.view.addSubview(poweredByView)
  286. self.poweredByView.translatesAutoresizingMaskIntoConstraints = false
  287. let constraintRightPowered = self.poweredByView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -10.0)
  288. let constraintBottomPowered = self.poweredByView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10.0)
  289. NSLayoutConstraint.activate([
  290. constraintRightPowered,
  291. constraintBottomPowered,
  292. poweredByView.heightAnchor.constraint(equalToConstant: 40.0),
  293. nexilisLogo.widthAnchor.constraint(equalToConstant: 30.0),
  294. nexilisLogo.heightAnchor.constraint(equalToConstant: 30.0)
  295. ])
  296. poweredByView.addArrangedSubview(poweredByLabel)
  297. poweredByView.addArrangedSubview(nexilisLogo)
  298. }
  299. // MARK: - Action
  300. @objc func didSpeaker(sender: Any?) {
  301. isSpeaker = !isSpeaker
  302. speaker.isSelected = isSpeaker
  303. if isSpeaker {
  304. UIDevice.current.isProximityMonitoringEnabled = false
  305. } else {
  306. UIDevice.current.isProximityMonitoringEnabled = true
  307. }
  308. Nexilis.turnSpeakerOn(bSpeakerOn: isSpeaker)
  309. }
  310. @objc func didInvite(sender: Any?) {
  311. let controller = QmeraCallContactViewController()
  312. controller.isDismiss = { user in
  313. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  314. if !onGoingCC.isEmpty {
  315. DispatchQueue.global().async {
  316. _ = Nexilis.write(message: CoreMessage_TMessageBank.getCCRoomInvite(l_pin: user.pin, ticket_id: onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2], channel: "1"))
  317. }
  318. DispatchQueue.main.async {
  319. self.isAddCall = user.pin
  320. }
  321. } else {
  322. self.users.append(user)
  323. // Start Calling
  324. Nexilis.shared.callManager.startCall(handle: user.pin)
  325. }
  326. }
  327. controller.selectedUser.append(contentsOf: users)
  328. present(UINavigationController(rootViewController: controller), animated: true, completion: nil)
  329. }
  330. @objc func didEnd(sender: Any?) {
  331. poweredByView.isHidden = true
  332. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  333. if !onGoingCC.isEmpty {
  334. if sender != nil && sender is Bool {
  335. self.dismiss(animated: false, completion: nil)
  336. let requester = onGoingCC.components(separatedBy: ",")[0]
  337. let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
  338. let complaintId = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2]
  339. let startTimeCC = UserDefaults.standard.string(forKey: "startTimeCC") ?? ""
  340. DispatchQueue.global().async {
  341. if sender as! Bool == true {
  342. let date = "\(Date().currentTimeMillis())"
  343. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  344. do {
  345. _ = try Database.shared.insertRecord(fmdb: fmdb, table: "CALL_CENTER_HISTORY", cvalues: [
  346. "type" : "1",
  347. "title" : "Contact Center".localized(),
  348. "time" : startTimeCC,
  349. "f_pin" : officer,
  350. "data" : complaintId,
  351. "time_end" : date,
  352. "complaint_id" : complaintId,
  353. "members" : "",
  354. "requester": requester
  355. ], replace: true)
  356. } catch {
  357. rollback.pointee = true
  358. print(error)
  359. }
  360. })
  361. }
  362. UserDefaults.standard.removeObject(forKey: "onGoingCC")
  363. UserDefaults.standard.removeObject(forKey: "membersCC")
  364. UserDefaults.standard.removeObject(forKey: "startTimeCC")
  365. UserDefaults.standard.removeObject(forKey: "waitingRequestCC")
  366. }
  367. return
  368. }
  369. let alert = UIAlertController(title: "Interaction with Call Center is in progress".localized(), message: "Are you sure you want to end the Call Center?".localized(), preferredStyle: .alert)
  370. alert.addAction(UIAlertAction(title: "No".localized(), style: UIAlertAction.Style.default, handler: nil))
  371. alert.addAction(UIAlertAction(title: "Yes".localized(), style: UIAlertAction.Style.default, handler: {(_) in
  372. self.dismiss(animated: false, completion: nil)
  373. let requester = onGoingCC.components(separatedBy: ",")[0]
  374. let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
  375. let complaintId = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2]
  376. let idMe = UserDefaults.standard.string(forKey: "me")!
  377. let startTimeCC = UserDefaults.standard.string(forKey: "startTimeCC") ?? ""
  378. DispatchQueue.global().async {
  379. let date = "\(Date().currentTimeMillis())"
  380. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  381. do {
  382. _ = try Database.shared.insertRecord(fmdb: fmdb, table: "CALL_CENTER_HISTORY", cvalues: [
  383. "type" : "1",
  384. "title" : "Contact Center".localized(),
  385. "time" : startTimeCC,
  386. "f_pin" : officer,
  387. "data" : complaintId,
  388. "time_end" : date,
  389. "complaint_id" : complaintId,
  390. "members" : "",
  391. "requester": requester
  392. ], replace: true)
  393. } catch {
  394. rollback.pointee = true
  395. print(error)
  396. }
  397. })
  398. if officer == idMe {
  399. _ = Nexilis.write(message: CoreMessage_TMessageBank.endCallCenter(complaint_id: complaintId, l_pin: requester))
  400. } else {
  401. if requester == idMe {
  402. _ = Nexilis.write(message: CoreMessage_TMessageBank.endCallCenter(complaint_id: complaintId, l_pin: officer))
  403. } else {
  404. _ = Nexilis.write(message: CoreMessage_TMessageBank.leaveCCRoomInvite(ticket_id: complaintId))
  405. }
  406. }
  407. UserDefaults.standard.removeObject(forKey: "onGoingCC")
  408. UserDefaults.standard.removeObject(forKey: "membersCC")
  409. UserDefaults.standard.removeObject(forKey: "startTimeCC")
  410. UserDefaults.standard.removeObject(forKey: "waitingRequestCC")
  411. }
  412. if let user = self.user, let call = Nexilis.shared.callManager.call(with: user.pin) {
  413. Nexilis.shared.callManager.end(call: call)
  414. } else {
  415. API.terminateCall(sParty: nil)
  416. }
  417. }))
  418. self.present(alert, animated: true, completion: nil)
  419. } else {
  420. if let user = self.user, let call = Nexilis.shared.callManager.call(with: user.pin) {
  421. Nexilis.shared.callManager.end(call: call)
  422. } else {
  423. API.terminateCall(sParty: nil)
  424. }
  425. dismiss(animated: false, completion: nil)
  426. }
  427. }
  428. @objc func didReject(sender: Any?) {
  429. didEnd(sender: sender)
  430. }
  431. @objc func didAccept(sender: Any?) {
  432. NSLayoutConstraint.deactivate(stack.constraints)
  433. stack.subviews.forEach { subview in
  434. subview.removeFromSuperview()
  435. }
  436. ongoingView()
  437. UIView.animate(withDuration: 0.3, animations: {
  438. self.view.layoutIfNeeded()
  439. })
  440. }
  441. // MARK: - Communication
  442. @objc func onReceiveMessage(notification: NSNotification) {
  443. DispatchQueue.main.async {
  444. let data:[AnyHashable : Any] = notification.userInfo!
  445. if let dataMessage = data["message"] as? TMessage {
  446. if (dataMessage.getCode() == CoreMessage_TMessageCode.PUSH_MEMBER_ROOM_CONTACT_CENTER) {
  447. let data = dataMessage.getBody(key: CoreMessage_TMessageKey.DATA)
  448. if !data.isEmpty {
  449. if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions()) as? [AnyObject] {
  450. var members = ""
  451. let idMe = UserDefaults.standard.string(forKey: "me")!
  452. for json in jsonArray {
  453. if "\(json)" != idMe {
  454. if members.isEmpty {
  455. members = "\(json)"
  456. } else {
  457. members += ",\(json)"
  458. }
  459. }
  460. }
  461. UserDefaults.standard.set(members, forKey: "inEditorPersonal")
  462. }
  463. }
  464. self.users.append(User.getData(pin: dataMessage.getPIN())!)
  465. // Start Calling
  466. if !self.isAddCall.isEmpty && self.isAddCall == dataMessage.getPIN() {
  467. // Nexilis.shared.callManager.startCall(handle: dataMessage.getPIN())
  468. API.initiateCCall(sParty: dataMessage.getPIN())
  469. }
  470. }
  471. }
  472. }
  473. }
  474. @objc func onStatusCall(_ notification: NSNotification) {
  475. if let data = notification.userInfo,
  476. let state = data["state"] as? Int,
  477. let message = data["message"] as? String
  478. {
  479. let arrayMessage = message.split(separator: ",")
  480. if state == 23 {
  481. if users.count == 1 {
  482. DispatchQueue.main.async {
  483. self.status.text = "Ringing..."
  484. }
  485. }
  486. } else if state == 22 {
  487. if users.count == 1 && firstCall {
  488. DispatchQueue.main.async {
  489. self.ongoingView()
  490. let connectDate = Date()
  491. self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
  492. let format = Utils.callDurationFormatter.string(from: Date().timeIntervalSince(connectDate))
  493. self.status.text = format
  494. }
  495. self.timer?.fire()
  496. self.firstCall = false
  497. }
  498. }
  499. if (!isOutgoing || !firstCall), users.count >= 1, let user = User.getData(pin: String(arrayMessage[1])), !users.contains(user) {
  500. self.users.append(user)
  501. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  502. if !onGoingCC.isEmpty {
  503. DispatchQueue.main.async {
  504. var members = ""
  505. for user in self.users {
  506. if members.isEmpty {
  507. members = "\(user.pin)"
  508. } else {
  509. members = ",\(user.pin)"
  510. }
  511. }
  512. UserDefaults.standard.set("\(members)", forKey: "membersCC")
  513. }
  514. }
  515. }
  516. } else if state == 28 {
  517. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  518. if let pin = arrayMessage.first, let index = users.firstIndex(of: User(pin: String(pin))) {
  519. users.remove(at: index)
  520. if !onGoingCC.isEmpty && users.count != 0 {
  521. let requester = onGoingCC.components(separatedBy: ",")[0]
  522. let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
  523. if pin == requester || pin == officer {
  524. DispatchQueue.main.async {
  525. if self.viewIfLoaded?.window != nil {
  526. let imageView = UIImageView(image: UIImage(systemName: "info.circle"))
  527. imageView.tintColor = .white
  528. 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)
  529. banner.show()
  530. }
  531. self.timer?.invalidate()
  532. self.timer = nil
  533. self.status.text = "Call Center Session has ended..."
  534. self.end.isEnabled = false
  535. }
  536. DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
  537. self.didEnd(sender: true)
  538. }
  539. return
  540. }
  541. } else if !onGoingCC.isEmpty && users.count == 0 {
  542. DispatchQueue.main.async {
  543. if self.viewIfLoaded?.window != nil {
  544. let imageView = UIImageView(image: UIImage(systemName: "info.circle"))
  545. imageView.tintColor = .white
  546. 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)
  547. banner.show()
  548. }
  549. self.timer?.invalidate()
  550. self.timer = nil
  551. self.status.text = "Call Center Session has ended..."
  552. self.end.isEnabled = false
  553. }
  554. DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
  555. self.didEnd(sender: true)
  556. }
  557. return
  558. } else {
  559. DispatchQueue.main.async {
  560. self.timer?.invalidate()
  561. self.timer = nil
  562. self.status.text = "Audio Call Ended"
  563. self.end.isEnabled = false
  564. }
  565. DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
  566. self.didEnd(sender: true)
  567. }
  568. return
  569. }
  570. }
  571. if users.count == 0 {
  572. DispatchQueue.main.async {
  573. self.dismiss(animated: false, completion: nil)
  574. }
  575. }
  576. } else if state == -3 { // Offline
  577. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  578. if let pin = arrayMessage.first, let index = users.firstIndex(of: User(pin: String(pin))) {
  579. users.remove(at: index)
  580. if !onGoingCC.isEmpty && users.count != 0 {
  581. DispatchQueue.main.async {
  582. var members = ""
  583. for user in self.users {
  584. if members.isEmpty {
  585. members = "\(user.pin)"
  586. } else {
  587. members = ",\(user.pin)"
  588. }
  589. }
  590. UserDefaults.standard.set("\(members)", forKey: "membersCC")
  591. }
  592. }
  593. }
  594. if users.count == 0 {
  595. DispatchQueue.main.async {
  596. self.status.text = "Offline..."
  597. self.end.isEnabled = false
  598. DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
  599. self.didEnd(sender: false)
  600. }
  601. }
  602. }
  603. } else if state == -4 { // Busy
  604. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  605. if let pin = arrayMessage.first, let index = users.firstIndex(of: User(pin: String(pin))) {
  606. users.remove(at: index)
  607. if !onGoingCC.isEmpty && users.count != 0 {
  608. DispatchQueue.main.async {
  609. var members = ""
  610. for user in self.users {
  611. if members.isEmpty {
  612. members = "\(user.pin)"
  613. } else {
  614. members = ",\(user.pin)"
  615. }
  616. }
  617. UserDefaults.standard.set("\(members)", forKey: "membersCC")
  618. }
  619. }
  620. }
  621. if users.count == 0 {
  622. DispatchQueue.main.async {
  623. self.status.text = "Busy..."
  624. self.end.isEnabled = false
  625. DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
  626. self.didEnd(sender: false)
  627. }
  628. }
  629. }
  630. }
  631. }
  632. }
  633. }