1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123 |
- //
- // OutgoingViewController.swift
- // Qmera
- //
- // Created by Yayan Dwi on 07/10/21.
- //
- import UIKit
- import AVFoundation
- import nuSDKService
- import NotificationBannerSwift
- class QmeraAudioViewController: UIViewController {
-
- let stackViewToolbar2 = UIStackView()
- var onScreenConstraintWB = [NSLayoutConstraint]()
- let buttonWB = UIButton()
- let buttonChat = UIButton()
- var wbVC : WhiteboardViewController?
- var wbTimer = Timer()
- var wbBlink = false
- var wbRoomId = ""
- var callFCM = false
-
- let buttonSize: CGFloat = 70
-
- lazy var data: String = "" {
- didSet {
- getUserData { user in
- self.user = user
- }
- }
- }
-
- var user: User?
-
- var isAddCall = ""
-
- private var isEndByMe = false
-
- private var users: [User] = [] {
- didSet {
- DispatchQueue.main.async {
- if oldValue.count > self.users.count { // remove
- let remove = oldValue.filter { !self.users.contains($0) }
- remove.forEach { user in
- if let subviews = self.profiles.subviews as? [ProfileView] {
- subviews.forEach { p in
- if p.user == user {
- self.profiles.removeArrangeSubview(view: p)
- }
- }
- }
- }
- } else {
- if let user = self.users.last {
- let profile = ProfileView(image: UIImage(systemName: "person.circle.fill"))
- profile.user = user
- self.profiles.addArrangedSubview(view: profile)
- }
- }
- self.name.text = self.users.map { $0.fullName }.joined(separator: ", ")
- }
- }
- }
-
- var isOutgoing: Bool = true
-
- var isOnGoing: Bool = false
-
- var ticketId: String = ""
-
- private var timer: Timer?
-
- private var firstCall: Bool = true
-
- private var isSpeaker: Bool = false
-
- private var isMuted: Bool = false
-
- var listRemoteViewFix: [UIImageView] = [
- UIImageView(),
- UIImageView(),
- UIImageView(),
- UIImageView(),
- UIImageView()
- ]
-
- let zoomView = UIImageView()
- let cameraView = UIImageView()
-
- let status: UILabel = {
- let label = UILabel()
- label.text = "Calling..."
- label.font = UIFont.systemFont(ofSize: 14)
- label.textColor = .white
- label.textAlignment = .center
- return label
- }()
-
- let profiles: GroupView = {
- let groupView = GroupView()
- groupView.spacing = 50
- groupView.maxUser = 3
- return groupView
- }()
-
- let name: UILabel = {
- let label = UILabel()
- label.text = "uwitan"
- label.font = UIFont.systemFont(ofSize: 14)
- label.textColor = .white
- label.textAlignment = .center
- return label
- }()
-
- let end: UIButton = {
- let button = UIButton()
- button.setImage(UIImage(systemName: "phone.down"), for: .normal)
- button.imageView?.contentMode = .scaleAspectFit
- button.imageView?.tintColor = .white
- button.setBackgroundColor(.red, for: .normal)
- button.setBackgroundColor(.white, for: .highlighted)
- button.contentVerticalAlignment = .fill
- button.contentHorizontalAlignment = .fill
- button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
- return button
- }()
-
- let reject: UIButton = {
- let button = UIButton()
- let image = UIImage(systemName: "xmark")
- button.setImage(image, for: .normal)
- let selectedImage = image?.withTintColor(.mainColor)
- button.setImage(selectedImage, for: .selected)
- button.imageView?.contentMode = .scaleAspectFit
- button.imageView?.tintColor = .white
- button.setBackgroundColor(.red, for: .normal)
- button.setBackgroundColor(.white, for: .highlighted)
- button.contentVerticalAlignment = .fill
- button.contentHorizontalAlignment = .fill
- button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
- return button
- }()
-
- let accept: UIButton = {
- let button = UIButton()
- let image = UIImage(systemName: "checkmark")
- button.setImage(image, for: .normal)
- button.imageView?.contentMode = .scaleAspectFit
- button.imageView?.tintColor = .white
- button.setBackgroundColor(.greenColor, for: .normal)
- button.setBackgroundColor(.white, for: .highlighted)
- button.contentVerticalAlignment = .fill
- button.contentHorizontalAlignment = .fill
- button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
- return button
- }()
-
- let invite: UIButton = {
- let button = UIButton()
- let image = UIImage(systemName: "person.badge.plus")
- button.setImage(image, for: .normal)
- button.imageView?.contentMode = .scaleAspectFit
- button.imageView?.tintColor = .mainColor
- button.setBackgroundColor(.white, for: .normal)
- button.setBackgroundColor(.mainColor, for: .highlighted)
- button.contentVerticalAlignment = .fill
- button.contentHorizontalAlignment = .fill
- button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
- return button
- }()
-
- let speaker: UIButton = {
- let button = UIButton()
- button.setImage(UIImage(systemName: "speaker.slash")?.withTintColor(.mainColor, renderingMode: .alwaysOriginal), for: .normal)
- button.setImage(UIImage(systemName: "speaker.wave.3")?.withTintColor(.white, renderingMode: .alwaysOriginal), for: .selected)
- button.imageView?.contentMode = .scaleAspectFit
- button.setBackgroundColor(.white, for: .normal)
- button.setBackgroundColor(.mainColor, for: .highlighted)
- button.setBackgroundColor(.mainColor, for: .selected)
- button.contentVerticalAlignment = .fill
- button.contentHorizontalAlignment = .fill
- button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
- return button
- }()
-
- let mic: UIButton = {
- let button = UIButton()
- button.setImage(UIImage(systemName: "mic")?.withTintColor(.mainColor, renderingMode: .alwaysOriginal), for: .normal)
- button.setImage(UIImage(systemName: "mic.slash")?.withTintColor(.white, renderingMode: .alwaysOriginal), for: .selected)
- button.imageView?.contentMode = .scaleAspectFit
- button.setBackgroundColor(.white, for: .normal)
- button.setBackgroundColor(.mainColor, for: .highlighted)
- button.setBackgroundColor(.mainColor, for: .selected)
- button.contentVerticalAlignment = .fill
- button.contentHorizontalAlignment = .fill
- button.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
- return button
- }()
-
- let stack: UIStackView = {
- let stackView = UIStackView()
- stackView.axis = .horizontal
- stackView.distribution = .fillEqually
- return stackView
- }()
- let stack2: UIStackView = {
- let stackView = UIStackView()
- stackView.axis = .horizontal
- stackView.distribution = .fillEqually
- return stackView
- }()
-
- let poweredByView: UIStackView = {
- let stackView = UIStackView()
- stackView.axis = .horizontal
- stackView.spacing = 5
- return stackView
- }()
-
- let poweredByLabel: UILabel = {
- let label = UILabel()
- label.text = "Powered by Nexilis".localized()
- return label
- }()
-
- let qmeraLogo: UIButton = {
- let image = UIImage(named: "Q-Button-PNG", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
- let button = UIButton()
- button.setImage(image, for: .normal)
- button.imageView?.contentMode = .scaleAspectFit
- button.imageEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
- button.contentVerticalAlignment = .fill
- button.contentHorizontalAlignment = .fill
- // button.frame.size.width = 30
- // button.frame.size.height = 30
- return button
- }()
-
- let nexilisLogo: UIButton = {
- let image = UIImage(named: "pb_powered_button", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
- let button = UIButton()
- button.setImage(image, for: .normal)
- button.imageView?.contentMode = .scaleAspectFit
- button.imageEdgeInsets = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
- button.contentVerticalAlignment = .fill
- button.contentHorizontalAlignment = .fill
- // button.frame.size.width = 30
- // button.frame.size.height = 30
- return button
- }()
-
- override func viewWillDisappear(_ animated: Bool) {
- UIDevice.current.isProximityMonitoringEnabled = false
- NotificationCenter.default.removeObserver(self)
- Nexilis.floatingButton.isHidden = false
- }
-
- deinit {
- UIDevice.current.isProximityMonitoringEnabled = false
- NotificationCenter.default.removeObserver(self)
- Nexilis.floatingButton.isHidden = false
- }
-
- override func viewDidAppear(_ animated: Bool) {
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "onShowAC"), object: nil, userInfo: nil)
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- Nexilis.floatingButton.isHidden = true
-
- let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .systemUltraThinMaterialDark))
- effectView.frame = view.frame
- view.insertSubview(effectView, at: 0)
-
- view.addSubview(status)
- view.addSubview(profiles)
- view.addSubview(name)
-
- status.anchor(left: view.leftAnchor, bottom: profiles.topAnchor, right: view.rightAnchor, paddingBottom: 30, centerX: view.centerXAnchor)
- profiles.anchor(centerX: view.centerXAnchor, centerY: view.centerYAnchor, width: 150, height: 150)
- name.anchor(top: profiles.bottomAnchor, left: view.leftAnchor, right: view.rightAnchor, paddingTop: 5, paddingLeft: 20, paddingRight: 20, centerX: view.centerXAnchor)
- definesPresentationContext = true
-
- if isOutgoing {
- outgoingView()
- } else if isOnGoing {
- ongoingView()
- } else {
- incomingView()
- }
-
- UIDevice.current.isProximityMonitoringEnabled = true
-
- NotificationCenter.default.addObserver(self, selector: #selector(onStatusCall(_:)), name: NSNotification.Name(rawValue: Nexilis.listenerStatusCall), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(onReceiveMessage(notification:)), name: NSNotification.Name(rawValue: Nexilis.listenerReceiveChat), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(onCallFCM(notification:)), name: NSNotification.Name(rawValue: Nexilis.callFCM), object: nil)
-
- if let u = self.user {
- self.users.append(u)
- if isOutgoing && ticketId.isEmpty {
- // if onGoingCC.isEmpty {
- // Nexilis.shared.callManager.startCall(handle: u.pin)
- // } else {
- // API.initiateCCall(sParty: u.pin)
- // }
- if callFCM {
- DispatchQueue.global().async {
- if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getCalling(fPin: u.pin, type: "1"), timeout: 30 * 1000) {
- if response.isOk() {
- } else {
- DispatchQueue.main.async {
- self.status.text = "Busy"
- self.end.isEnabled = false
- }
- }
- } else {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Unable to access servers. Try again later".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
- banner.show()
- }
- }
- } else {
- Nexilis.ringbacktonePlayer?.play()
- API.initiateCCall(sParty: u.pin)
- }
- } else if !ticketId.isEmpty {
- if isOutgoing {
- API.ccs(sTicketID: ticketId, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView, bCameraOn: false)
- if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getIncomingCallCS(f_pin_opposite: u.pin), timeout: 30 * 1000){
- if response.mBodies[CoreMessage_TMessageKey.ERRCOD] != "01" {
- self.didEnd(sender: true)
- }
- }
- } else {
- API.csa(sTicketID: ticketId, nCamIdx: 1, nResIdx: 2, nVQuality: 4, ivRemoteView: listRemoteViewFix, ivLocalView: cameraView, ivRemoteZ: zoomView, bCameraOn: false)
- }
- }
- }
- }
-
- @objc func onCallFCM(notification: NSNotification) {
- DispatchQueue.main.async {
- let data:[AnyHashable : Any] = notification.userInfo!
- if let l_pin = data["l_pin"] as? String {
- Nexilis.ringbacktonePlayer?.play()
- API.initiateCCall(sParty: l_pin)
- }
- }
- }
-
- override func viewWillLayoutSubviews() {
- super.viewWillLayoutSubviews()
- end.circle()
- reject.circle()
- accept.circle()
- invite.circle()
- speaker.circle()
- mic.circle()
- }
-
- private func getUserData(completion: @escaping (User?) -> ()) {
- if let user = self.user {
- completion(user)
- return
- }
- var user: User?
- DispatchQueue.global().async {
- Database.shared.database?.inTransaction({ fmdb, rollback in
- do {
- 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() {
- user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
- firstName: cursor.string(forColumnIndex: 1) ?? "",
- lastName: cursor.string(forColumnIndex: 2) ?? "",
- thumb: cursor.string(forColumnIndex: 3) ?? "")
- cursor.close()
- }
- } catch {
- rollback.pointee = true
- print("Access database error: \(error.localizedDescription)")
- }
- })
- }
- completion(user)
- }
-
- private func outgoingView() {
- view.addSubview(end)
- end.anchor(bottom: view.bottomAnchor, paddingBottom: 60, centerX: view.centerXAnchor, width: buttonSize, height: buttonSize)
-
- end.addTarget(self, action: #selector(didPressEnd(sender:)), for: .touchUpInside)
- }
-
- private func incomingView() {
- status.text = "Incoming..."
-
- stack.spacing = buttonSize
-
- view.addSubview(stack)
-
- stack.anchor(bottom: view.bottomAnchor, paddingBottom: 60, centerX: view.centerXAnchor, width: buttonSize * 3, height: buttonSize)
-
- stack.addArrangedSubview(reject)
- stack.addArrangedSubview(accept)
-
- reject.addTarget(self, action: #selector(didReject(sender:)), for: .touchUpInside)
- accept.addTarget(self, action: #selector(didAccept(sender:)), for: .touchUpInside)
- }
-
- private func ongoingView() {
- status.text = "Connecting..."
-
- stack.spacing = buttonSize / 2
- stack2.spacing = buttonSize / 2
-
- view.addSubview(stack)
- view.addSubview(stack2)
-
- stack.anchor(bottom: view.bottomAnchor, paddingBottom: 60, centerX: view.centerXAnchor, width: buttonSize * 4, height: buttonSize)
- stack2.anchor(bottom: stack.topAnchor, paddingBottom: 10, centerX: view.centerXAnchor, width: buttonSize, height: buttonSize)
-
- stack.addArrangedSubview(invite)
- stack.addArrangedSubview(end)
- stack.addArrangedSubview(speaker)
- stack2.addArrangedSubview(mic)
-
- invite.addTarget(self, action: #selector(didInvite(sender:)), for: .touchUpInside)
- end.addTarget(self, action: #selector(didPressEnd(sender:)), for: .touchUpInside)
- speaker.addTarget(self, action: #selector(didSpeaker(sender:)), for: .touchUpInside)
- mic.addTarget(self, action: #selector(didMute(sender:)), for: .touchUpInside)
-
- if !ticketId.isEmpty {
- self.view.addSubview(self.stackViewToolbar2)
- self.stackViewToolbar2.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- self.stackViewToolbar2.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
- self.stackViewToolbar2.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 10.0)
- ])
- self.stackViewToolbar2.axis = .vertical
- self.stackViewToolbar2.distribution = .equalSpacing
- self.stackViewToolbar2.alignment = .center
- self.stackViewToolbar2.spacing = 5
-
- view.addSubview(buttonWB)
- buttonWB.translatesAutoresizingMaskIntoConstraints = false
- buttonWB.frame.size = CGSize(width: 40.0, height: 40.0)
- NSLayoutConstraint.activate([
- buttonWB.widthAnchor.constraint(equalToConstant: 40.0),
- buttonWB.heightAnchor.constraint(equalToConstant: 40.0)
- ])
- buttonWB.backgroundColor = .lightGray
- buttonWB.setImage(UIImage(systemName: "ipad.landscape", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .medium, scale: .default)), for: .normal)
- buttonWB.circle()
- buttonWB.tintColor = .black
- buttonWB.addTarget(self, action: #selector(didTapWBButton), for: .touchUpInside)
-
- view.addSubview(buttonChat)
- buttonChat.translatesAutoresizingMaskIntoConstraints = false
- buttonChat.frame.size = CGSize(width: 40.0, height: 40.0)
- NSLayoutConstraint.activate([
- buttonChat.widthAnchor.constraint(equalToConstant: 40.0),
- buttonChat.heightAnchor.constraint(equalToConstant: 40.0)
- ])
- buttonChat.backgroundColor = .lightGray
- buttonChat.setImage(UIImage(systemName: "bubble.right", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .medium, scale: .default)), for: .normal)
- buttonChat.circle()
- buttonChat.tintColor = .black
- buttonChat.addTarget(self, action: #selector(didTapChatButton), for: .touchUpInside)
- }
-
- self.view.addSubview(poweredByView)
- self.poweredByView.translatesAutoresizingMaskIntoConstraints = false
- let constraintRightPowered = self.poweredByView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -10.0)
- let constraintBottomPowered = self.poweredByView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10.0)
- NSLayoutConstraint.activate([
- constraintRightPowered,
- constraintBottomPowered,
- nexilisLogo.widthAnchor.constraint(equalToConstant: 30.0),
- nexilisLogo.heightAnchor.constraint(equalToConstant: 30.0)
- ])
-
- poweredByView.addArrangedSubview(poweredByLabel)
- poweredByView.addArrangedSubview(nexilisLogo)
- stackViewToolbar2.addArrangedSubview(buttonWB)
- stackViewToolbar2.addArrangedSubview(buttonChat)
-
- }
-
-
-
-
- // MARK: - Action
-
- @objc func didTapChatButton(){
- let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
- let members: String = SecureUserDefaults.shared.value(forKey: "membersCC") ?? ""
- let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
- let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
- editorPersonalVC.hidesBottomBarWhenPushed = true
- editorPersonalVC.unique_l_pin = officer
- editorPersonalVC.fromNotification = true
- editorPersonalVC.isContactCenter = true
- editorPersonalVC.fPinContacCenter = members
- editorPersonalVC.complaintId = ticketId
- editorPersonalVC.onGoingCC = true
- editorPersonalVC.isRequestContactCenter = false
- editorPersonalVC.users = users
- editorPersonalVC.fromVCAC = true
- let navigationController = CustomNavigationController(rootViewController: editorPersonalVC)
- navigationController.modalPresentationStyle = .overCurrentContext
- navigationController.navigationBar.tintColor = .white
- navigationController.navigationBar.barTintColor = self.traitCollection.userInterfaceStyle == .dark ? .blackDarkMode : .mainColor
- navigationController.navigationBar.isTranslucent = false
- navigationController.navigationBar.overrideUserInterfaceStyle = .dark
- navigationController.navigationBar.barStyle = .black
- let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]
- UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, for: .normal)
- let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
- navigationController.navigationBar.titleTextAttributes = textAttributes
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
- }
- }
-
- @objc func didTapWBButton(){
- if(wbVC == nil){
- wbVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "wbVC") as? WhiteboardViewController
- if(wbRoomId.isEmpty){
- let me = User.getMyPin()!
- let tid = CoreMessage_TMessageUtil.getTID()
- wbRoomId = "\(me)wbvc\(tid)"
- wbVC!.roomId = wbRoomId
- var destinations = [String]()
- var destString = ""
- for d in users{
- destinations.append(d.pin)
- if destString.isEmpty{
- destString = d.pin
- } else {
- destString = destString + ",\(d.pin)"
- }
- }
- wbVC!.destinations = destinations
- wbVC!.sendInit()
- SecureUserDefaults.shared.set("\(me),\(destString)", forKey: "wb_vc")
- }
- else {
- self.wbTimer.invalidate()
- self.buttonWB.backgroundColor = .lightGray
- wbVC!.roomId = wbRoomId
- wbVC!.sendJoin()
- }
- }
- wbVC!.close = {
- DispatchQueue.main.async {
- if self.wbVC!.view.isDescendant(of: self.view){
- self.wbVC!.view.removeFromSuperview()
- }
- // self.buttonDecline.isHidden = false
- // self.buttonSpeaker.isHidden = false
- // self.buttonAddParticipant.isHidden = false
- // self.buttonRotate.isHidden = false
- // if(!self.wbRoomId.isEmpty){
- // DispatchQueue.main.async {
- // self.wbTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.runTimer), userInfo: nil, repeats: true)
- // }
- // }
- }
- }
- // self.buttonDecline.isHidden = true
- // self.buttonSpeaker.isHidden = true
- // self.buttonAddParticipant.isHidden = true
- // self.buttonRotate.isHidden = true
- addChild(wbVC!)
- wbVC!.view.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(wbVC!.view)
- onScreenConstraintWB = [
- wbVC!.view.topAnchor.constraint(equalTo: self.view.topAnchor),
- wbVC!.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
- wbVC!.view.rightAnchor.constraint(equalTo: self.view.rightAnchor),
- wbVC!.view.leftAnchor.constraint(equalTo: self.view.leftAnchor),
- ]
- NSLayoutConstraint.activate(onScreenConstraintWB)
-
- // Notify the child view controller that the move is complete.
- wbVC!.didMove(toParent: self)
- // self.navigationController?.setNavigationBarHidden(false, animated: true)
- // controller.modalPresentationStyle = .overCurrentContext
- // self.navigationController?.present(controller, animated: true)
- }
-
- @objc func didSpeaker(sender: Any?) {
- isSpeaker = !isSpeaker
- speaker.isSelected = isSpeaker
- if isSpeaker {
- UIDevice.current.isProximityMonitoringEnabled = false
- } else {
- UIDevice.current.isProximityMonitoringEnabled = true
- }
- Nexilis.setSpeaker(isSpeaker)
- }
-
- @objc func didMute(sender: Any?) {
- isMuted = !isMuted
- mic.isSelected = isMuted
- API.mmc(int: 1, boolean: isMuted)
- }
-
- @objc func didInvite(sender: Any?) {
- let controller = QmeraCallContactViewController()
- controller.isDismiss = { user in
- let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
- if !onGoingCC.isEmpty {
- DispatchQueue.global().async {
- _ = Nexilis.write(message: CoreMessage_TMessageBank.getCCRoomInvite(l_pin: user.pin, ticket_id: onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2], channel: "1"))
- }
- DispatchQueue.main.async {
- self.isAddCall = user.pin
- }
- } else {
- self.users.append(user)
- // Start Calling
- // Nexilis.shared.callManager.startCall(handle: user.pin)
- API.initiateCCall(sParty: user.pin)
- }
- }
- controller.selectedUser.append(contentsOf: users)
- present(CustomNavigationController(rootViewController: controller), animated: true, completion: nil)
- }
-
- @objc func didPressEnd(sender: Any?) {
- let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
- if !onGoingCC.isEmpty {
- self.isEndByMe = true
- self.didEnd(sender: nil)
- return
- }
- let alert = LibAlertController(title: "End Audio Call".localized(), message: "Are you sure you want to end audio call?".localized(), preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "No".localized(), style: UIAlertAction.Style.default, handler: nil))
- alert.addAction(UIAlertAction(title: "Yes".localized(), style: UIAlertAction.Style.default, handler: {(_) in
- DispatchQueue.main.async {
- self.timer?.invalidate()
- self.timer = nil
- self.status.text = "Audio Call Ended".localized()
- self.end.isEnabled = false
- self.invite.isEnabled = false
- self.speaker.isEnabled = false
- self.mic.isEnabled = false
- }
- self.isEndByMe = true
- self.didEnd(sender: nil)
- }))
- self.present(alert, animated: true, completion: nil)
- }
-
- @objc func didEnd(sender: Any?) {
- poweredByView.isHidden = true
- let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
- if !onGoingCC.isEmpty {
- if sender != nil && sender is Bool {
- let controller = self.presentedViewController
- if controller != nil {
- controller!.dismiss(animated: true)
- }
- self.dismiss(animated: false, completion: nil)
- let requester = onGoingCC.components(separatedBy: ",")[0]
- let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
- let complaintId = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2]
- let startTimeCC: String = SecureUserDefaults.shared.value(forKey: "startTimeCC") ?? ""
- DispatchQueue.global().async {
- if sender as! Bool == true {
- let date = "\(Date().currentTimeMillis())"
- Database.shared.database?.inTransaction({ (fmdb, rollback) in
- do {
- _ = try Database.shared.insertRecord(fmdb: fmdb, table: "CALL_CENTER_HISTORY", cvalues: [
- "type" : "1",
- "title" : "Contact Center".localized(),
- "time" : startTimeCC,
- "f_pin" : officer,
- "data" : complaintId,
- "time_end" : date,
- "complaint_id" : complaintId,
- "members" : "",
- "requester": requester
- ], replace: true)
- } catch {
- rollback.pointee = true
- print("Access database error: \(error.localizedDescription)")
- }
- })
- }
- SecureUserDefaults.shared.removeValue(forKey: "onGoingCC")
- SecureUserDefaults.shared.removeValue(forKey: "membersCC")
- SecureUserDefaults.shared.removeValue(forKey: "startTimeCC")
- SecureUserDefaults.shared.removeValue(forKey: "waitingRequestCC")
- }
- return
- }
- 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)
- alert.addAction(UIAlertAction(title: "No".localized(), style: UIAlertAction.Style.default, handler: nil))
- alert.addAction(UIAlertAction(title: "Yes".localized(), style: UIAlertAction.Style.default, handler: {(_) in
- self.dismiss(animated: false, completion: nil)
- let requester = onGoingCC.components(separatedBy: ",")[0]
- let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
- let complaintId = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[2]
- let idMe = User.getMyPin()!
- let startTimeCC: String = SecureUserDefaults.shared.value(forKey: "startTimeCC") ?? ""
- DispatchQueue.global().async {
- let date = "\(Date().currentTimeMillis())"
- Database.shared.database?.inTransaction({ (fmdb, rollback) in
- do {
- _ = try Database.shared.insertRecord(fmdb: fmdb, table: "CALL_CENTER_HISTORY", cvalues: [
- "type" : "1",
- "title" : "Contact Center".localized(),
- "time" : startTimeCC,
- "f_pin" : officer,
- "data" : complaintId,
- "time_end" : date,
- "complaint_id" : complaintId,
- "members" : "",
- "requester": requester
- ], replace: true)
- } catch {
- rollback.pointee = true
- print("Access database error: \(error.localizedDescription)")
- }
- })
- if officer == idMe {
- _ = Nexilis.write(message: CoreMessage_TMessageBank.endCallCenter(complaint_id: complaintId, l_pin: requester))
- } else {
- if requester == idMe {
- _ = Nexilis.write(message: CoreMessage_TMessageBank.endCallCenter(complaint_id: complaintId, l_pin: officer))
- } else {
- _ = Nexilis.write(message: CoreMessage_TMessageBank.leaveCCRoomInvite(ticket_id: complaintId))
- }
- }
- SecureUserDefaults.shared.removeValue(forKey: "onGoingCC")
- SecureUserDefaults.shared.removeValue(forKey: "membersCC")
- SecureUserDefaults.shared.removeValue(forKey: "startTimeCC")
- SecureUserDefaults.shared.removeValue(forKey: "waitingRequestCC")
- }
- // if let user = self.user, let call = Nexilis.shared.callManager.call(with: user.pin) {
- // Nexilis.shared.callManager.end(call: call)
- // } else {
- API.terminateCall(sParty: nil)
- // }
- }))
- self.present(alert, animated: true, completion: nil)
- } else {
- let controller = self.presentedViewController
- if controller != nil {
- controller!.dismiss(animated: true)
- }
- if isEndByMe {
- // for i in 0..<Nexilis.shared.callManager.calls.count {
- // Nexilis.shared.callManager.end(call: Nexilis.shared.callManager.calls[i])
- // }
- API.terminateCall(sParty: nil)
- DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
- self.dismiss(animated: false, completion: nil)
- }
- } else {
- // if let user = self.user, let call = Nexilis.shared.callManager.call(with: user.pin) {
- // Nexilis.shared.callManager.end(call: call)
- // } else {
- API.terminateCall(sParty: nil)
- // }
- self.dismiss(animated: false, completion: nil)
- }
- }
- }
-
- @objc func didReject(sender: Any?) {
- if isOutgoing {
- Nexilis.ringbacktonePlayer?.stop()
- } else {
- Nexilis.ringtonePlayer?.stop()
- }
- didEnd(sender: sender)
- }
-
- @objc func didAccept(sender: Any?) {
- if isOutgoing {
- Nexilis.ringbacktonePlayer?.stop()
- } else {
- Nexilis.ringtonePlayer?.stop()
- }
- NSLayoutConstraint.deactivate(stack.constraints)
- stack.subviews.forEach { subview in
- subview.removeFromSuperview()
- }
- ongoingView()
- UIView.animate(withDuration: 0.3, animations: {
- self.view.layoutIfNeeded()
- })
- API.receiveCCall(sParty: user?.pin)
- }
-
- // MARK: - Communication
-
- @objc func onReceiveMessage(notification: NSNotification) {
- DispatchQueue.main.async {
- let data:[AnyHashable : Any] = notification.userInfo!
- if let dataMessage = data["message"] as? TMessage {
- if (dataMessage.getCode() == CoreMessage_TMessageCode.PUSH_MEMBER_ROOM_CONTACT_CENTER) {
- let data = dataMessage.getBody(key: CoreMessage_TMessageKey.DATA)
- if !data.isEmpty {
- if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions()) as? [AnyObject] {
- var members = ""
- let idMe = User.getMyPin()!
- for json in jsonArray {
- if "\(json)" != idMe {
- if members.isEmpty {
- members = "\(json)"
- } else {
- members += ",\(json)"
- }
- }
- }
- SecureUserDefaults.shared.set(members, forKey: "inEditorPersonal")
- SecureUserDefaults.shared.set("\(members)", forKey: "membersCC")
- }
- }
- self.users.append(User.getData(pin: dataMessage.getPIN())!)
- }
- }
- }
- }
-
- @objc func onStatusCall(_ notification: NSNotification) {
- if let data = notification.userInfo,
- let state = data["state"] as? Int,
- let message = data["message"] as? String
- {
- let arrayMessage = message.split(separator: ",")
- if state == Nexilis.AUDIO_VIDEO_CALL_MUTED {
- DispatchQueue.main.async { [self] in
- if let pin = arrayMessage.first, let index = users.firstIndex(of: User(pin: String(pin))) {
- if arrayMessage[1] == "1" {
- users[index].isMuted = true
- if let profile = profiles.subviews[index] as? ProfileView {
- profile.imageMuted.isHidden = false
- }
- } else {
- users[index].isMuted = false
- if let profile = profiles.subviews[index] as? ProfileView {
- profile.imageMuted.isHidden = true
- }
- }
- }
- }
- } else if state == Nexilis.AUDIO_CALL_RINGING || (!ticketId.isEmpty && state == Nexilis.VIDEO_CALL_RINGING) {
- if users.count == 1 {
- DispatchQueue.main.async {
- self.status.text = "Ringing..."
- }
- }
- } else if state == Nexilis.AUDIO_CALL_OFFHOOK || (!ticketId.isEmpty && state == Nexilis.VIDEO_CALL_OFFHOOK) {
- if isOutgoing {
- Nexilis.ringbacktonePlayer?.stop()
- }
- if users.count == 1 && firstCall {
- DispatchQueue.main.async {
- if !self.ticketId.isEmpty {
- NSLayoutConstraint.deactivate(self.stack.constraints)
- self.stack.subviews.forEach { subview in
- subview.removeFromSuperview()
- }
- UIView.animate(withDuration: 0.3, animations: {
- self.view.layoutIfNeeded()
- })
- }
- self.ongoingView()
- let connectDate = Date()
- self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
- let format = Utils.callDurationFormatter.string(from: Date().timeIntervalSince(connectDate))
- self.status.text = format
- }
- self.timer?.fire()
- self.firstCall = false
- }
- }
- if (!isOutgoing || !firstCall), users.count >= 1, let user = User.getData(pin: String(arrayMessage[1])), !users.contains(user) {
- self.users.append(user)
- }
- } else if state == Nexilis.AUDIO_CALL_END || (!ticketId.isEmpty && state == Nexilis.VIDEO_CALL_END) {
- if isOutgoing {
- Nexilis.ringbacktonePlayer?.stop()
- } else {
- Nexilis.ringtonePlayer?.stop()
- }
- let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
- if let pin = arrayMessage.first, let index = users.firstIndex(of: User(pin: String(pin))) {
- users.remove(at: index)
- if !onGoingCC.isEmpty && users.count != 0 {
- let requester = onGoingCC.components(separatedBy: ",")[0]
- let officer = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
- if pin == requester || pin == officer {
- DispatchQueue.main.async {
- if !self.end.isEnabled {
- return
- }
- if self.buttonWB.isDescendant(of: self.view){
- self.buttonWB.removeFromSuperview()
- }
- if self.buttonChat.isDescendant(of: self.view){
- self.buttonChat.removeFromSuperview()
- }
- if self.viewIfLoaded?.window != nil {
- let imageView = UIImageView(image: UIImage(systemName: "info.circle"))
- imageView.tintColor = .white
- 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)
- banner.show()
- }
- self.timer?.invalidate()
- self.timer = nil
- self.status.text = "Call Center Session has ended..."
- self.end.isEnabled = false
- }
- DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
- self.didEnd(sender: true)
- }
- return
- }
- } else if !onGoingCC.isEmpty && users.count == 0 {
- DispatchQueue.main.async {
- if !self.end.isEnabled {
- return
- }
- if self.buttonWB.isDescendant(of: self.view){
- self.buttonWB.removeFromSuperview()
- }
- if self.buttonChat.isDescendant(of: self.view){
- self.buttonChat.removeFromSuperview()
- }
- if self.viewIfLoaded?.window != nil {
- let imageView = UIImageView(image: UIImage(systemName: "info.circle"))
- imageView.tintColor = .white
- 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)
- banner.show()
- }
- self.timer?.invalidate()
- self.timer = nil
- self.status.text = "Call Center Session has ended..."
- self.end.isEnabled = false
- }
- DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
- self.didEnd(sender: true)
- }
- return
- } else if users.count == 0 {
- DispatchQueue.main.async {
- self.timer?.invalidate()
- self.timer = nil
- self.status.text = "Audio Call Ended".localized()
- self.end.isEnabled = false
- self.invite.isEnabled = false
- self.speaker.isEnabled = false
- self.mic.isEnabled = false
- let controller = self.presentedViewController
- if controller != nil {
- controller!.dismiss(animated: true)
- }
- }
- DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
- self.didEnd(sender: true)
- }
- return
- }
- DispatchQueue.main.async{ [self] in
- if users.count == 1 && !buttonWB.isEnabled {
- buttonWB.isEnabled = true
- }
- }
- }
- // if users.count == 0 {
- // DispatchQueue.main.async {
- // self.dismiss(animated: false, completion: nil)
- // }
- // }
- } else if state == Nexilis.OFFLINE { // Offline
- if isOutgoing {
- Nexilis.ringbacktonePlayer?.stop()
- } else {
- Nexilis.ringtonePlayer?.stop()
- }
- let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
- if let pin = arrayMessage.first, let index = users.firstIndex(of: User(pin: String(pin))) {
- users.remove(at: index)
- if !onGoingCC.isEmpty && users.count != 0 {
- DispatchQueue.main.async {
- var members = ""
- for user in self.users {
- if members.isEmpty {
- members = "\(user.pin)"
- } else {
- members = ",\(user.pin)"
- }
- }
- SecureUserDefaults.shared.set("\(members)", forKey: "membersCC")
- }
- }
- DispatchQueue.main.async { [self] in
- if users.count == 1 && !buttonWB.isEnabled {
- buttonWB.isEnabled = true
- }
- }
- }
- if users.count == 0 {
- DispatchQueue.main.async {
- self.status.text = "Offline..."
- self.end.isEnabled = false
- DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
- self.didEnd(sender: false)
- }
- }
- }
- } else if state == Nexilis.BUSY { // Busy
- if isOutgoing {
- Nexilis.ringbacktonePlayer?.stop()
- } else {
- Nexilis.ringtonePlayer?.stop()
- }
- let onGoingCC: String = SecureUserDefaults.shared.value(forKey: "onGoingCC") ?? ""
- if let pin = arrayMessage.first, let index = users.firstIndex(of: User(pin: String(pin))) {
- users.remove(at: index)
- if !onGoingCC.isEmpty && users.count != 0 {
- DispatchQueue.main.async {
- var members = ""
- for user in self.users {
- if members.isEmpty {
- members = "\(user.pin)"
- } else {
- members = ",\(user.pin)"
- }
- }
- SecureUserDefaults.shared.set("\(members)", forKey: "membersCC")
- }
- }
- DispatchQueue.main.async { [self] in
- if users.count == 1 && !buttonWB.isEnabled {
- buttonWB.isEnabled = true
- }
- }
- }
- if users.count == 0 {
- DispatchQueue.main.async {
- self.status.text = "Busy..."
- self.end.isEnabled = false
- DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
- self.didEnd(sender: false)
- }
- }
- }
- }
- }
- }
-
- }
- extension QmeraAudioViewController : WhiteboardReceiver {
-
- func incomingWB(roomId: String) {
- //print("incoming wb")
- self.wbTimer.invalidate()
- if(wbRoomId.isEmpty){
- //print("wbroom empty")
- DispatchQueue.main.async {
- self.wbTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.runTimer), userInfo: nil, repeats: true)
- }
- let me = User.getMyPin()!
- var destString = ""
- for d in users{
- if d.pin == roomId.components(separatedBy: "wbvc")[0] {
- continue
- }
- if destString.isEmpty{
- destString = d.pin
- } else {
- destString = destString + ",\(d.pin)"
- }
- }
- if destString.isEmpty {
- SecureUserDefaults.shared.set("\(roomId.components(separatedBy: "wbvc")[0]),\(me)", forKey: "wb_vc")
- } else {
- SecureUserDefaults.shared.set("\(roomId.components(separatedBy: "wbvc")[0]),\(me),\(destString)", forKey: "wb_vc")
- }
- wbRoomId = roomId
- }
- }
-
- func cancel(roomId: String) {
- DispatchQueue.main.async {
- self.wbTimer.invalidate()
- self.wbBlink = false
- self.buttonWB.backgroundColor = .lightGray
- self.buttonWB.setNeedsDisplay()
- }
- wbRoomId = ""
- }
-
- @objc func runTimer(){
- DispatchQueue.main.async {
- self.wbBlink = !self.wbBlink
- if(self.wbBlink){
- //print("set wb blink on")
- self.buttonWB.backgroundColor = .green
- }
- else {
- //print("set wb blink off")
- self.buttonWB.backgroundColor = .lightGray
- }
- self.buttonWB.setNeedsDisplay()
- }
- }
-
- }
|