QmeraVideoViewController.swift 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. //
  2. // VideoViewControllerQmera.swift
  3. // Qmera
  4. //
  5. // Created by Akhmad Al Qindi Irsyam on 07/10/21.
  6. //
  7. // Rn
  8. // (Kedip whiteboard)
  9. // extension Whiteboard
  10. // wbVC!.close wbTimer wbBlink
  11. import UIKit
  12. import nuSDKService
  13. import AVFoundation
  14. import NotificationBannerSwift
  15. class QmeraVideoViewController: UIViewController {
  16. var dataPerson: [[String: String?]] = []
  17. var fPin = ""
  18. var wbRoomId = ""
  19. var isInisiator = true
  20. var isSpeaker = true
  21. var isPresent = false
  22. var listRemoteViewFix: [UIImageView] = [
  23. UIImageView(),
  24. UIImageView(),
  25. UIImageView(),
  26. UIImageView(),
  27. UIImageView()
  28. ]
  29. var containerLabelName: [UIView] = [
  30. UIView(),
  31. UIView(),
  32. UIView(),
  33. UIView(),
  34. UIView()
  35. ]
  36. let myImage = UIImageView()
  37. let name = UILabel()
  38. let profileImage = UIImageView()
  39. let labelIncomingOutgoing = UILabel()
  40. let buttonDecline = UIButton()
  41. let buttonAccept = UIButton()
  42. let zoomView = UIImageView()
  43. let cameraView = UIImageView()
  44. var constraintLeadingButtonDecline = NSLayoutConstraint()
  45. var constraintBottomButtonDecline = NSLayoutConstraint()
  46. var constraintBottomStackViewToolbar = NSLayoutConstraint()
  47. var constraintLeftStackViewToolbar2 = NSLayoutConstraint()
  48. let stackViewToolbar = UIStackView()
  49. let stackViewToolbar2 = UIStackView()
  50. var onScreenConstraintWB = [NSLayoutConstraint]()
  51. let buttonWB = UIButton()
  52. let buttonChat = UIButton()
  53. var wbVC : WhiteboardViewController?
  54. let buttonAddParticipant = UIButton()
  55. let buttonSpeaker = UIButton()
  56. let buttonRotate = UIButton()
  57. var showStackViewToolbar = true
  58. let scrollRemoteView = UIScrollView()
  59. var isAutoAccept = false
  60. var wbTimer = Timer()
  61. var wbBlink = false
  62. var showNotifCCEnd = false
  63. var transformZoomAfterNewUserMore2 = false
  64. var isAddCall = ""
  65. var ticketId = ""
  66. private var frontCamera = true
  67. var users: [User] = []
  68. let poweredByView: UIStackView = {
  69. let stackView = UIStackView()
  70. stackView.axis = .horizontal
  71. stackView.spacing = 5
  72. return stackView
  73. }()
  74. private var vcTimer = Timer()
  75. private var containerTimerVC = UIView()
  76. private var labelTimerVC = UILabel()
  77. let poweredByLabel: UILabel = {
  78. let label = UILabel()
  79. label.text = "Powered by Nexilis".localized()
  80. return label
  81. }()
  82. let qmeraLogo: UIButton = {
  83. let image = UIImage(named: "Q-Button-PNG", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  84. let button = UIButton()
  85. button.setImage(image, for: .normal)
  86. button.imageView?.contentMode = .scaleAspectFit
  87. button.imageEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
  88. button.contentVerticalAlignment = .fill
  89. button.contentHorizontalAlignment = .fill
  90. // button.frame.size.width = 30
  91. // button.frame.size.height = 30
  92. return button
  93. }()
  94. let nexilisLogo: UIButton = {
  95. let image = UIImage(named: "pb_powered_button", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  96. let button = UIButton()
  97. button.setImage(image, for: .normal)
  98. button.imageView?.contentMode = .scaleAspectFit
  99. button.imageEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
  100. button.contentVerticalAlignment = .fill
  101. button.contentHorizontalAlignment = .fill
  102. // button.frame.size.width = 30
  103. // button.frame.size.height = 30
  104. return button
  105. }()
  106. deinit {
  107. navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
  108. navigationController?.navigationBar.shadowImage = nil
  109. navigationController?.navigationBar.isTranslucent = false
  110. navigationController?.view.backgroundColor = .mainColor
  111. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  112. navigationController?.navigationBar.titleTextAttributes = textAttributes
  113. navigationController?.navigationBar.topItem?.backBarButtonItem = nil
  114. navigationController?.interactivePopGestureRecognizer?.isEnabled = true
  115. NotificationCenter.default.removeObserver(self)
  116. }
  117. override func viewWillDisappear(_ animated: Bool) {
  118. if self.isMovingFromParent {
  119. navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
  120. navigationController?.navigationBar.shadowImage = nil
  121. navigationController?.navigationBar.isTranslucent = false
  122. navigationController?.view.backgroundColor = .mainColor
  123. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  124. navigationController?.navigationBar.titleTextAttributes = textAttributes
  125. navigationController?.navigationBar.topItem?.backBarButtonItem = nil
  126. navigationController?.interactivePopGestureRecognizer?.isEnabled = true
  127. NotificationCenter.default.removeObserver(self)
  128. }
  129. }
  130. override func viewDidLoad() {
  131. super.viewDidLoad()
  132. Nexilis.setWhiteboardReceiver(receiver: self)
  133. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
  134. let navBarAppearance = UINavigationBarAppearance()
  135. navBarAppearance.configureWithTransparentBackground()
  136. navigationController?.navigationBar.standardAppearance = navBarAppearance
  137. navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
  138. navigationController?.changeAppearance(clear: true)
  139. NotificationCenter.default.addObserver(self, selector: #selector(self.onStatusCall(_:)), name: NSNotification.Name(rawValue: "onStatusCall"), object: nil)
  140. NotificationCenter.default.addObserver(self, selector: #selector(onReceiveMessage(notification:)), name: NSNotification.Name(rawValue: "onReceiveChat"), object: nil)
  141. view.backgroundColor = .clear
  142. navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
  143. navigationController?.interactivePopGestureRecognizer?.isEnabled = false
  144. navigationItem.setHidesBackButton(true, animated: false)
  145. if fPin != ""{
  146. getDataProfile(fPin: fPin)
  147. }
  148. addZoomView()
  149. addCameraView()
  150. addListRemoteView()
  151. addBackgroundIncoming()
  152. addProfileNameCalling()
  153. Calling()
  154. addToolbar()
  155. addTimerVC()
  156. if isAutoAccept {
  157. didTapAcceptCallButton()
  158. }
  159. }
  160. func getDataProfile(fPin: String) {
  161. let query = "SELECT f_pin, first_name, last_name, official_account, image_id, device_id, offline_mode, user_type FROM BUDDY where f_pin = '\(fPin)'"
  162. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  163. if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
  164. var row: [String: String?] = [:]
  165. if cursorData.next() {
  166. row["f_pin"] = cursorData.string(forColumnIndex: 0)
  167. var name = ""
  168. if let firstname = cursorData.string(forColumnIndex: 1) {
  169. name = firstname
  170. }
  171. if let lastname = cursorData.string(forColumnIndex: 2) {
  172. name = name + " " + lastname
  173. }
  174. row["name"] = name
  175. row["picture"] = cursorData.string(forColumnIndex: 4)
  176. row["isOfficial"] = cursorData.string(forColumnIndex: 3)
  177. row["deviceId"] = cursorData.string(forColumnIndex: 5)
  178. row["isOffline"] = cursorData.string(forColumnIndex: 6)
  179. row["user_type"] = cursorData.string(forColumnIndex: 7)
  180. if fPin != User.getMyPin() {
  181. dataPerson.append(row)
  182. }
  183. } else {
  184. var row: [String: String?] = [:]
  185. row["f_pin"] = fPin
  186. row["name"] = "User".localized()
  187. row["picture"] = ""
  188. row["isOfficial"] = ""
  189. row["deviceId"] = ""
  190. row["isOffline"] = ""
  191. row["user_type"] = ""
  192. dataPerson.append(row)
  193. }
  194. cursorData.close()
  195. } else {
  196. var row: [String: String?] = [:]
  197. row["f_pin"] = fPin
  198. row["name"] = "User".localized()
  199. row["picture"] = ""
  200. row["isOfficial"] = ""
  201. row["deviceId"] = ""
  202. row["isOffline"] = ""
  203. row["user_type"] = ""
  204. dataPerson.append(row)
  205. // if let response = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getAddFriendQRCode(fpin: fPin)), response.isOk() {
  206. // self.getDataProfile(fPin: fPin)
  207. // }
  208. // Nexilis.addFriend (fpin: "\(fPin)") { result in
  209. // if result {
  210. // self.getDataProfile(fPin: fPin)
  211. // } else {
  212. // let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  213. // imageView.tintColor = .white
  214. // let banner = FloatingNotificationBanner(title: "Server busy, please try again later".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  215. // banner.show()
  216. // }
  217. // }
  218. }
  219. })
  220. }
  221. func addTimerVC() {
  222. view.addSubview(containerTimerVC)
  223. containerTimerVC.anchor(top: view.topAnchor, paddingTop: 20, centerX: view.centerXAnchor, minWidth: 40)
  224. containerTimerVC.makeRoundedView(radius: 8)
  225. containerTimerVC.backgroundColor = .black.withAlphaComponent(0.3)
  226. containerTimerVC.addSubview(labelTimerVC)
  227. labelTimerVC.anchor(left: containerTimerVC.leftAnchor, right: containerTimerVC.rightAnchor, paddingLeft: 8, paddingRight: 8, centerX: containerTimerVC.centerXAnchor, centerY: containerTimerVC.centerYAnchor)
  228. labelTimerVC.textColor = .white
  229. containerTimerVC.isHidden = true
  230. }
  231. func addZoomView() {
  232. view.addSubview(zoomView)
  233. zoomView.translatesAutoresizingMaskIntoConstraints = false
  234. NSLayoutConstraint.activate([
  235. zoomView.topAnchor.constraint(equalTo: view.topAnchor),
  236. zoomView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  237. zoomView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  238. zoomView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  239. ])
  240. zoomView.backgroundColor = .secondaryColor
  241. zoomView.isUserInteractionEnabled = true
  242. zoomView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideToolbar)))
  243. }
  244. func addCameraView() {
  245. view.addSubview(cameraView)
  246. // cameraView.frame = CGRect(x: view.frame.width - 130, y: 20, width: 120, height: 160)
  247. cameraView.translatesAutoresizingMaskIntoConstraints = false
  248. NSLayoutConstraint.activate([
  249. cameraView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20.0),
  250. cameraView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10.0),
  251. cameraView.widthAnchor.constraint(equalToConstant: 120.0),
  252. cameraView.heightAnchor.constraint(equalToConstant: 160.0)
  253. ])
  254. cameraView.backgroundColor = .secondaryColor
  255. cameraView.makeRoundedView(radius: 8)
  256. }
  257. func addListRemoteView() {
  258. view.addSubview(scrollRemoteView)
  259. scrollRemoteView.translatesAutoresizingMaskIntoConstraints = false
  260. NSLayoutConstraint.activate([
  261. scrollRemoteView.topAnchor.constraint(equalTo: cameraView.bottomAnchor, constant: 10),
  262. scrollRemoteView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10),
  263. scrollRemoteView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
  264. scrollRemoteView.widthAnchor.constraint(equalToConstant: 120.0)
  265. ])
  266. scrollRemoteView.showsHorizontalScrollIndicator = false
  267. scrollRemoteView.showsVerticalScrollIndicator = false
  268. scrollRemoteView.contentSize.width = 120.0
  269. scrollRemoteView.backgroundColor = .clear
  270. }
  271. func addBackgroundIncoming() {
  272. view.addSubview(myImage)
  273. myImage.translatesAutoresizingMaskIntoConstraints = false
  274. NSLayoutConstraint.activate([
  275. myImage.topAnchor.constraint(equalTo: view.topAnchor),
  276. myImage.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  277. myImage.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  278. myImage.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  279. ])
  280. myImage.backgroundColor = .lightGray
  281. myImage.tintColor = .secondaryColor
  282. let image = dataPerson[0]["picture"]!!
  283. if image.isEmpty {
  284. myImage.image = UIImage(systemName: "person")
  285. myImage.contentMode = .scaleAspectFit
  286. } else {
  287. myImage.setImage(name: image)
  288. myImage.contentMode = .scaleAspectFill
  289. }
  290. // let idMe = UserDefaults.standard.string(forKey: "me") as String?
  291. // Database().database?.inTransaction({ fmdb, rollback in
  292. // if let c = Database().getRecords(fmdb: fmdb, query: "select image_id from BUDDY where f_pin = '\(idMe!)'"), c.next() {
  293. // let image = c.string(forColumnIndex: 0)!
  294. // if image.isEmpty {
  295. // myImage.image = UIImage(systemName: "person")
  296. // myImage.contentMode = .scaleAspectFit
  297. // } else {
  298. // myImage.setImage(name: image)
  299. // myImage.contentMode = .scaleAspectFill
  300. // }
  301. // c.close()
  302. // }
  303. // })
  304. }
  305. func addProfileNameCalling() {
  306. view.addSubview(profileImage)
  307. profileImage.translatesAutoresizingMaskIntoConstraints = false
  308. profileImage.frame.size = CGSize(width: 60.0, height: 60.0)
  309. NSLayoutConstraint.activate([
  310. profileImage.topAnchor.constraint(equalTo: view.topAnchor, constant: 40.0),
  311. profileImage.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  312. profileImage.widthAnchor.constraint(equalToConstant: 60.0),
  313. profileImage.heightAnchor.constraint(equalToConstant: 63.0)
  314. ])
  315. profileImage.backgroundColor = .lightGray
  316. profileImage.tintColor = .secondaryColor
  317. profileImage.circle()
  318. let image = dataPerson[0]["picture"]!!
  319. if image.isEmpty {
  320. profileImage.image = UIImage(systemName: "person")
  321. profileImage.contentMode = .scaleAspectFit
  322. profileImage.layer.borderWidth = 1
  323. profileImage.layer.borderColor = UIColor.secondaryColor.cgColor
  324. } else {
  325. profileImage.setImage(name: image)
  326. profileImage.contentMode = .scaleAspectFill
  327. }
  328. view.addSubview(name)
  329. name.translatesAutoresizingMaskIntoConstraints = false
  330. NSLayoutConstraint.activate([
  331. name.topAnchor.constraint(equalTo: profileImage.bottomAnchor, constant: 5.0),
  332. name.centerXAnchor.constraint(equalTo: view.centerXAnchor)
  333. ])
  334. name.font = UIFont.systemFont(ofSize: 12)
  335. name.backgroundColor = .black.withAlphaComponent(0.05)
  336. name.layer.cornerRadius = 5.0
  337. name.clipsToBounds = true
  338. name.textColor = .mainColor
  339. name.text = dataPerson[0]["name"]!?.trimmingCharacters(in: .whitespaces)
  340. }
  341. func Calling() {
  342. view.addSubview(labelIncomingOutgoing)
  343. labelIncomingOutgoing.translatesAutoresizingMaskIntoConstraints = false
  344. NSLayoutConstraint.activate([
  345. labelIncomingOutgoing.topAnchor.constraint(equalTo: name.bottomAnchor, constant: 40.0),
  346. labelIncomingOutgoing.centerXAnchor.constraint(equalTo: view.centerXAnchor)
  347. ])
  348. if isInisiator {
  349. labelIncomingOutgoing.text = "Outgoing video call".localized() + "..."
  350. // Nexilis.startAudio()
  351. if ticketId.isEmpty {
  352. API.initiateCCall(sParty: dataPerson[0]["f_pin"]!, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView)
  353. } else {
  354. API.ccs(sTicketID: ticketId, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView, bCameraOn: true)
  355. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getIncomingCallCS(f_pin_opposite: users[0].pin), timeout: 30 * 1000){
  356. if response.mBodies[CoreMessage_TMessageKey.ERRCOD] != "01" {
  357. endAllCall()
  358. }
  359. }
  360. }
  361. } else {
  362. let systemSoundID: SystemSoundID = 1254
  363. AudioServicesPlaySystemSound(systemSoundID)
  364. labelIncomingOutgoing.text = "Incoming video call".localized() + "..."
  365. }
  366. labelIncomingOutgoing.font = UIFont.systemFont(ofSize: 12)
  367. labelIncomingOutgoing.backgroundColor = .black.withAlphaComponent(0.05)
  368. labelIncomingOutgoing.layer.cornerRadius = 5.0
  369. labelIncomingOutgoing.clipsToBounds = true
  370. labelIncomingOutgoing.textColor = .mainColor
  371. }
  372. func addToolbar() {
  373. view.addSubview(buttonDecline)
  374. buttonDecline.translatesAutoresizingMaskIntoConstraints = false
  375. buttonDecline.frame.size = CGSize(width: 70.0, height: 70.0)
  376. if isInisiator {
  377. constraintLeadingButtonDecline = buttonDecline.centerXAnchor.constraint(equalTo: view.centerXAnchor)
  378. } else {
  379. constraintLeadingButtonDecline = buttonDecline.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: view.frame.width * 0.2)
  380. }
  381. constraintBottomButtonDecline = buttonDecline.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -60.0)
  382. NSLayoutConstraint.activate([
  383. constraintBottomButtonDecline,
  384. constraintLeadingButtonDecline,
  385. buttonDecline.widthAnchor.constraint(equalToConstant: 70.0),
  386. buttonDecline.heightAnchor.constraint(equalToConstant: 70.0)
  387. ])
  388. buttonDecline.backgroundColor = .red
  389. buttonDecline.circle()
  390. buttonDecline.setImage(UIImage(systemName: "xmark", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  391. buttonDecline.tintColor = .white
  392. buttonDecline.addTarget(self, action: #selector(didTapDeclineCallButton(sender:)), for: .touchUpInside)
  393. if !isInisiator{
  394. view.addSubview(buttonAccept)
  395. buttonAccept.translatesAutoresizingMaskIntoConstraints = false
  396. buttonAccept.frame.size = CGSize(width: 70.0, height: 70.0)
  397. NSLayoutConstraint.activate([
  398. buttonAccept.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -60.0),
  399. buttonAccept.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -(view.frame.width * 0.2)),
  400. buttonAccept.widthAnchor.constraint(equalToConstant: 70.0),
  401. buttonAccept.heightAnchor.constraint(equalToConstant: 70.0)
  402. ])
  403. buttonAccept.backgroundColor = .greenColor
  404. buttonAccept.circle()
  405. buttonAccept.setImage(UIImage(systemName: "checkmark", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  406. buttonAccept.tintColor = .white
  407. buttonAccept.addTarget(self, action: #selector(didTapAcceptCallButton), for: .touchUpInside)
  408. }
  409. }
  410. @objc func onReceiveMessage(notification: NSNotification) {
  411. DispatchQueue.main.async {
  412. let data:[AnyHashable : Any] = notification.userInfo!
  413. if let dataMessage = data["message"] as? TMessage {
  414. if (dataMessage.getCode() == CoreMessage_TMessageCode.PUSH_MEMBER_ROOM_CONTACT_CENTER) {
  415. let data = dataMessage.getBody(key: CoreMessage_TMessageKey.DATA)
  416. if !data.isEmpty {
  417. if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions()) as? [AnyObject] {
  418. var members = ""
  419. let idMe = UserDefaults.standard.string(forKey: "me")!
  420. for json in jsonArray {
  421. if "\(json)" != idMe {
  422. if members.isEmpty {
  423. members = "\(json)"
  424. } else {
  425. members += ",\(json)"
  426. }
  427. }
  428. }
  429. UserDefaults.standard.set("\(members)", forKey: "membersCC")
  430. }
  431. }
  432. }
  433. }
  434. }
  435. }
  436. @objc func didTapDeclineCallButton(sender: AnyObject){
  437. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  438. if !onGoingCC.isEmpty {
  439. 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)
  440. alert.addAction(UIAlertAction(title: "No".localized(), style: UIAlertAction.Style.default, handler: nil))
  441. alert.addAction(UIAlertAction(title: "Yes".localized(), style: UIAlertAction.Style.default, handler: {(_) in
  442. if(!self.wbRoomId.isEmpty){
  443. DispatchQueue.main.async {
  444. self.wbTimer.invalidate()
  445. _ = Nexilis.getWhiteboardDelegate()?.terminate()
  446. }
  447. }
  448. self.endAllCall()
  449. self.dismiss(animated: true, completion: nil)
  450. }))
  451. self.present(alert, animated: true, completion: nil)
  452. } else {
  453. let alert = LibAlertController(title: "End Video Call".localized(), message: "Are you sure you want to end video call?".localized(), preferredStyle: .alert)
  454. alert.addAction(UIAlertAction(title: "No".localized(), style: UIAlertAction.Style.default, handler: nil))
  455. alert.addAction(UIAlertAction(title: "Yes".localized(), style: UIAlertAction.Style.default, handler: {(_) in
  456. if self.labelIncomingOutgoing.isDescendant(of: self.view) {
  457. self.labelIncomingOutgoing.text = "Video call is over".localized()
  458. }
  459. if self.stackViewToolbar.isDescendant(of: self.view){
  460. self.stackViewToolbar.removeFromSuperview()
  461. }
  462. if self.stackViewToolbar2.isDescendant(of: self.view){
  463. self.stackViewToolbar2.removeFromSuperview()
  464. }
  465. if self.buttonWB.isDescendant(of: self.view){
  466. self.buttonWB.removeFromSuperview()
  467. }
  468. if self.buttonChat.isDescendant(of: self.view){
  469. self.buttonChat.removeFromSuperview()
  470. }
  471. if self.buttonDecline.isDescendant(of: self.view) {
  472. self.buttonDecline.removeFromSuperview()
  473. }
  474. if self.buttonAccept.isDescendant(of: self.view) {
  475. self.buttonAccept.removeFromSuperview()
  476. }
  477. if self.buttonRotate.isDescendant(of: self.view) {
  478. self.buttonRotate.removeFromSuperview()
  479. }
  480. if self.wbVC != nil{
  481. self.wbVC!.close?()
  482. }
  483. self.wbTimer.invalidate()
  484. self.vcTimer.invalidate()
  485. self.labelTimerVC.text = "Video call is over".localized()
  486. self.endAllCall()
  487. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  488. if self.isInisiator && !self.isPresent {
  489. self.navigationController?.popViewController(animated: true)
  490. } else {
  491. self.dismiss(animated: true, completion: nil)
  492. }
  493. }
  494. }))
  495. self.present(alert, animated: true, completion: nil)
  496. }
  497. }
  498. @objc func didTapAcceptCallButton() {
  499. if !isInisiator{
  500. let goAudioCall = Nexilis.checkMicPermission()
  501. let goVideoCall = Nexilis.checkCameraPermission()
  502. if goVideoCall == 0 {
  503. let alert = LibAlertController(title: "Attention!".localized(), message: !goAudioCall && goVideoCall == 0 ? "Please allow microphone & camera permission in your settings".localized() : !goAudioCall ? "Please allow microphone permission in your settings".localized() : "Please allow camera permission in your settings", preferredStyle: .alert)
  504. alert.addAction(UIAlertAction(title: "OK".localized(), style: UIAlertAction.Style.default, handler: {_ in
  505. if let url = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(url) {
  506. UIApplication.shared.open(url, options: [:], completionHandler: nil)
  507. }
  508. }))
  509. self.navigationController?.present(alert, animated: true, completion: nil)
  510. return
  511. } else if goVideoCall == -1 {
  512. return
  513. }
  514. // Nexilis.startAudio()
  515. if ticketId.isEmpty {
  516. API.receiveCCall(sParty: dataPerson[0]["f_pin"]!, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView,ivRemoteZ: zoomView)
  517. } else {
  518. API.csa(sTicketID: ticketId, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView, bCameraOn: true)
  519. }
  520. }
  521. DispatchQueue.main.async {
  522. self.myImage.removeFromSuperview()
  523. self.name.removeFromSuperview()
  524. self.profileImage.removeFromSuperview()
  525. self.labelIncomingOutgoing.removeFromSuperview()
  526. self.buttonAccept.removeFromSuperview()
  527. NSLayoutConstraint.deactivate([
  528. self.constraintLeadingButtonDecline,
  529. self.constraintBottomButtonDecline
  530. ])
  531. self.addToolbarAfterAccept()
  532. self.buttonDecline.setImage(UIImage(systemName: "phone.down", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  533. UIView.animate(withDuration: 1.0, animations: {
  534. self.view.layoutIfNeeded()
  535. })
  536. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  537. self.containerTimerVC.isHidden = false
  538. self.buttonRotate.isHidden = false
  539. self.buttonAddParticipant.isHidden = false
  540. self.buttonSpeaker.isHidden = false
  541. self.buttonWB.isHidden = false
  542. self.buttonChat.isHidden = false
  543. self.poweredByView.isHidden = false
  544. let connectDate = Date()
  545. self.vcTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
  546. let format = Utils.callDurationFormatter.string(from: Date().timeIntervalSince(connectDate))
  547. self.labelTimerVC.text = format
  548. }
  549. self.vcTimer.fire()
  550. API.adjustVolume(fValue: 10.0)
  551. }
  552. }
  553. }
  554. @objc func didTapChatButton(){
  555. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  556. let members = UserDefaults.standard.string(forKey: "membersCC") ?? ""
  557. let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
  558. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  559. editorPersonalVC.hidesBottomBarWhenPushed = true
  560. editorPersonalVC.unique_l_pin = officer
  561. editorPersonalVC.fromNotification = true
  562. editorPersonalVC.isContactCenter = true
  563. editorPersonalVC.fPinContacCenter = members
  564. editorPersonalVC.complaintId = ticketId
  565. editorPersonalVC.onGoingCC = true
  566. editorPersonalVC.isRequestContactCenter = false
  567. editorPersonalVC.users = users
  568. editorPersonalVC.fromVCAC = true
  569. let navigationController = UINavigationController(rootViewController: editorPersonalVC)
  570. navigationController.modalPresentationStyle = .overCurrentContext
  571. navigationController.navigationBar.tintColor = .white
  572. navigationController.navigationBar.barTintColor = .mainColor
  573. navigationController.navigationBar.isTranslucent = false
  574. navigationController.navigationBar.overrideUserInterfaceStyle = .dark
  575. navigationController.navigationBar.barStyle = .black
  576. let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]
  577. UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, for: .normal)
  578. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  579. navigationController.navigationBar.titleTextAttributes = textAttributes
  580. navigationController.view.backgroundColor = .mainColor
  581. if UIApplication.shared.visibleViewController?.navigationController != nil {
  582. UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
  583. } else {
  584. UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
  585. }
  586. }
  587. @objc func didTapWBButton(){
  588. if(wbVC == nil){
  589. wbVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "wbVC") as? WhiteboardViewController
  590. if(wbRoomId.isEmpty){
  591. let me = UserDefaults.standard.string(forKey: "me")!
  592. let tid = CoreMessage_TMessageUtil.getTID()
  593. wbRoomId = "\(me)wbvc\(tid)"
  594. wbVC!.roomId = wbRoomId
  595. var destinations = [String]()
  596. var destString = ""
  597. for d in dataPerson{
  598. destinations.append(d["f_pin"]!!)
  599. if destString.isEmpty{
  600. destString = d["f_pin"]!!
  601. } else {
  602. destString = destString + ",\(d["f_pin"]!!)"
  603. }
  604. }
  605. wbVC!.destinations = destinations
  606. wbVC!.sendInit()
  607. UserDefaults.standard.set("\(me),\(destString)", forKey: "wb_vc")
  608. }
  609. else {
  610. self.wbTimer.invalidate()
  611. self.buttonWB.backgroundColor = .lightGray
  612. wbVC!.roomId = wbRoomId
  613. wbVC!.sendJoin()
  614. }
  615. }
  616. wbVC!.close = {
  617. DispatchQueue.main.async {
  618. if self.wbVC!.view.isDescendant(of: self.view){
  619. self.wbVC!.view.removeFromSuperview()
  620. }
  621. self.buttonDecline.isHidden = false
  622. self.buttonSpeaker.isHidden = false
  623. self.buttonAddParticipant.isHidden = false
  624. self.buttonRotate.isHidden = false
  625. // if(!self.wbRoomId.isEmpty){
  626. // DispatchQueue.main.async {
  627. // self.wbTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.runTimer), userInfo: nil, repeats: true)
  628. // }
  629. // }
  630. }
  631. }
  632. self.buttonDecline.isHidden = true
  633. self.buttonSpeaker.isHidden = true
  634. self.buttonAddParticipant.isHidden = true
  635. self.buttonRotate.isHidden = true
  636. addChild(wbVC!)
  637. wbVC!.view.translatesAutoresizingMaskIntoConstraints = false
  638. view.addSubview(wbVC!.view)
  639. onScreenConstraintWB = [
  640. wbVC!.view.topAnchor.constraint(equalTo: self.view.topAnchor),
  641. wbVC!.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
  642. wbVC!.view.rightAnchor.constraint(equalTo: self.view.rightAnchor),
  643. wbVC!.view.leftAnchor.constraint(equalTo: self.view.leftAnchor),
  644. ]
  645. NSLayoutConstraint.activate(onScreenConstraintWB)
  646. // Notify the child view controller that the move is complete.
  647. wbVC!.didMove(toParent: self)
  648. // self.navigationController?.setNavigationBarHidden(false, animated: true)
  649. // controller.modalPresentationStyle = .overCurrentContext
  650. // self.navigationController?.present(controller, animated: true)
  651. }
  652. func addToolbarAfterAccept() {
  653. self.view.addSubview(self.stackViewToolbar)
  654. self.stackViewToolbar.translatesAutoresizingMaskIntoConstraints = false
  655. constraintBottomStackViewToolbar = self.stackViewToolbar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -60.0)
  656. NSLayoutConstraint.activate([
  657. self.stackViewToolbar.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
  658. constraintBottomStackViewToolbar
  659. ])
  660. self.stackViewToolbar.axis = .horizontal
  661. self.stackViewToolbar.distribution = .equalSpacing
  662. self.stackViewToolbar.alignment = .center
  663. self.stackViewToolbar.spacing = 30
  664. self.view.addSubview(buttonRotate)
  665. buttonRotate.translatesAutoresizingMaskIntoConstraints = false
  666. buttonRotate.frame.size = CGSize(width: 70.0, height: 70.0)
  667. NSLayoutConstraint.activate([
  668. buttonRotate.widthAnchor.constraint(equalToConstant: 70.0),
  669. buttonRotate.heightAnchor.constraint(equalToConstant: 70.0),
  670. buttonRotate.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
  671. buttonRotate.bottomAnchor.constraint(equalTo: self.stackViewToolbar.topAnchor, constant: -10.0)
  672. ])
  673. buttonRotate.backgroundColor = .secondaryColor
  674. buttonRotate.setImage(UIImage(systemName: "arrow.triangle.2.circlepath.camera", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  675. buttonRotate.tintColor = .mainColor
  676. buttonRotate.circle()
  677. buttonRotate.isHidden = true
  678. buttonRotate.addTarget(self, action: #selector(camera(sender:)), for: .touchUpInside)
  679. view.addSubview(buttonAddParticipant)
  680. buttonAddParticipant.translatesAutoresizingMaskIntoConstraints = false
  681. buttonAddParticipant.frame.size = CGSize(width: 70.0, height: 70.0)
  682. NSLayoutConstraint.activate([
  683. buttonAddParticipant.widthAnchor.constraint(equalToConstant: 70.0),
  684. buttonAddParticipant.heightAnchor.constraint(equalToConstant: 70.0)
  685. ])
  686. buttonAddParticipant.backgroundColor = .secondaryColor
  687. buttonAddParticipant.setImage(UIImage(systemName: "person.badge.plus", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  688. buttonAddParticipant.tintColor = .mainColor
  689. buttonAddParticipant.circle()
  690. buttonAddParticipant.isHidden = true
  691. buttonAddParticipant.addTarget(self, action: #selector(didTapAddParticipantButton(sender:)), for: .touchUpInside)
  692. view.addSubview(buttonSpeaker)
  693. buttonSpeaker.translatesAutoresizingMaskIntoConstraints = false
  694. buttonSpeaker.frame.size = CGSize(width: 70.0, height: 70.0)
  695. NSLayoutConstraint.activate([
  696. buttonSpeaker.widthAnchor.constraint(equalToConstant: 70.0),
  697. buttonSpeaker.heightAnchor.constraint(equalToConstant: 70.0)
  698. ])
  699. buttonSpeaker.setImage(UIImage(systemName: "speaker.wave.2", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  700. self.buttonSpeaker.backgroundColor = .lightGray
  701. self.buttonSpeaker.tintColor = .mainColor
  702. buttonSpeaker.circle()
  703. buttonSpeaker.isHidden = true
  704. buttonSpeaker.addTarget(self, action: #selector(didTapSpeakerButton(sender:)), for: .touchUpInside)
  705. self.view.addSubview(self.stackViewToolbar2)
  706. self.stackViewToolbar2.translatesAutoresizingMaskIntoConstraints = false
  707. constraintLeftStackViewToolbar2 = self.stackViewToolbar2.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 10.0)
  708. NSLayoutConstraint.activate([
  709. self.stackViewToolbar2.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
  710. constraintLeftStackViewToolbar2
  711. ])
  712. self.stackViewToolbar2.axis = .vertical
  713. self.stackViewToolbar2.distribution = .equalSpacing
  714. self.stackViewToolbar2.alignment = .center
  715. self.stackViewToolbar2.spacing = 5
  716. view.addSubview(buttonWB)
  717. buttonWB.translatesAutoresizingMaskIntoConstraints = false
  718. buttonWB.frame.size = CGSize(width: 40.0, height: 40.0)
  719. NSLayoutConstraint.activate([
  720. buttonWB.widthAnchor.constraint(equalToConstant: 40.0),
  721. buttonWB.heightAnchor.constraint(equalToConstant: 40.0)
  722. ])
  723. buttonWB.backgroundColor = .lightGray
  724. buttonWB.setImage(UIImage(systemName: "ipad.landscape", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .medium, scale: .default)), for: .normal)
  725. buttonWB.circle()
  726. buttonWB.tintColor = .black
  727. buttonWB.isHidden = true
  728. buttonWB.addTarget(self, action: #selector(didTapWBButton), for: .touchUpInside)
  729. if !ticketId.isEmpty {
  730. view.addSubview(buttonChat)
  731. buttonChat.translatesAutoresizingMaskIntoConstraints = false
  732. buttonChat.frame.size = CGSize(width: 40.0, height: 40.0)
  733. NSLayoutConstraint.activate([
  734. buttonChat.widthAnchor.constraint(equalToConstant: 40.0),
  735. buttonChat.heightAnchor.constraint(equalToConstant: 40.0)
  736. ])
  737. buttonChat.backgroundColor = .lightGray
  738. buttonChat.setImage(UIImage(systemName: "bubble.right", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .medium, scale: .default)), for: .normal)
  739. buttonChat.circle()
  740. buttonChat.tintColor = .black
  741. buttonChat.isHidden = true
  742. buttonChat.addTarget(self, action: #selector(didTapChatButton), for: .touchUpInside)
  743. }
  744. self.view.addSubview(poweredByView)
  745. self.poweredByView.translatesAutoresizingMaskIntoConstraints = false
  746. let constraintRightPowered = self.poweredByView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -10.0)
  747. let constraintBottomPowered = self.poweredByView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10.0)
  748. NSLayoutConstraint.activate([
  749. constraintRightPowered,
  750. constraintBottomPowered,
  751. nexilisLogo.widthAnchor.constraint(equalToConstant: 30.0),
  752. nexilisLogo.heightAnchor.constraint(equalToConstant: 30.0)
  753. ])
  754. poweredByView.addArrangedSubview(poweredByLabel)
  755. poweredByView.addArrangedSubview(nexilisLogo)
  756. poweredByView.isHidden = true
  757. stackViewToolbar.addArrangedSubview(buttonAddParticipant)
  758. stackViewToolbar.addArrangedSubview(buttonDecline)
  759. stackViewToolbar.addArrangedSubview(buttonSpeaker)
  760. stackViewToolbar2.addArrangedSubview(buttonWB)
  761. stackViewToolbar2.addArrangedSubview(buttonChat)
  762. // startFaceTimer()
  763. }
  764. func endAllCall() {
  765. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  766. if !onGoingCC.isEmpty {
  767. let requester = onGoingCC.components(separatedBy: ",")[0]
  768. let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
  769. let complaintId = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2]
  770. let idMe = UserDefaults.standard.string(forKey: "me")!
  771. let startTimeCC = UserDefaults.standard.string(forKey: "startTimeCC") ?? ""
  772. DispatchQueue.global().async {
  773. let date = "\(Date().currentTimeMillis())"
  774. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  775. do {
  776. _ = try Database.shared.insertRecord(fmdb: fmdb, table: "CALL_CENTER_HISTORY", cvalues: [
  777. "type" : "2",
  778. "title" : "Contact Center".localized(),
  779. "time" : startTimeCC,
  780. "f_pin" : officer,
  781. "data" : complaintId,
  782. "time_end" : date,
  783. "complaint_id" : complaintId,
  784. "members" : "",
  785. "requester": requester
  786. ], replace: true)
  787. } catch {
  788. rollback.pointee = true
  789. //print(error)
  790. }
  791. })
  792. if officer == idMe {
  793. _ = Nexilis.write(message: CoreMessage_TMessageBank.endCallCenter(complaint_id: complaintId, l_pin: requester))
  794. } else {
  795. if requester == idMe {
  796. _ = Nexilis.write(message: CoreMessage_TMessageBank.endCallCenter(complaint_id: complaintId, l_pin: officer))
  797. } else {
  798. _ = Nexilis.write(message: CoreMessage_TMessageBank.leaveCCRoomInvite(ticket_id: complaintId))
  799. }
  800. }
  801. UserDefaults.standard.removeObject(forKey: "onGoingCC")
  802. UserDefaults.standard.removeObject(forKey: "membersCC")
  803. UserDefaults.standard.removeObject(forKey: "startTimeCC")
  804. UserDefaults.standard.removeObject(forKey: "waitingRequestCC")
  805. }
  806. }
  807. API.terminateCall(sParty: nil)
  808. cameraView.image = nil
  809. zoomView.image = nil
  810. listRemoteViewFix.removeAll()
  811. dataPerson.removeAll()
  812. }
  813. func setSpeaker(isSpeaker: Bool) {
  814. DispatchQueue.main.async {
  815. if (isSpeaker) {
  816. self.buttonSpeaker.backgroundColor = .lightGray
  817. self.buttonSpeaker.tintColor = .mainColor
  818. self.buttonSpeaker.setImage(UIImage(systemName: "speaker.wave.2", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  819. } else {
  820. self.buttonSpeaker.backgroundColor = .secondaryColor
  821. self.buttonSpeaker.tintColor = .mainColor
  822. self.buttonSpeaker.setImage(UIImage(systemName: "speaker.slash", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .medium, scale: .default)), for: .normal)
  823. }
  824. self.isSpeaker = isSpeaker
  825. }
  826. Nexilis.setSpeaker(isSpeaker)
  827. }
  828. @objc func didTapSpeakerButton(sender: AnyObject){
  829. setSpeaker(isSpeaker: !(self.isSpeaker))
  830. }
  831. @objc func didTapAddParticipantButton(sender: AnyObject){
  832. if let contactViewController = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactSID") as? ContactCallViewController {
  833. contactViewController.isAddParticipantVideo = true
  834. contactViewController.connectedCall = dataPerson
  835. contactViewController.isDismiss = { data in
  836. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  837. if !onGoingCC.isEmpty {
  838. DispatchQueue.global().async {
  839. _ = Nexilis.write(message: CoreMessage_TMessageBank.getCCRoomInvite(l_pin: data["f_pin"]!!, ticket_id: onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2], channel: "2"))
  840. }
  841. DispatchQueue.main.async {
  842. self.isAddCall = data["f_pin"]!!
  843. }
  844. } else {
  845. DispatchQueue.main.async {
  846. self.dataPerson.append(data)
  847. API.initiateCCall(sParty: data["f_pin"]!, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: self.listRemoteViewFix, ivLocalView: self.cameraView, ivRemoteZ: self.zoomView)
  848. }
  849. }
  850. }
  851. present(UINavigationController(rootViewController: contactViewController), animated: true, completion: nil)
  852. }
  853. }
  854. @objc func camera(sender: Any?) {
  855. if frontCamera {
  856. API.changeCameraParam(nCameraIdx: 0, nResolutionIndex: 2, nQuality: 4)
  857. frontCamera = false
  858. } else {
  859. API.changeCameraParam(nCameraIdx: 1, nResolutionIndex: 2, nQuality: 4)
  860. frontCamera = true
  861. }
  862. }
  863. @objc func hideToolbar() {
  864. DispatchQueue.main.async {
  865. if self.showStackViewToolbar {
  866. self.showStackViewToolbar = false
  867. self.constraintBottomStackViewToolbar.constant = 150
  868. self.constraintLeftStackViewToolbar2.constant = -60
  869. UIView.animate(withDuration: 0.35, animations: {
  870. self.view.layoutIfNeeded()
  871. })
  872. } else {
  873. self.showStackViewToolbar = true
  874. self.constraintBottomStackViewToolbar.constant = -60
  875. self.constraintLeftStackViewToolbar2.constant = 10
  876. UIView.animate(withDuration: 0.35, animations: {
  877. self.view.layoutIfNeeded()
  878. })
  879. }
  880. }
  881. }
  882. @objc func onStatusCall(_ notification: NSNotification) {
  883. let data = notification.userInfo
  884. let state = (data?["state"] ?? 0) as! Int
  885. let message = (data?["message"] ?? "") as! String
  886. var remoteChannel = [String:String]()
  887. let arrayMessage = message.split(separator: ",")
  888. if(state == 35){
  889. DispatchQueue.main.async {
  890. if self.dataPerson.count > 1 {
  891. if !self.transformZoomAfterNewUserMore2 {
  892. self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 1.9).rotated(by: (CGFloat.pi * 3)/2)
  893. self.transformZoomAfterNewUserMore2 = true
  894. }
  895. }
  896. }
  897. }
  898. else if (state == 34){
  899. if(arrayMessage[3] == "0"){
  900. DispatchQueue.main.async {
  901. if self.dataPerson.count == 1 && arrayMessage[2] == "1" && arrayMessage[4] == "1" {
  902. self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 1.9).rotated(by: (CGFloat.pi * 3)/2)
  903. } else {
  904. self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 1.9).rotated(by: (CGFloat.pi)/2)
  905. }
  906. }
  907. }
  908. }
  909. else if (state == 32) {
  910. let channel = arrayMessage[3]
  911. remoteChannel[String(channel)] = String(arrayMessage[5])
  912. DispatchQueue.main.async {
  913. if (self.dataPerson.count == 1 && String(arrayMessage[1]) != self.dataPerson[0]["f_pin"]!!) {
  914. self.getDataProfile(fPin: String(arrayMessage[1]))
  915. for i in 0...1 {
  916. self.scrollRemoteView.addSubview(self.listRemoteViewFix[i])
  917. self.listRemoteViewFix[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 160)
  918. self.listRemoteViewFix[i].backgroundColor = .clear
  919. self.listRemoteViewFix[i].makeRoundedView(radius: 8.0)
  920. self.scrollRemoteView.addSubview(self.containerLabelName[i])
  921. self.containerLabelName[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 30)
  922. self.containerLabelName[i].backgroundColor = .orangeBNI.withAlphaComponent(0.5)
  923. self.containerLabelName[i].makeRoundedView(radius: 8.0)
  924. if i == 0 {
  925. if self.dataPerson[0]["user_type"] == "2" {
  926. self.listRemoteViewFix[0].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi)/2)
  927. } else {
  928. self.listRemoteViewFix[0].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi * 3 )/2)
  929. }
  930. } else {
  931. if arrayMessage[5] == "2" {
  932. self.listRemoteViewFix[1].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi)/2)
  933. } else {
  934. self.listRemoteViewFix[1].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi * 3 )/2)
  935. }
  936. }
  937. let pictureImage = self.dataPerson[i]["picture"] ?? ""
  938. let namePerson = self.dataPerson[i]["name"] ?? ""
  939. if (!pictureImage!.isEmpty) {
  940. self.listRemoteViewFix[i].setImage(name: pictureImage!)
  941. self.listRemoteViewFix[i].contentMode = .scaleAspectFill
  942. } else {
  943. self.listRemoteViewFix[i].image = UIImage(systemName: "person")
  944. self.listRemoteViewFix[i].backgroundColor = UIColor.systemGray6
  945. self.listRemoteViewFix[i].contentMode = .scaleAspectFit
  946. }
  947. let labelName = UILabel()
  948. self.containerLabelName[i].addSubview(labelName)
  949. labelName.anchor(left: self.containerLabelName[i].leftAnchor, right: self.containerLabelName[i].rightAnchor, paddingLeft: 5, paddingRight: 5, centerX: self.containerLabelName[i].centerXAnchor, centerY: self.containerLabelName[i].centerYAnchor)
  950. labelName.text = namePerson
  951. labelName.textAlignment = .center
  952. labelName.textColor = .white
  953. }
  954. self.scrollRemoteView.contentSize.height = CGFloat(170 * 2)
  955. } else if self.dataPerson.count > 1 {
  956. if self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[1]}) != nil {
  957. return
  958. }
  959. self.getDataProfile(fPin: String(arrayMessage[1]))
  960. let i = self.dataPerson.count - 1
  961. self.scrollRemoteView.addSubview(self.listRemoteViewFix[i])
  962. self.listRemoteViewFix[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 160)
  963. self.listRemoteViewFix[i].backgroundColor = .clear
  964. self.listRemoteViewFix[i].makeRoundedView(radius: 8.0)
  965. self.scrollRemoteView.addSubview(self.containerLabelName[i])
  966. self.containerLabelName[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 30)
  967. self.containerLabelName[i].backgroundColor = .orangeBNI.withAlphaComponent(0.5)
  968. self.containerLabelName[i].makeRoundedView(radius: 8.0)
  969. if arrayMessage[5] == "2" {
  970. self.listRemoteViewFix[i].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi)/2)
  971. } else {
  972. self.listRemoteViewFix[i].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi * 3 )/2)
  973. }
  974. let pictureImage = self.dataPerson[self.dataPerson.count - 1]["picture"] ?? ""
  975. let namePerson = self.dataPerson[self.dataPerson.count - 1]["name"] ?? ""
  976. if (!pictureImage!.isEmpty) {
  977. self.listRemoteViewFix[i].setImage(name: pictureImage!)
  978. self.listRemoteViewFix[i].contentMode = .scaleAspectFill
  979. } else {
  980. self.listRemoteViewFix[i].image = UIImage(systemName: "person")
  981. self.listRemoteViewFix[i].backgroundColor = UIColor.systemGray6
  982. self.listRemoteViewFix[i].contentMode = .scaleAspectFit
  983. }
  984. self.scrollRemoteView.contentSize.height = CGFloat(170 * (i + 1))
  985. let labelName = UILabel()
  986. self.containerLabelName[i].addSubview(labelName)
  987. labelName.anchor(left: self.containerLabelName[i].leftAnchor, right: self.containerLabelName[i].rightAnchor, paddingLeft: 5, paddingRight: 5, centerX: self.containerLabelName[i].centerXAnchor, centerY: self.containerLabelName[i].centerYAnchor)
  988. labelName.text = namePerson
  989. labelName.textAlignment = .center
  990. labelName.textColor = .white
  991. }
  992. }
  993. if arrayMessage[5] == "2" && self.dataPerson.count == 1 {
  994. DispatchQueue.main.async {
  995. self.zoomView.transform = CGAffineTransform.init(scaleX: -1.9, y: 1.9).rotated(by: (CGFloat.pi)/2)
  996. self.zoomView.contentMode = .scaleAspectFit
  997. }
  998. }
  999. else if self.dataPerson.count == 1 {
  1000. DispatchQueue.main.async {
  1001. self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 1.9).rotated(by: (CGFloat.pi * 3)/2)
  1002. self.zoomView.contentMode = .scaleAspectFit
  1003. }
  1004. } else if self.dataPerson.count > 1 {
  1005. DispatchQueue.main.async {
  1006. for i in 0..<self.dataPerson.count {
  1007. // self.listRemoteViewFix[i].image = self.listRemoteViewFix[i].image?.rotate(radians: (CGFloat.pi * 3 )/2)
  1008. self.listRemoteViewFix[i].image = nil
  1009. if self.dataPerson[i]["user_type"] == "2" || arrayMessage[5] == "2" {
  1010. self.listRemoteViewFix[i].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi)/2)
  1011. } else {
  1012. self.listRemoteViewFix[i].transform = CGAffineTransform.init(scaleX: 1.4, y: 1.3).rotated(by: (CGFloat.pi * 3 )/2)
  1013. }
  1014. }
  1015. }
  1016. }
  1017. DispatchQueue.main.async {
  1018. if self.isInisiator && self.name.isDescendant(of: self.view) {
  1019. self.didTapAcceptCallButton()
  1020. }
  1021. let indexPerson = self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[1]})
  1022. if indexPerson != nil {
  1023. self.dataPerson[indexPerson!]["user_type"] = String(arrayMessage[5])
  1024. }
  1025. }
  1026. } else if (state == 38 || state == 28) {
  1027. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  1028. if !onGoingCC.isEmpty {
  1029. let requester = onGoingCC.components(separatedBy: ",")[0]
  1030. let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
  1031. if arrayMessage[0] == requester || arrayMessage[0] == officer {
  1032. DispatchQueue.main.async {
  1033. let controller = self.presentedViewController
  1034. if controller != nil {
  1035. controller!.dismiss(animated: true)
  1036. }
  1037. if !self.showNotifCCEnd{
  1038. let imageView = UIImageView(image: UIImage(systemName: "info.circle"))
  1039. imageView.tintColor = .white
  1040. 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)
  1041. banner.show()
  1042. self.showNotifCCEnd = true
  1043. }
  1044. if self.stackViewToolbar.isDescendant(of: self.view){
  1045. self.stackViewToolbar.removeFromSuperview()
  1046. }
  1047. if self.stackViewToolbar2.isDescendant(of: self.view){
  1048. self.stackViewToolbar2.removeFromSuperview()
  1049. }
  1050. if self.buttonWB.isDescendant(of: self.view){
  1051. self.buttonWB.removeFromSuperview()
  1052. }
  1053. if self.buttonChat.isDescendant(of: self.view){
  1054. self.buttonChat.removeFromSuperview()
  1055. }
  1056. if self.buttonDecline.isDescendant(of: self.view) {
  1057. self.buttonDecline.removeFromSuperview()
  1058. }
  1059. if self.buttonAccept.isDescendant(of: self.view) {
  1060. self.buttonAccept.removeFromSuperview()
  1061. }
  1062. if self.buttonRotate.isDescendant(of: self.view) {
  1063. self.buttonRotate.removeFromSuperview()
  1064. }
  1065. if self.wbVC != nil{
  1066. self.wbVC!.close?()
  1067. }
  1068. self.wbTimer.invalidate()
  1069. _ = Nexilis.getWhiteboardDelegate()?.terminate()
  1070. }
  1071. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  1072. self.endAllCall()
  1073. self.dismiss(animated: true, completion: nil)
  1074. }
  1075. return
  1076. }
  1077. }
  1078. DispatchQueue.main.async {
  1079. if (self.dataPerson.count == 1) {
  1080. if self.labelIncomingOutgoing.isDescendant(of: self.view) {
  1081. self.labelIncomingOutgoing.text = "Video call is over".localized()
  1082. }
  1083. if self.stackViewToolbar.isDescendant(of: self.view){
  1084. self.stackViewToolbar.removeFromSuperview()
  1085. }
  1086. if self.stackViewToolbar2.isDescendant(of: self.view){
  1087. self.stackViewToolbar2.removeFromSuperview()
  1088. }
  1089. if self.buttonWB.isDescendant(of: self.view){
  1090. self.buttonWB.removeFromSuperview()
  1091. }
  1092. if self.buttonChat.isDescendant(of: self.view){
  1093. self.buttonChat.removeFromSuperview()
  1094. }
  1095. if self.buttonDecline.isDescendant(of: self.view) {
  1096. self.buttonDecline.removeFromSuperview()
  1097. }
  1098. if self.buttonAccept.isDescendant(of: self.view) {
  1099. self.buttonAccept.removeFromSuperview()
  1100. }
  1101. if self.buttonRotate.isDescendant(of: self.view) {
  1102. self.buttonRotate.removeFromSuperview()
  1103. }
  1104. if self.wbVC != nil{
  1105. self.wbVC!.close?()
  1106. }
  1107. self.wbTimer.invalidate()
  1108. self.vcTimer.invalidate()
  1109. self.labelTimerVC.text = "Video call is over".localized()
  1110. _ = Nexilis.getWhiteboardDelegate()?.terminate()
  1111. let controller = self.presentedViewController
  1112. if controller != nil {
  1113. controller!.dismiss(animated: true)
  1114. }
  1115. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  1116. self.endAllCall()
  1117. if self.isInisiator && !self.isPresent {
  1118. self.navigationController?.popViewController(animated: true)
  1119. } else {
  1120. self.dismiss(animated: true, completion: nil)
  1121. }
  1122. }
  1123. } else {
  1124. let indexPerson = self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[0]})
  1125. if indexPerson != nil {
  1126. if (self.dataPerson.count == 2) {
  1127. self.containerLabelName.forEach({ $0.subviews.forEach({ $0.removeFromSuperview() }) })
  1128. self.scrollRemoteView.subviews.forEach({ $0.removeFromSuperview() })
  1129. } else {
  1130. self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
  1131. self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
  1132. self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
  1133. self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
  1134. if indexPerson! + 1 <= self.listRemoteViewFix.count {
  1135. let iLoop = (self.listRemoteViewFix.count - 1) - (indexPerson! + 1)
  1136. if iLoop >= 0 {
  1137. for i in 0...iLoop {
  1138. let viewAfterRemote = self.listRemoteViewFix[(indexPerson! + i) + 1]
  1139. let viewAfterName = self.containerLabelName[(indexPerson! + i) + 1]
  1140. viewAfterRemote.frame.origin.y = viewAfterRemote.frame.origin.y - 170
  1141. viewAfterName.frame.origin.y = viewAfterName.frame.origin.y - 170
  1142. UIView.animate(withDuration: 0.35, animations: {
  1143. self.scrollRemoteView.layoutIfNeeded()
  1144. })
  1145. }
  1146. }
  1147. }
  1148. }
  1149. self.dataPerson.remove(at: indexPerson!)
  1150. }
  1151. if !onGoingCC.isEmpty {
  1152. if let pin = arrayMessage.first, let index = self.users.firstIndex(of: User(pin: String(pin))) {
  1153. self.users.remove(at: index)
  1154. }
  1155. }
  1156. if self.dataPerson.count == 1 {
  1157. self.transformZoomAfterNewUserMore2 = false
  1158. self.zoomView.transform = CGAffineTransform.init(scaleX: 1.9, y: 1.9).rotated(by: (CGFloat.pi)/2)
  1159. }
  1160. }
  1161. }
  1162. } else if (state == -3) {
  1163. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  1164. DispatchQueue.main.async {
  1165. if (self.dataPerson.count == 1) {
  1166. if self.labelIncomingOutgoing.isDescendant(of: self.view) {
  1167. self.labelIncomingOutgoing.text = "Offline".localized()
  1168. }
  1169. if self.buttonDecline.isDescendant(of: self.view) {
  1170. self.buttonDecline.removeFromSuperview()
  1171. }
  1172. if self.buttonAccept.isDescendant(of: self.view) {
  1173. self.buttonAccept.removeFromSuperview()
  1174. }
  1175. } else {
  1176. let indexPerson = self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[0]})
  1177. if indexPerson != nil {
  1178. if (self.dataPerson.count == 2) {
  1179. self.containerLabelName.forEach({ $0.subviews.forEach({ $0.removeFromSuperview() }) })
  1180. self.scrollRemoteView.subviews.forEach({ $0.removeFromSuperview() })
  1181. } else {
  1182. self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
  1183. self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
  1184. self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
  1185. self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
  1186. if indexPerson! + 1 <= self.listRemoteViewFix.count {
  1187. let viewAfterRemote = self.listRemoteViewFix[indexPerson! + 1]
  1188. let viewAfterName = self.containerLabelName[indexPerson! + 1]
  1189. viewAfterRemote.frame.origin.y = viewAfterRemote.frame.origin.y - 170
  1190. viewAfterName.frame.origin.y = viewAfterName.frame.origin.y - 170
  1191. UIView.animate(withDuration: 0.35, animations: {
  1192. self.scrollRemoteView.layoutIfNeeded()
  1193. })
  1194. }
  1195. }
  1196. }
  1197. if !onGoingCC.isEmpty {
  1198. if let pin = arrayMessage.first, let index = self.users.firstIndex(of: User(pin: String(pin))) {
  1199. self.users.remove(at: index)
  1200. if !onGoingCC.isEmpty && self.users.count != 0 {
  1201. DispatchQueue.main.async {
  1202. var members = ""
  1203. for user in self.users {
  1204. if members.isEmpty {
  1205. members = "\(user.pin)"
  1206. } else {
  1207. members = ",\(user.pin)"
  1208. }
  1209. }
  1210. UserDefaults.standard.set("\(members)", forKey: "membersCC")
  1211. }
  1212. }
  1213. }
  1214. }
  1215. self.dataPerson.remove(at: indexPerson!)
  1216. }
  1217. }
  1218. if (self.dataPerson.count == 1) {
  1219. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  1220. self.endAllCall()
  1221. if self.isInisiator && onGoingCC.isEmpty && !self.isPresent {
  1222. self.navigationController?.popViewController(animated: true)
  1223. } else {
  1224. self.dismiss(animated: true, completion: nil)
  1225. }
  1226. }
  1227. }
  1228. } else if (state == -4) {
  1229. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  1230. DispatchQueue.main.async { [self] in
  1231. if (self.dataPerson.count == 1) {
  1232. if self.labelIncomingOutgoing.isDescendant(of: self.view) {
  1233. self.labelIncomingOutgoing.text = "Busy".localized()
  1234. }
  1235. if self.buttonDecline.isDescendant(of: self.view) {
  1236. self.buttonDecline.removeFromSuperview()
  1237. }
  1238. if self.buttonAccept.isDescendant(of: self.view) {
  1239. self.buttonAccept.removeFromSuperview()
  1240. }
  1241. } else {
  1242. let indexPerson = self.dataPerson.firstIndex(where: {$0["f_pin"]!! == arrayMessage[0]})
  1243. if indexPerson != nil {
  1244. if (self.dataPerson.count == 2) {
  1245. self.containerLabelName.forEach({ $0.subviews.forEach({ $0.removeFromSuperview() }) })
  1246. self.scrollRemoteView.subviews.forEach({ $0.removeFromSuperview() })
  1247. } else {
  1248. self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
  1249. self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
  1250. self.containerLabelName[indexPerson! + indexPerson!].subviews.forEach({ $0.removeFromSuperview() })
  1251. self.scrollRemoteView.subviews[indexPerson! + indexPerson!].removeFromSuperview()
  1252. if indexPerson! + 1 <= self.listRemoteViewFix.count {
  1253. let viewAfterRemote = self.listRemoteViewFix[indexPerson! + 1]
  1254. let viewAfterName = self.containerLabelName[indexPerson! + 1]
  1255. viewAfterRemote.frame.origin.y = viewAfterRemote.frame.origin.y - 170
  1256. viewAfterName.frame.origin.y = viewAfterName.frame.origin.y - 170
  1257. UIView.animate(withDuration: 0.35, animations: {
  1258. self.scrollRemoteView.layoutIfNeeded()
  1259. })
  1260. }
  1261. }
  1262. }
  1263. if !onGoingCC.isEmpty {
  1264. if let pin = arrayMessage.first, let index = self.users.firstIndex(of: User(pin: String(pin))) {
  1265. self.users.remove(at: index)
  1266. if !onGoingCC.isEmpty && users.count != 0 {
  1267. DispatchQueue.main.async {
  1268. var members = ""
  1269. for user in self.users {
  1270. if members.isEmpty {
  1271. members = "\(user.pin)"
  1272. } else {
  1273. members = ",\(user.pin)"
  1274. }
  1275. }
  1276. UserDefaults.standard.set("\(members)", forKey: "membersCC")
  1277. }
  1278. }
  1279. }
  1280. }
  1281. self.dataPerson.remove(at: indexPerson!)
  1282. }
  1283. }
  1284. if (self.dataPerson.count == 1) {
  1285. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  1286. self.endAllCall()
  1287. if self.isInisiator && onGoingCC.isEmpty && !self.isPresent {
  1288. self.navigationController?.popViewController(animated: true)
  1289. } else {
  1290. self.dismiss(animated: true, completion: nil)
  1291. }
  1292. }
  1293. }
  1294. } else if (state == 33) {
  1295. DispatchQueue.main.async {
  1296. if (self.dataPerson.count > 1) {
  1297. if (self.dataPerson.count == 2) {
  1298. for i in 0...1{
  1299. self.scrollRemoteView.addSubview(self.listRemoteViewFix[i])
  1300. self.listRemoteViewFix[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 160)
  1301. self.listRemoteViewFix[i].backgroundColor = .clear
  1302. self.listRemoteViewFix[i].makeRoundedView(radius: 8.0)
  1303. self.scrollRemoteView.addSubview(self.containerLabelName[i])
  1304. self.containerLabelName[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 30)
  1305. self.containerLabelName[i].backgroundColor = .orangeBNI.withAlphaComponent(0.5)
  1306. self.containerLabelName[i].makeRoundedView(radius: 8.0)
  1307. let pictureImage = self.dataPerson[i]["picture"] ?? ""
  1308. let namePerson = self.dataPerson[i]["name"] ?? ""
  1309. if (!pictureImage!.isEmpty) {
  1310. self.listRemoteViewFix[i].setImage(name: pictureImage!)
  1311. self.listRemoteViewFix[i].contentMode = .scaleAspectFill
  1312. } else {
  1313. self.listRemoteViewFix[i].image = UIImage(systemName: "person")
  1314. self.listRemoteViewFix[i].backgroundColor = UIColor.systemGray6
  1315. self.listRemoteViewFix[i].contentMode = .scaleAspectFit
  1316. }
  1317. let labelName = UILabel()
  1318. self.containerLabelName[i].addSubview(labelName)
  1319. labelName.anchor(left: self.containerLabelName[i].leftAnchor, right: self.containerLabelName[i].rightAnchor, paddingLeft: 5, paddingRight: 5, centerX: self.containerLabelName[i].centerXAnchor, centerY: self.containerLabelName[i].centerYAnchor)
  1320. labelName.text = namePerson
  1321. labelName.textAlignment = .center
  1322. labelName.textColor = .white
  1323. }
  1324. self.scrollRemoteView.contentSize.height = CGFloat(170 * 2)
  1325. } else {
  1326. let i = self.dataPerson.count - 1
  1327. self.scrollRemoteView.addSubview(self.listRemoteViewFix[i])
  1328. self.listRemoteViewFix[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 160)
  1329. self.listRemoteViewFix[i].backgroundColor = .clear
  1330. self.listRemoteViewFix[i].makeRoundedView(radius: 8.0)
  1331. self.scrollRemoteView.addSubview(self.containerLabelName[i])
  1332. self.containerLabelName[i].frame = CGRect(x: 0, y: 170 * i, width: 120, height: 30)
  1333. self.containerLabelName[i].backgroundColor = .orangeBNI.withAlphaComponent(0.5)
  1334. self.containerLabelName[i].makeRoundedView(radius: 8.0)
  1335. let pictureImage = self.dataPerson[self.dataPerson.count - 1]["picture"] ?? ""
  1336. let namePerson = self.dataPerson[self.dataPerson.count - 1]["name"] ?? ""
  1337. if (!pictureImage!.isEmpty) {
  1338. self.listRemoteViewFix[i].setImage(name: pictureImage!)
  1339. self.listRemoteViewFix[i].contentMode = .scaleAspectFill
  1340. } else {
  1341. self.listRemoteViewFix[i].image = UIImage(systemName: "person")
  1342. self.listRemoteViewFix[i].backgroundColor = UIColor.systemGray6
  1343. self.listRemoteViewFix[i].contentMode = .scaleAspectFit
  1344. }
  1345. self.scrollRemoteView.contentSize.height = CGFloat(170 * (i + 1))
  1346. let labelName = UILabel()
  1347. self.containerLabelName[i].addSubview(labelName)
  1348. labelName.anchor(left: self.containerLabelName[i].leftAnchor, right: self.containerLabelName[i].rightAnchor, paddingLeft: 5, paddingRight: 5, centerX: self.containerLabelName[i].centerXAnchor, centerY: self.containerLabelName[i].centerYAnchor)
  1349. labelName.text = namePerson
  1350. labelName.textAlignment = .center
  1351. labelName.textColor = .white
  1352. }
  1353. }
  1354. }
  1355. }
  1356. }
  1357. }
  1358. extension QmeraVideoViewController : WhiteboardReceiver {
  1359. func incomingWB(roomId: String) {
  1360. //print("incoming wb")
  1361. self.wbTimer.invalidate()
  1362. if(wbRoomId.isEmpty){
  1363. //print("wbroom empty")
  1364. DispatchQueue.main.async {
  1365. self.wbTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.runTimer), userInfo: nil, repeats: true)
  1366. }
  1367. let me = UserDefaults.standard.string(forKey: "me")!
  1368. var destString = ""
  1369. for d in dataPerson{
  1370. if d["f_pin"]!! == roomId.components(separatedBy: "wbvc")[0] {
  1371. continue
  1372. }
  1373. if destString.isEmpty{
  1374. destString = d["f_pin"]!!
  1375. } else {
  1376. destString = destString + ",\(d["f_pin"]!!)"
  1377. }
  1378. }
  1379. if destString.isEmpty {
  1380. UserDefaults.standard.set("\(roomId.components(separatedBy: "wbvc")[0]),\(me)", forKey: "wb_vc")
  1381. } else {
  1382. UserDefaults.standard.set("\(roomId.components(separatedBy: "wbvc")[0]),\(me),\(destString)", forKey: "wb_vc")
  1383. }
  1384. wbRoomId = roomId
  1385. }
  1386. }
  1387. func cancel(roomId: String) {
  1388. DispatchQueue.main.async {
  1389. self.wbTimer.invalidate()
  1390. self.wbBlink = false
  1391. self.buttonWB.backgroundColor = .lightGray
  1392. self.buttonWB.setNeedsDisplay()
  1393. }
  1394. wbRoomId = ""
  1395. }
  1396. @objc func runTimer(){
  1397. DispatchQueue.main.async {
  1398. self.wbBlink = !self.wbBlink
  1399. if(self.wbBlink){
  1400. //print("set wb blink on")
  1401. self.buttonWB.backgroundColor = .green
  1402. }
  1403. else {
  1404. //print("set wb blink off")
  1405. self.buttonWB.backgroundColor = .lightGray
  1406. }
  1407. self.buttonWB.setNeedsDisplay()
  1408. }
  1409. }
  1410. }