QmeraAudioViewController.swift 45 KB

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