1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939 |
- //
- // APIS.swift
- // NexilisLite
- //
- // Created by Akhmad Al Qindi Irsyam on 05/05/23.
- //
- import Foundation
- import UIKit
- import FMDB
- import NotificationBannerSwift
- import Toast_Swift
- import nuSDKService
- import AVFoundation
- import AVKit
- import Intents
- public class APIS: NSObject {
- private static var isAlertPresented = false
- public static func connect(appName: String, apiKey: String, delegate: ConnectDelegate, showButton: Bool = true, fromMAB: Bool = false) {
- APIS.appNm = appName.trimmingCharacters(in: .whitespacesAndNewlines)
- Nexilis.connect(apiKey: apiKey, delegate: delegate, showButton: showButton, fromMAB: fromMAB)
- }
-
- public static func getTotalCounter() -> Int32 {
- var counter: Int32?
- Database.shared.database?.inTransaction({ (fmdb, rollback) in
- do {
- let query = """
- select SUM(counter) as total_counter
- from (
- select ms.counter from MESSAGE_SUMMARY ms, MESSAGE m, BUDDY b
- where ms.l_pin = b.f_pin and ms.message_id = m.message_id and m.is_call_center = 0
- union all
- select ms.counter from MESSAGE_SUMMARY ms, MESSAGE m
- where ms.l_pin = '-999' and ms.message_id = m.message_id
- union all
- select ms.counter from MESSAGE_SUMMARY ms, MESSAGE m
- where ms.l_pin = '-997' and ms.message_id = m.message_id
- union all
- select ms.counter from MESSAGE_SUMMARY ms, MESSAGE m, GROUPZ b
- where ms.l_pin = b.group_id and ms.message_id = m.message_id and m.is_call_center = 0
- union all
- select ms.counter from MESSAGE_SUMMARY ms, MESSAGE m, DISCUSSION_FORUM b, GROUPZ c
- where b.group_id = c.group_id and ms.l_pin = b.chat_id and ms.message_id = m.message_id and m.is_call_center = 0
- ) as subquery
- """
- if let cursor = Database.shared.getRecords(fmdb: fmdb, query: query), cursor.next() {
- counter = cursor.int(forColumnIndex: 0)
- cursor.close()
- }
- } catch {
- rollback.pointee = true
- print("Access database error: \(error.localizedDescription)")
- }
- })
- return counter ?? 0
- }
-
- private static func showChangeProfile() {
- guard !isAlertPresented else { return }
- isAlertPresented = true
- let alert = LibAlertController(title: "Set Profile".localized(), message: "You must set your profile to use this feature".localized(), preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "OK".localized(), style: UIAlertAction.Style.default, handler: {(_) in
- isAlertPresented = false
- let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "signupsignin") as! SignUpSignIn
- controller.forceLogin = true
- let navigationController = CustomNavigationController(rootViewController: controller)
- navigationController.defaultStyle()
- 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)
- }
- }))
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(alert, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(alert, animated: true, completion: nil)
- }
- }
-
- public static func openContactCenter(media: Int? = nil, category: Int? = nil) {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- if User.isCallCenter(userType: (User.getData(pin: User.getMyPin())?.userType)!) {
- let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "myHistoryCC") as! HistoryCCViewController
- controller.isOfficer = true
- controller.fromAPI = true
- let navigationController = CustomNavigationController(rootViewController: controller)
- navigationController.setNavigationBarHidden(false, animated: false)
- navigationController.navigationBar.isTranslucent = false
- navigationController.defaultStyle()
- 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)
- }
- } else {
- if media != nil || (media == nil && category != nil) {
- if media == nil || media! < 0 || media! > 2 {
- UIApplication.shared.visibleViewController?.view.makeToast("108:Invalid Contact Center media parameter (0:Chat, 1:Audio Call, 2:Video Call)".localized(), duration: 3)
- return
- }
- }
- if category != nil {
- if category != 0 {
- let service = CategoryCC.getDataFromServiceId(service_id: "\(category!)")
- if service == nil {
- UIApplication.shared.visibleViewController?.view.makeToast("109:Invalid Contact Center category parameter".localized(), duration: 3)
- return
- }
- let serviceChilds = CategoryCC.getDatafromParent(parent: service!.service_id)
- if serviceChilds.count > 0 {
- UIApplication.shared.visibleViewController?.view.makeToast("109:Invalid Contact Center category parameter".localized(), duration: 3)
- return
- }
- }
- }
- let isWaitingRequestCC: Bool = SecureUserDefaults.shared.value(forKey: "waitingRequestCC") ?? false
- if isWaitingRequestCC {
- let imageView = UIImageView(image: UIImage(systemName: "info.circle"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "You have requested Call Center, please wait for response.".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()
- return
- }
- let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
- controller.isContactCenter = true
- if media != nil {
- controller.isDirectCC = true
- controller.channelContactCenter = "\(media!)"
- controller.serviceIdCC = category == nil ? "" : "\(category!)"
- }
- let navigationController = CustomNavigationController(rootViewController: controller)
- navigationController.defaultStyle()
- 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)
- }
- }
- }
-
- public static func openUrl(url: String) {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let controller = BNIBookingWebView()
- controller.customUrl = url
- let navigationController = CustomNavigationController(rootViewController: controller)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func openNotificationCenter() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let controller = HistoryBroadcastViewController()
- let navigationController = CustomNavigationController(rootViewController: controller)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func openChat() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let navigationController = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactChatNav") as! UINavigationController
- Utils.addBackground(view: navigationController.view)
- let vc = navigationController.topViewController as! ContactChatViewController
- vc.noUCList = true
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func openSmartChatbot() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let smartChatVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "chatGptVC") as! ChatGPTBotView
- smartChatVC.hidesBottomBarWhenPushed = true
- let navigationController = CustomNavigationController(rootViewController: smartChatVC)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func startChat(name: String) {
- if name.isEmpty {
- UIApplication.shared.visibleViewController?.view.makeToast("92:Username is empty".localized(), duration: 2)
- return
- }
- let user = User.getDataFromNameCanNil(name: name)
- if user == nil {
- UIApplication.shared.visibleViewController?.view.makeToast("91:Invalid name or you must add Username to your contact first".localized(), duration: 3)
- return
- }
- let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
- editorPersonalVC.hidesBottomBarWhenPushed = true
- editorPersonalVC.unique_l_pin = user!.pin
- editorPersonalVC.fromNotification = true
- let navigationController = CustomNavigationController(rootViewController: editorPersonalVC)
- navigationController.modalPresentationStyle = .fullScreen
- navigationController.navigationBar.tintColor = .white
- navigationController.navigationBar.barTintColor = UIApplication.shared.visibleViewController?.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)
- }
- }
-
- public static func openCall() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let callContact = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactSID")
- let navigationController = CustomNavigationController(rootViewController: callContact)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func openStreaming() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let navigationController = CustomNavigationController(rootViewController: QmeraCreateStreamingViewController())
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func openConference() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let navigationController = CustomNavigationController(rootViewController: CreateSeminarViewController())
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func openAudioCall() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let callContact = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactSID") as! ContactCallViewController
- callContact.onlyAudioOrVideo = 1
- let navigationController = CustomNavigationController(rootViewController: callContact)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func startAudioCall(name: String) {
- if name.isEmpty {
- UIApplication.shared.visibleViewController?.view.makeToast("92:Username is empty".localized(), duration: 3)
- return
- }
- let user = User.getDataFromNameCanNil(name: name)
- if user == nil {
- UIApplication.shared.visibleViewController?.view.makeToast("91:Invalid name or you must add Username to your contact first".localized(), duration: 3)
- return
- }
- if !CheckConnection.isConnectedToNetwork() {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Check your connection".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()
- return
- }
- let controller = QmeraAudioViewController()
- controller.user = user
- controller.isOutgoing = true
- controller.modalPresentationStyle = .overFullScreen
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(controller, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(controller, animated: true, completion: nil)
- }
- }
-
- public static func openVideoCall() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let callContact = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactSID") as! ContactCallViewController
- callContact.onlyAudioOrVideo = 2
- let navigationController = CustomNavigationController(rootViewController: callContact)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func startVideoCall(name: String) {
- if name.isEmpty {
- UIApplication.shared.visibleViewController?.view.makeToast("92:Username is empty".localized(), duration: 3)
- return
- }
- let user = User.getDataFromNameCanNil(name: name)
- if user == nil {
- UIApplication.shared.visibleViewController?.view.makeToast("91:Invalid name or you must add Username to your contact first".localized(), duration: 3)
- return
- }
- if !CheckConnection.isConnectedToNetwork() {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Check your connection".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()
- return
- }
- let videoVC = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "videoVCQmera") as! QmeraVideoViewController
- var data: [String: String?] = [:]
- data["f_pin"] = user!.pin
- data["name"] = user!.fullName
- data["picture"] = user!.thumb
- data["isOfficial"] = user!.official
- data["deviceId"] = user!.device_id
- data["isOffline"] = user!.offline_mode
- data["user_type"] = user!.userType
- videoVC.dataPerson.append(data)
- videoVC.isPresent = true
- videoVC.modalPresentationStyle = .overFullScreen
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(videoVC, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(videoVC, animated: true, completion: nil)
- }
- }
-
- public static func openBroadcastForm() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "broadcastNav")
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(controller, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(controller, animated: true, completion: nil)
- }
- }
-
- public static func openConversation() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let navigationController = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactChatNav") as! UINavigationController
- Utils.addBackground(view: navigationController.view)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func openFavoriteMessage() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let editorStaredVC = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "staredVC") as! EditorStarMessages
- editorStaredVC.fromNotification = true
- let navigationController = CustomNavigationController(rootViewController: editorStaredVC)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func openSecureFolder() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "secureFolderView") as! SecureFolderViewController
- let navigationController = CustomNavigationController(rootViewController: controller)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func openSecureBrowser() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let controller = BNIBookingWebView()
- controller.isSecureBrowser = true
- let navigationController = CustomNavigationController(rootViewController: controller)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func openCreateGroup() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "createGroupNav") as! UINavigationController
- Utils.addBackground(view: controller.view)
- let vc = controller.topViewController as! GroupCreateViewController
- vc.isDismiss = { id in
- let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "groupDetailView") as! GroupDetailViewController
- controller.data = id
- controller.fromNotification = true
- let navigationController = CustomNavigationController(rootViewController: controller)
- navigationController.defaultStyle()
- DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
- 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)
- }
- })
- }
- controller.defaultStyle()
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(controller, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(controller, animated: true, completion: nil)
- }
- }
-
- public static func openAddFriend() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "addFriendNav") as! UINavigationController
- Utils.addBackground(view: controller.view)
- controller.defaultStyle()
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(controller, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(controller, animated: true, completion: nil)
- }
- }
-
- public static func openSignUpOrSignIn() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "signupsignin") as! SignUpSignIn
- controller.forceLogin = true
- let navigationController = CustomNavigationController(rootViewController: controller)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func openSetting() {
- let navigationController = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "settingNav") as! UINavigationController
- let vc = navigationController.rootViewController as! SettingTableViewController
- vc.fromAPI = true
- Utils.addBackground(view: navigationController.view)
- navigationController.defaultStyle()
- if UIApplication.shared.visibleViewController is UINavigationController && (UIApplication.shared.visibleViewController as! UINavigationController).rootViewController is SettingTableViewController {
- return
- }
- 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)
- }
- }
-
- public static func openProfile() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "profileView") as! ProfileViewController
- controller.data = User.getMyPin()!
- controller.flag = .me
- controller.fromAPI = true
- controller.dismissImage = { image, imageName in
- var dataImage: [AnyHashable : Any] = [:]
- dataImage["name"] = imageName
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "imageFBUpdate"), object: nil, userInfo: dataImage)
- }
- let navigationController = CustomNavigationController(rootViewController: controller)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func openChatWallpaper(){
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "chatWallpaper") as! ChatWallpaperViewController
- let navigationController = CustomNavigationController(rootViewController: controller)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func openWhiteboard() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let callContact = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactSID") as! ContactCallViewController
- callContact.startWhiteBoard = true
- let navigationController = CustomNavigationController(rootViewController: callContact)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func startWhiteboard(name: String) {
- if name.isEmpty {
- UIApplication.shared.visibleViewController?.view.makeToast("92:Username is empty".localized(), duration: 3)
- return
- }
- let user = User.getDataFromNameCanNil(name: name)
- if user == nil {
- UIApplication.shared.visibleViewController?.view.makeToast("91:Invalid name or you must add Username to your contact first".localized(), duration: 3)
- return
- }
- if !CheckConnection.isConnectedToNetwork() {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Check your connection".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()
- return
- }
- let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "wbVC") as! WhiteboardViewController
- controller.modalPresentationStyle = .overFullScreen
- controller.fromContact = 0
- controller.user = user
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(controller, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(controller, animated: true, completion: nil)
- }
- }
-
- // public static func openScreenSharing() {
- // let isChangeProfile = Utils.getSetProfile()
- // if !isChangeProfile {
- // APIS.showChangeProfile()
- // return
- // }
- // let callContact = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactSID") as! ContactCallViewController
- // callContact.startSS = true
- // let navigationController = CustomNavigationController(rootViewController: callContact)
- // navigationController.defaultStyle()
- // 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)
- // }
- // }
-
- public static func startScreenSharing(name: String) {
- if name.isEmpty {
- UIApplication.shared.visibleViewController?.view.makeToast("92:Username is empty".localized(), duration: 3)
- return
- }
- let user = User.getDataFromNameCanNil(name: name)
- if user == nil {
- UIApplication.shared.visibleViewController?.view.makeToast("91:Invalid name or you must add Username to your contact first".localized(), duration: 3)
- return
- }
- if !CheckConnection.isConnectedToNetwork() {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Check your connection".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()
- return
- }
- let controller = ScreenSharingViewController()
- controller.modalPresentationStyle = .overFullScreen
- controller.fromContact = 0
- controller.user = user
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(controller, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(controller, animated: true, completion: nil)
- }
- }
-
- public static func openWhiteboardAndScreenSharing() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let callContact = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactSID") as! ContactCallViewController
- callContact.startSS = true
- callContact.startWhiteBoard = true
- let navigationController = CustomNavigationController(rootViewController: callContact)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func signInAdmin(password: String) {
- if password.isEmpty {
- UIApplication.shared.visibleViewController?.view.makeToast("113:Password is empty".localized(), duration: 3)
- return
- }
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let isAdmin = User.isAdmin()
- if isAdmin {
- UIApplication.shared.visibleViewController?.view.makeToast("112:You already login or registered as Admin".localized(), duration: 3)
- return
- }
- if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Check your connection".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()
- return
- }
- Nexilis.showLoader()
- self.signInAdmin(password: password, completion: { result in
- if result {
- DispatchQueue.main.async {
- Nexilis.hideLoader {
- let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Successfully Sign-In Admin".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .success, colors: nil, iconPosition: .center)
- banner.show()
- }
- }
- } else {
- DispatchQueue.main.async {
- Nexilis.hideLoader {}
- }
- }
- })
- }
-
- private static func signInAdmin(password: String, completion: @escaping (Bool) -> ()) {
- DispatchQueue.global().async {
- let idMe = User.getMyPin() as String?
- let p_password = password
- let md5Hex = p_password
- var result: Bool = false
- if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getSignInApiAdmin(p_name: idMe!, p_password: md5Hex)) {
- if response.isOk() {
- result = true
- }
- DispatchQueue.main.async {
- if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "11" {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Username or password does not match".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: .top)
- banner.show()
- } else if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "20" {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Invalid password".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: .top)
- banner.show()
- }
- }
- } else {
- DispatchQueue.main.async {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Unable to access servers".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: .top)
- banner.show()
- }
- }
- completion(result)
- }
- }
-
- public static func openSetAsOfficerForm() {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let isAdmin = User.isAdmin()
- if !isAdmin {
- UIApplication.shared.visibleViewController?.view.makeToast("111:You must Sign In as Admin to use this feature".localized(), duration: 3)
- return
- }
- let controller = SetInternalCSAccount()
- controller.isSetCS = true
- controller.fromNotification = true
- let navigationController = CustomNavigationController(rootViewController: controller)
- navigationController.defaultStyle()
- 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)
- }
- }
-
- public static func sendSMS(phoneNumber: String, message: String = ""){
- let formattedNumber = phoneNumber.replacingOccurrences(of: "-", with: "")
- let urlStringEncoded = message.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
- let paramMessage = message.isEmpty ? "" : "&body=\(urlStringEncoded!)"
- let url = URL(string: "sms:\(formattedNumber)\(paramMessage)")
- if UIApplication.shared.canOpenURL(url!) {
- UIApplication.shared.open(url!)
- }
- }
-
- public static func sendWhatsapp(phoneNumber: String, message: String = "") {
- let formattedNumber = phoneNumber.replacingOccurrences(of: "+", with: "").replacingOccurrences(of: "-", with: "")
- let urlStringEncoded = message.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
- let paramMessage = message.isEmpty ? "" : "?text=\(urlStringEncoded!)"
- let url = URL(string: "https://wa.me/\(formattedNumber)\(paramMessage)")
- if UIApplication.shared.canOpenURL(url!) {
- UIApplication.shared.open(url!, options: [:]) { (success) in
- // if success {
- // //print("WhatsApp accessed successfully")
- // } else {
- // //print("Error accessing WhatsApp")
- // }
- }
- }
- }
-
- public static func changeUsername(uname: String) {
- let isChangeProfile = Utils.getSetProfile()
- if !isChangeProfile {
- APIS.showChangeProfile()
- return
- }
- let finalUname = uname.replacingOccurrences(of: "[\\n\\r\\t~%()\"]", with: "", options: .regularExpression)
- if finalUname == User.getData(pin: User.getMyPin())?.fullName {
- UIApplication.shared.visibleViewController?.view.makeToast("102:Duplicate username".localized(), duration: 3)
- return
- }
- if finalUname.count == 0 {
- UIApplication.shared.visibleViewController?.view.makeToast("103:Username is empty".localized(), duration: 3)
- return
- }
- if finalUname.count < 3 {
- UIApplication.shared.visibleViewController?.view.makeToast("104:Username length is too short".localized(), duration: 3)
- return
- }
- let a = finalUname.split(separator: " ", maxSplits: 1)
- let first = String(a[0])
- let last = a.count == 2 ? String(a[1]) : ""
- DispatchQueue.global().async {
- if let resp = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getChangePersonInfoName(firstname: first, lastname: last)) {
- if resp.isOk() {
- Database.shared.database?.inTransaction({ fmdb, rollback in
- do {
- _ = Database.shared.updateRecord(fmdb: fmdb, table: "BUDDY", cvalues: ["first_name": first , "last_name": last], _where: "f_pin = '\(User.getMyPin())'")
- } catch {
- rollback.pointee = true
- print("Access database error: \(error.localizedDescription)")
- }
- })
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateFifthTab"), object: nil, userInfo: nil)
- DispatchQueue.main.async {
- Nexilis.hideLoader {
- let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Successfully changed name".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .success, colors: nil, iconPosition: .center)
- banner.show()
- }
- }
- } else if resp.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "1a" {
- DispatchQueue.main.async {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Username has been registered, please use another name".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()
- }
- }
- }
- }
- }
-
- public static func openMail() {
- Nexilis.openmailAction()
- }
-
- public static func sendPushToken(_ token: String, isResend: Bool = false, isCall: Bool = false) {
- DispatchQueue.global().async{
- if !isCall {
- if Utils.getTokenAPN().isEmpty || token != Utils.getTokenAPN() {
- Utils.setTokenAPN(value: token)
- }
- DispatchQueue.global().async {
- while API.nGetCLXConnState() == 0 {
- Thread.sleep(forTimeInterval: 1)
- }
- _ = Nexilis.write(message: CoreMessage_TMessageBank.getToken(token: token, tokenCall: Utils.getTokenCall()))
- }
- }
- else {
- if Utils.getTokenCall().isEmpty || token != Utils.getTokenCall() {
- Utils.setTokenCall(value: token)
- }
- // DispatchQueue.global().async {
- // while API.nGetCLXConnState() == 0 {
- // Thread.sleep(forTimeInterval: 1)
- // }
- // print("SEND TOKEN CALL")
- // _ = Nexilis.write(message: CoreMessage_TMessageBank.getToken(token: token, isCall: true))
- // }
- }
- }
- }
-
- public static var uuidCall: UUID?
- public static var fpinCall: String?
- public static func showNotificationNexilis(_ userInfo: [AnyHashable : Any]) {
- if checkAppStateisBackground() {
- // Nexilis.sendStateToServer(s: "MASUK SHOW NOTIFICATION NEXILIS")
- // print("MASUK SHOW NOTIFICATION NEXILIS: \(userInfo)")
- DispatchQueue.main.async {
- if let payload = userInfo["payload"] as? [String: Any] {
- if let messagePayload = payload["message"] as? [String: Any] {
- if let data = messagePayload["data"] as? [String: Any] {
- let code = data["nx_code"] as? String ?? ""
- if code == "CL01" {
- if let message = data["bodies"] as? [String: String] {
- let idAck = data["message_id"] as? String ?? ""
- let messageToSave = TMessage()
- messageToSave.mBodies = message
- do {
- var messageExist = false
- Database.shared.database?.inTransaction({ (fmdb, rollback) in
- if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select message_id from MESSAGE where message_id = '\(message[CoreMessage_TMessageKey.MESSAGE_ID] ?? "")'"), cursor.next() {
- messageExist = true
- cursor.close()
- }
- })
- if messageExist {
- ackAPN(id: idAck)
- return
- }
- } catch {
- print("error saving message: \(error)")
- }
- APIS.addNotificationNexilis(messageToSave)
- ackAPN(id: idAck)
- Nexilis.saveMessage(message: messageToSave, withStatus: false, fromAPNS: true)
- }
- } else if code == "CL03" {
- let callFromName = data["call-from-name"] as? String ?? ""
- let callFrom = data["call-from"] as? String ?? ""
- let callType = data["call-type"] as? String ?? ""
- // uuidCall = UUID()
- fpinCall = callFrom
- Nexilis.callAPNActivated = true
- let center = UNUserNotificationCenter.current()
- let content = UNMutableNotificationContent()
- content.title = callFromName
- if callType == "1" {
- content.body = "Incoming Audio Call".localized()
- } else {
- content.body = "Incoming Video Call".localized()
- }
- content.userInfo = ["id" : callFrom, "type" : code, "callType": callType]
- content.sound = nil
- let request = UNNotificationRequest(identifier: callFrom, content: content, trigger: nil)
- center.add(request) { error in
- if let error = error {
- print("Error scheduling notification: \(error.localizedDescription)")
- }
- }
- let session = AVAudioSession.sharedInstance()
- do {
- try session.setCategory(.playback, options: [.duckOthers])
- try session.setActive(true)
- } catch {
- print("Audio session error: \(error)")
- }
- Nexilis.playRingtoneCall()
- } else if code == "CL02" {
- print("data \(data)")
- let callFromName = data["call-cancel-name"] as? String ?? ""
- let callFrom = data["call-cancel"] as? String ?? ""
- let callType = data["call-type"] as? String ?? ""
- // if let uuidCall = uuidCall {
- Nexilis.stopRingtoneCall()
- Nexilis.callAPNActivated = false
- let center = UNUserNotificationCenter.current()
- center.removeDeliveredNotifications(withIdentifiers: [callFrom])
- var textCall = ""
- if callType == "1" {
- textCall = "audio"
- } else {
- textCall = "video"
- }
- let content = UNMutableNotificationContent()
- content.title = callFromName
- content.body = "☎️ Missed \(textCall) call".localized()
- content.userInfo = ["id" : callFrom, "type" : code, "callType": callType]
- content.sound = nil
- let request = UNNotificationRequest(identifier: callFrom, content: content, trigger: nil)
- center.add(request) { error in
- if let error = error {
- print("Error scheduling notification: \(error.localizedDescription)")
- }
- }
- Nexilis.saveMessageCall(idCall: (User.getMyPin() ?? "") + CoreMessage_TMessageUtil.getTID(), textMessage: "Missed \(textCall) call".localized() + " at 0", fPin: callFrom, lPin: (User.getMyPin() ?? ""), timeCall: String(Date().currentTimeMillis()), attachment_type: MessageScope.MISSED_CALL)
- }
- }
- }
- } else if let message_id = userInfo["message_id"] as? String {
- getMessageById(id: message_id)
- }
- }
- }
- }
-
- public static func showNotificationCallKitNexilis(payload: [AnyHashable : Any], completion: @escaping () -> ()) {
- if let messagePayload = payload["payload"] as? [String: Any] {
- if let message = messagePayload["message"] as? [String: Any] {
- if let data = message["data"] as? [String: Any] {
- let nxCode = data["nx_code"] as? String ?? ""
- let callFromName = data["call-from-name"] as? String ?? ""
- let callCancelName = data["call-cancel-name"] as? String ?? ""
- let callFrom = data["call-from"] as? String ?? ""
- let callCancel = data["call-cancel"] as? String ?? ""
- let callType = data["call-type"] as? String ?? ""
- if nxCode == "CL03" {
- Nexilis.callAPNActivated = true
- APIS.uuidCall = UUID()
- CallManager.shared.reportIncomingCall(uuid: APIS.uuidCall ?? UUID(), callerName: callFromName, callerId: callFrom, isVideo: callType != "1")
- } else {
- if APIS.uuidCall != nil {
- CallManager.shared.endCall(uuid: APIS.uuidCall!) {
- Nexilis.callAPNActivated = false
- APIS.uuidCall = nil
- let center = UNUserNotificationCenter.current()
- var textCall = ""
- if callType == "1" {
- textCall = "audio"
- } else {
- textCall = "video"
- }
- let content = UNMutableNotificationContent()
- content.title = callCancelName
- content.body = "☎️ Missed \(textCall) call".localized()
- content.userInfo = ["id" : callFrom, "type" : nxCode, "callType": callType]
- content.sound = nil
- let request = UNNotificationRequest(identifier: callCancel, content: content, trigger: nil)
- center.add(request) { error in
- if let error = error {
- print("Error scheduling notification: \(error.localizedDescription)")
- }
- }
- Nexilis.saveMessageCall(idCall: (User.getMyPin() ?? "") + CoreMessage_TMessageUtil.getTID(), textMessage: "Missed \(textCall) call".localized() + " at 0", fPin: callCancel, lPin: (User.getMyPin() ?? ""), timeCall: String(Date().currentTimeMillis()), attachment_type: MessageScope.MISSED_CALL)
- }
- } else if UIApplication.shared.visibleViewController is QmeraAudioViewController || UIApplication.shared.visibleViewController is QmeraVideoViewController {
- var dataMessage: [AnyHashable : Any] = [:]
- dataMessage["call_cancel"] = true
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: Nexilis.callFCM), object: nil, userInfo: dataMessage)
- }
- }
- }
- }
- }
- }
-
- private static func ackAPN(id: String) {
- DispatchQueue.global().async {
- // Nexilis.sendStateToServer(s: "send ack from apn")
- DispatchQueue.global().async {
- let parameter: [String : Any] = [
- "pin": User.getMyPin() ?? "",
- "message_id": id
- ]
- Utils.postDataWithCookiesAndUserAgent(from: URL(string: Utils.getDomainOpr() + "ack_message")!, parameter: parameter, isFormData: true) { data, response, error in
- }
- }
- }
- }
-
- private static func getMessageById(id: String) {
- DispatchQueue.global().async {
- // Nexilis.sendStateToServer(s: "send ack from apn")
- DispatchQueue.global().async {
- let parameter: [String : Any] = [
- "pin": User.getMyPin() ?? "",
- "message_id": id
- ]
- Utils.postDataWithCookiesAndUserAgent(from: URL(string: Utils.getDomainOpr() + "pull_notification")!, parameter: parameter, isFormData: true) { data, response, error in
- if let data = data {
- do {
- if let dataString = String(data: data, encoding: .utf8) {
- if let jsonObj = try JSONSerialization.jsonObject(with: dataString.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions()) as? [String: Any] {
- let dataObj = jsonObj["data"] as? String ?? ""
- let message = TMessage(data: dataObj)
- Nexilis.saveMessage(message: message, withStatus: false, fromAPNS: true)
- ackAPN(id: id)
- }
- }
- } catch {
-
- }
- }
- DispatchQueue.main.async {
- UIApplication.shared.applicationIconBadgeNumber = Int(APIS.getTotalCounter())
- }
- }
- DispatchQueue.main.async {
- UIApplication.shared.applicationIconBadgeNumber = Int(APIS.getTotalCounter())
- }
- }
- // do {
- // if API.nGetCLXConnState() == 0 {
- // let id = Utils.getConnectionID()
- // try API.initConnection(sAPIK: Nexilis.sAPIKey, cbiI: Callback(), sTCPAddr: Nexilis.ADDRESS, nTCPPort: Nexilis.PORT, sUserID: id, sStartWH: "09:00")
- // while API.nGetCLXConnState() == 0 {
- // print("nGetCLXConnState: 0")
- // Thread.sleep(forTimeInterval: 1)
- // }
- // print("nGetCLXConnState: lewat")
- // getMessage()
- // } else {
- // getMessage()
- // }
- // func getMessage() {
- // if let result = Nexilis.writeSync(message: CoreMessage_TMessageBank.getMessageById(messageId: id), timeout: 30 * 1000) {
- // print("result: \(result.toLogString())")
- // if result.isOk() {
- // let respData = result.getBody(key: CoreMessage_TMessageKey.DATA)
- // if let data = Data(base64Encoded: respData, options: .ignoreUnknownCharacters),
- // let decodedString = String(data: data, encoding: .utf8) {
- // let message = TMessage(data: decodedString)
- // print("message: \(message.toLogString())")
- // }
- // } else {
- //
- // }
- // } else {
- // }
- // }
- // } catch {
- //
- // }
- }
- }
-
- public static func addNotificationNexilis(_ message: TMessage) {
- var text = message.getBody(key: CoreMessage_TMessageKey.MESSAGE_TEXT)
- text = text.toNormalString()
- let nameUser = message.getBody(key: CoreMessage_TMessageKey.F_DISPLAY_NAME)
- var threadIdentifier = message.getBody(key: CoreMessage_TMessageKey.OPPOSITE_PIN)
- let scope = message.getBody(key: CoreMessage_TMessageKey.MESSAGE_SCOPE_ID)
- if threadIdentifier.isEmpty {
- if scope == "4" {
- threadIdentifier = message.getBody(key: CoreMessage_TMessageKey.CHAT_ID).isEmpty ? message.getBody(key: CoreMessage_TMessageKey.L_PIN) : message.getBody(key: CoreMessage_TMessageKey.CHAT_ID)
- } else {
- threadIdentifier = message.getBody(key: CoreMessage_TMessageKey.F_PIN)
- }
- }
- let messageId = message.getBody(key: CoreMessage_TMessageKey.MESSAGE_ID)
- var nameSubtitle = ""
- let imageId = CoreMessage_TMessageKey.IMAGE_ID
- let videoId = CoreMessage_TMessageKey.VIDEO_ID
- let fileId = CoreMessage_TMessageKey.FILE_ID
- let audioId = CoreMessage_TMessageKey.AUDIO_ID
- let attachmentFlag = CoreMessage_TMessageKey.ATTACHMENT_FLAG
- let messageScopeId = CoreMessage_TMessageKey.MESSAGE_SCOPE_ID
- let credential = CoreMessage_TMessageKey.CREDENTIAL
- let gif_id = CoreMessage_TMessageKey.GIF_ID
- let is_secret = CoreMessage_TMessageKey.IS_SECRET
- if message.getBody(key: is_secret) == "1" {
- text = "You got messages..."
- } else if message.getBody(key: gif_id) != "" {
- text = "Sent GIF 🎬"
- } else if !message.getBody(key: imageId).isEmpty {
- text = "Sent Image 📷"
- } else if message.getBody(key: attachmentFlag) == "11" {
- text = "Sent Sticker ❤️"
- } else if !message.getBody(key: videoId).isEmpty {
- text = "Sent Video 📹"
- } else if !message.getBody(key: fileId).isEmpty {
- if message.getBody(key: messageScopeId) == "18" {
- text = "Sent Form 📄"
- } else {
- text = "Sent File 📄"
- }
- } else if !message.getBody(key: audioId).isEmpty {
- text = "Sent Audio ♫"
- } else if text.contains("Share%20location%20") {
- text = "Sent Location 📌"
- } else if message.getBody(key: attachmentFlag) == "27" {
- text = "Sent Live Streaming"
- } else if message.getBody(key: attachmentFlag) == "26" {
- text = "Sent Seminar"
- } else if message.getBody(key: attachmentFlag) == "25" {
- text = "Sent Video Conference Room"
- } else if message.getBody(key: attachmentFlag) == "24" {
- text = "Sent Quiz"
- } else if message.getBody(key: credential) == "1" {
- text = "Sent Confidential Message"
- }
- var type = "1"
- var nameTopic = "Lounge".localized()
- var idGroup = ""
- if scope == "3" || scope == "18" || scope == "5"{
- type = "0"
- }
- var soundId: String = SecureUserDefaults.shared.value(forKey: "newNotifSoundPersonal") ?? "001:Nexilis Message (Default)"
- if type == "1" {
- Database.shared.database?.inTransaction({ (fmdb, rollback) in
- do {
- if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "SELECT title, group_id FROM DISCUSSION_FORUM WHERE chat_id='\(threadIdentifier)'"), cursor.next() {
- nameTopic = cursor.string(forColumnIndex: 0) ?? ""
- idGroup = cursor.string(forColumnIndex: 1) ?? ""
- cursor.close()
- }
- if idGroup.isEmpty {
- idGroup = threadIdentifier
- }
- if let cursorGroup = Database.shared.getRecords(fmdb: fmdb, query: "SELECT f_name, image_id FROM GROUPZ WHERE group_id='\(idGroup)'"), cursorGroup.next() {
- let nameGroup = cursorGroup.string(forColumnIndex: 0) ?? ""
- nameSubtitle = "\(nameGroup) (\(nameTopic))"
- cursorGroup.close()
- }
- } catch {
- rollback.pointee = true
- print("Access database error: \(error.localizedDescription)")
- }
- })
- soundId = SecureUserDefaults.shared.value(forKey: "newNotifSoundGroup") ?? "001:Nexilis Message (Default)"
- if idGroup.isEmpty {
- return
- }
- }
- var nameSound = soundId.components(separatedBy: ":")[1].replacingOccurrences(of: " ", with: "_")
- var fromPref = false
- if nameSound.contains("_(Default)") {
- if !Utils.getDefaultIncomingMsg().isEmpty {
- nameSound = Utils.getDefaultIncomingMsg()
- fromPref = true
- } else {
- nameSound = nameSound.replacingOccurrences(of: "_(Default)", with: "")
- }
- }
- copySoundToLocalPath(nameSound, fromPref)
- let center = UNUserNotificationCenter.current()
- let content = UNMutableNotificationContent()
- content.title = nameUser
- if type == "1" {
- content.body = text.richText(group_id: idGroup).string
- content.subtitle = nameSubtitle
- } else {
- content.body = text.richText().string
- }
- content.userInfo = ["id" : threadIdentifier, "type" : type]
- content.sound = UNNotificationSound(named: UNNotificationSoundName("\(nameSound).mp3"))
- let request = UNNotificationRequest(identifier: messageId, content: content, trigger: nil)
- center.add(request) { error in
- if let error = error {
- print("Error scheduling notification: \(error.localizedDescription)")
- }
- }
- DispatchQueue.main.async {
- UIApplication.shared.applicationIconBadgeNumber = Int(APIS.getTotalCounter())
- }
- }
-
- private static func copySoundToLocalPath(_ nameSound: String, _ fromPref: Bool) {
- var sourceURL: URL?
- if fromPref {
- let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
- let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
- let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
- if let dirPath = paths.first {
- let audioURL = URL(fileURLWithPath: dirPath).appendingPathComponent(nameSound)
- if FileManager.default.fileExists(atPath: audioURL.path) {
- sourceURL = audioURL
- } else if FileEncryption.shared.isSecureExists(filename: nameSound) {
- do {
- if var audioData = try FileEncryption.shared.readSecure(filename: nameSound) {
- let dataDecrypt = FileEncryption.shared.decryptFileFromServer(data: audioData)
- if dataDecrypt != nil {
- audioData = dataDecrypt!
- }
- let cachesDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
- let tempPath = cachesDirectory.appendingPathComponent(nameSound)
- try audioData.write(to: tempPath)
- sourceURL = tempPath
- }
- } catch {
-
- }
- } else {
- sourceURL = Bundle.resourceBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
- if sourceURL == nil {
- sourceURL = Bundle.resourcesMediaBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
- }
- }
- }
- } else {
- sourceURL = Bundle.resourceBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
- if sourceURL == nil {
- sourceURL = Bundle.resourcesMediaBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
- }
- }
- if sourceURL == nil {
- return
- }
- let fileManager = FileManager.default
- let soundDirectory = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!.appendingPathComponent("Sounds", isDirectory: true)
- if !fileManager.fileExists(atPath: soundDirectory.path) {
- do {
- try fileManager.createDirectory(at: soundDirectory, withIntermediateDirectories: true, attributes: nil)
- } catch {
- print("Error creating Sounds directory: \(error)")
- return
- }
- }
- let destinationURL = soundDirectory.appendingPathComponent("\(nameSound).mp3")
- if !fileManager.fileExists(atPath: destinationURL.path) {
- do {
- try fileManager.copyItem(at: sourceURL!, to: destinationURL)
- } catch {
-
- }
- }
- }
-
- public static func openNotificationNexilis(_ response: UNNotificationResponse) {
- DispatchQueue.main.async{
- if let userInfo = response.notification.request.content.userInfo as? [String: String] {
- let id = userInfo["id"] ?? ""
- let type = userInfo["type"] ?? ""
- let callType = userInfo["callType"] ?? ""
- if let navigationC = UIApplication.shared.visibleViewController as? UINavigationController {
- if navigationC.viewControllers[navigationC.viewControllers.count - 1] is EditorPersonal || navigationC.viewControllers[navigationC.viewControllers.count - 1] is EditorGroup {
- navigationC.popViewController(animated: true)
- }
- }
- showEditorOrCallFromAPN(id, type, callType)
- } else {
- let userInfo = response.notification.request.content.userInfo
- DispatchQueue.main.async {
- if let message_id = userInfo[CoreMessage_TMessageKey.MESSAGE_ID] as? String {
- var f_pin = ""
- var l_pin = ""
- var message_scope_id = ""
- var pin = ""
- var chat_id = ""
- Database.shared.database?.inTransaction({ (fmdb, rollback) in
- if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select f_pin, l_pin, message_scope_id, chat_id from MESSAGE where message_id = '\(message_id)'"), cursor.next() {
- f_pin = cursor.string(forColumnIndex: 0) ?? ""
- l_pin = cursor.string(forColumnIndex: 1) ?? ""
- message_scope_id = cursor.string(forColumnIndex: 2) ?? ""
- chat_id = cursor.string(forColumnIndex: 3) ?? ""
- pin = f_pin == User.getMyPin() ? l_pin : f_pin
- if message_scope_id == "4" {
- pin = chat_id.isEmpty ? l_pin : chat_id
- }
- cursor.close()
- }
- })
- if let navigationC = UIApplication.shared.visibleViewController as? UINavigationController {
- if navigationC.viewControllers[navigationC.viewControllers.count - 1] is EditorPersonal || navigationC.viewControllers[navigationC.viewControllers.count - 1] is EditorGroup {
- navigationC.popViewController(animated: false)
- }
- }
- print("HUHU \(f_pin) \(l_pin) \(message_scope_id)")
- showEditorOrCallFromAPN(pin, message_scope_id == "4" ? "1" : "0", "CL01")
- }
- }
- }
- }
- // UNUserNotificationCenter.current().removeAllDeliveredNotifications()
- }
-
- private static func showEditorOrCallFromAPN(_ id: String, _ type: String, _ callType: String) {
- if type == "0" {
- if User.getDataCanNil(pin: id) == nil && id != "-999" && id != "-997" {
- return
- }
- let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
- editorPersonalVC.hidesBottomBarWhenPushed = true
- editorPersonalVC.unique_l_pin = id
- editorPersonalVC.fromNotification = true
- let navigationController = CustomNavigationController(rootViewController: editorPersonalVC)
- navigationController.modalPresentationStyle = .fullScreen
- navigationController.navigationBar.tintColor = .white
- navigationController.navigationBar.barTintColor = UIApplication.shared.visibleViewController?.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 is UINavigationController && Nexilis.fromMAB {
- editorPersonalVC.fromNotification = false
- UIApplication.shared.visibleViewController?.show(editorPersonalVC, sender: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
- }
- } else if type == "1" {
- var groupExist = false
- Database.shared.database?.inTransaction({ (fmdb, rollback) in
- var idGroup = ""
- if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "SELECT title, group_id FROM DISCUSSION_FORUM WHERE chat_id='\(id)'"), cursor.next() {
- groupExist = true
- cursor.close()
- } else {
- if idGroup.isEmpty {
- idGroup = id
- }
- if let cursorGroup = Database.shared.getRecords(fmdb: fmdb, query: "SELECT f_name, image_id FROM GROUPZ WHERE group_id='\(idGroup)'"), cursorGroup.next() {
- groupExist = true
- cursorGroup.close()
- }
- }
- })
- if !groupExist {
- return
- }
- let editorGroupVC = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "editorGroupVC") as! EditorGroup
- editorGroupVC.hidesBottomBarWhenPushed = true
- editorGroupVC.unique_l_pin = id
- editorGroupVC.fromNotification = true
- let navigationController = CustomNavigationController(rootViewController: editorGroupVC)
- navigationController.modalPresentationStyle = .fullScreen
- navigationController.navigationBar.tintColor = .white
- navigationController.navigationBar.barTintColor = UIApplication.shared.visibleViewController?.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 is UINavigationController && Nexilis.fromMAB {
- editorGroupVC.fromNotification = false
- UIApplication.shared.visibleViewController?.show(editorGroupVC, sender: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
- }
- } else if type == "CL03" {
- Nexilis.stopRingtoneCall()
- if !Nexilis.callAPNActivated {
- return
- }
- if callType == "1" {
- if let user = User.getData(pin: id), user.firstName == "User".localized() {
- return
- }
- let controller = QmeraAudioViewController()
- controller.isOutgoing = false
- controller.user = User.getData(pin: id)
- controller.autoAcceptAPN = true
- controller.modalPresentationStyle = .overCurrentContext
- if UIApplication.shared.visibleViewController is UIAlertController {
- let vc = UIApplication.shared.visibleViewController as! UIAlertController
- vc.dismiss(animated: true, completion: {
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(controller, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(controller, animated: true, completion: nil)
- }
- })
- return
- }
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(controller, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(controller, animated: true, completion: nil)
- }
- } else {
- if let user = User.getData(pin: id), user.firstName == "User".localized() {
- return
- }
- let videoController = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "videoVCQmera") as! QmeraVideoViewController
- videoController.fPin = id
- videoController.isInisiator = false
- videoController.autoAcceptAPN = true
- let navigationController = CustomNavigationController(rootViewController: videoController)
- navigationController.modalPresentationStyle = .fullScreen
- if UIApplication.shared.visibleViewController is UIAlertController {
- let vc = UIApplication.shared.visibleViewController as! UIAlertController
- vc.dismiss(animated: true, completion: {
- 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)
- }
- })
- return
- }
- 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)
- }
- }
- }
- }
-
- public static func checkAppStateisBackground() -> Bool {
- let state = UIApplication.shared.applicationState
-
- switch state {
- case .active:
- return false
- case .inactive:
- return false
- case .background:
- return true
- @unknown default:
- return false
- }
- }
-
- public static func enterBackground() {
- // if !API.bAVisOngoing() {
- // API.deinitConnection()
- // }
- notifTimer.invalidate()
- }
-
- public static var notifTimer = Timer()
- public static var stopNotif = false
- public static var afterEnterForeground = false
- public static func enterForeground() {
- APIS.checkNotificationPermission(completion: { isAllowed in
- if !isAllowed {
- showEnableNotificationsAlert()
- } else {
- UIApplication.shared.registerForRemoteNotifications()
- }
- })
- DispatchQueue.main.async {
- stopNotif = true
- self.notifTimer = Timer.scheduledTimer(withTimeInterval: 30, repeats: false) { _ in
- stopNotif = false
- }
- do {
- if !Nexilis.afterConnect && API.nGetCLXConnState() == 0 {
- let id = Utils.getConnectionID()
- try API.initConnection(sAPIK: Nexilis.sAPIKey, cbiI: Callback(), sTCPAddr: Nexilis.ADDRESS, nTCPPort: Nexilis.PORT, sUserID: id, sStartWH: "09:00")
- }
- // listIdentifierNotif.removeAll()
- Nexilis.afterConnect = false
- } catch {
- }
- }
- checkDataForShareExtension()
- UIApplication.shared.applicationIconBadgeNumber = 0
- UNUserNotificationCenter.current().removeAllDeliveredNotifications()
- afterEnterForeground = true
- }
-
- public static func willEnterForeground() {
- if APIS.uuidCall != nil {
- CallManager.shared.endCall(uuid: APIS.uuidCall!) {
- APIS.uuidCall = nil
- }
- }
- }
-
- private static func checkNotificationPermission(completion: @escaping (Bool) -> Void) {
- let center = UNUserNotificationCenter.current()
-
- center.getNotificationSettings { settings in
- DispatchQueue.main.async {
- switch settings.authorizationStatus {
- case .authorized, .provisional, .notDetermined:
- completion(true) // Notifications are allowed
- case .denied, .ephemeral:
- completion(false) // Notifications are disabled or not requested
- @unknown default:
- completion(false)
- }
- }
- }
- }
-
- private static func showEnableNotificationsAlert() {
- guard !isAlertPresented else { return }
- isAlertPresented = true
- let alertController = LibAlertController(
- title: "Enable Notification".localized(),
- message: "To stay updated, please enable notification in the Settings.".localized(),
- preferredStyle: .alert
- )
-
- alertController.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: nil))
-
- alertController.addAction(UIAlertAction(title: "Go to Settings".localized(), style: .default, handler: { _ in
- isAlertPresented = false
- openAppSettings()
- }))
-
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(alertController, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(alertController, animated: true, completion: nil)
- }
- }
-
- private static func openAppSettings() {
- if let settingsURL = URL(string: UIApplication.openSettingsURLString) {
- if UIApplication.shared.canOpenURL(settingsURL) {
- UIApplication.shared.open(settingsURL, options: [:], completionHandler: nil)
- }
- }
- }
-
- public static func willTerminate() {
- Nexilis.destroyAll()
- }
-
- public static func checkDataForShareExtension() {
- DispatchQueue.global().async {
- if let userDefaults = UserDefaults(suiteName: nameGroupShared) {
- if let value = userDefaults.string(forKey: "sharedItem") {
- if !value.isEmpty {
- if let jsonData = value.data(using: .utf8) {
- do {
- if let json = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] {
- let typeImage = 2
- let typeVideo = 3
- let typeFile = 4
- let typeAudio = 5
- let typeContact = json["typeContact"] as? String ?? "0"
- var data = json["data"] as? String ?? ""
- let idContact = json["idContact"] as? String ?? ""
- let typeShare = json["typeShare"] as? Int ?? 1
- var message = TMessage()
- var groupId = ""
- var chatId = ""
- let scopeId = typeContact == "1" ? "4" : "3"
- let thumb = json["thumb"] as? String ?? ""
- let imageId = json["image"] as? String ?? ""
- let videoId = json["video"] as? String ?? ""
- let fileId = json["file"] as? String ?? ""
- let audioId = json["audio"] as? String ?? ""
- var renamedFileId = ""
- var renamedAudioId = ""
- var attachmentFlag = ""
- if scopeId == "4" {
- Database.shared.database?.inTransaction({ (fmdb, rollback) in
- if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "SELECT group_id id FROM DISCUSSION_FORUM WHERE chat_id = '\(idContact)'"), cursor.next() {
- groupId = cursor.string(forColumnIndex: 0) ?? ""
- chatId = idContact
- cursor.close()
- } else {
- groupId = idContact
- }
- })
- }
- if typeShare == typeImage {
- if let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: nameGroupShared) {
- let sharedImageURL = appGroupURL.appendingPathComponent(imageId)
- let sharedThumbURL = appGroupURL.appendingPathComponent(thumb)
- let documentDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
- if FileManager.default.fileExists(atPath: sharedImageURL.path) {
- let file = documentDir.appendingPathComponent(imageId)
- if !FileManager().fileExists(atPath: file.path) {
- try? FileManager.default.copyItem(at: sharedImageURL, to: file)
- }
- }
- if FileManager.default.fileExists(atPath: sharedThumbURL.path) {
- let file = documentDir.appendingPathComponent(thumb)
- if !FileManager().fileExists(atPath: file.path) {
- try? FileManager.default.copyItem(at: sharedThumbURL, to: file)
- }
- }
- }
- attachmentFlag = "1"
- } else if typeShare == typeVideo {
- if let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: nameGroupShared) {
- let sharedVideoURL = appGroupURL.appendingPathComponent(videoId)
- let sharedThumbURL = appGroupURL.appendingPathComponent(thumb)
- let documentDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
- if FileManager.default.fileExists(atPath: sharedVideoURL.path) {
- let file = documentDir.appendingPathComponent(videoId)
- if !FileManager().fileExists(atPath: file.path) {
- try? FileManager.default.copyItem(at: sharedVideoURL, to: file)
- }
- }
- if FileManager.default.fileExists(atPath: sharedThumbURL.path) {
- let file = documentDir.appendingPathComponent(thumb)
- if !FileManager().fileExists(atPath: file.path) {
- try? FileManager.default.copyItem(at: sharedThumbURL, to: file)
- }
- }
- }
- attachmentFlag = "2"
- } else if typeShare == typeFile {
- if let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: nameGroupShared) {
- renamedFileId = "Nexilis_\(Date().currentTimeMillis())_" + fileId
- let sharedFileURL = appGroupURL.appendingPathComponent(fileId)
- let documentDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
- if FileManager.default.fileExists(atPath: sharedFileURL.path) {
- let file = documentDir.appendingPathComponent(renamedFileId)
- if !FileManager().fileExists(atPath: file.path) {
- try? FileManager.default.copyItem(at: sharedFileURL, to: file)
- }
- }
- data = "\(fileId)|\(data)"
- }
- attachmentFlag = "6"
- } else if typeShare == typeAudio {
- if let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: nameGroupShared) {
- renamedAudioId = "Nexilis_\(Date().currentTimeMillis())_" + audioId
- let sharedFileURL = appGroupURL.appendingPathComponent(audioId)
- let documentDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
- if FileManager.default.fileExists(atPath: sharedFileURL.path) {
- let file = documentDir.appendingPathComponent(renamedAudioId)
- if !FileManager().fileExists(atPath: file.path) {
- try? FileManager.default.copyItem(at: sharedFileURL, to: file)
- }
- }
- data = "\(audioId)|\(data)"
- }
- attachmentFlag = "5"
- }
- message = CoreMessage_TMessageBank.sendMessage(l_pin: groupId.isEmpty ? idContact : groupId, message_scope_id: scopeId, status: scopeId == "3" ? "1" : "2", message_text: data, credential: "0", attachment_flag: attachmentFlag, ex_blog_id: "", message_large_text: "", ex_format: "", image_id: imageId, audio_id: renamedAudioId, video_id: videoId, file_id: renamedFileId, thumb_id: thumb, reff_id: "", read_receipts: "4", chat_id: chatId, is_call_center: "0", call_center_id: "", opposite_pin: scopeId == "3" ? (User.getMyPin() ?? "") : idContact, gif_id: "", isForwarded: "0", isSecret: "0")
- Nexilis.addQueueMessage(message: message)
- userDefaults.set("", forKey: "sharedItem")
- userDefaults.synchronize()
- }
- } catch {
- print("Error parsing JSON: \(error)")
- }
- }
- }
- }
- }
- DispatchQueue.global().asyncAfter(deadline: .now() + 0.5, execute: {
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadTabChats"), object: nil, userInfo: nil)
- })
- }
- }
-
- public static func setDataForShareExtension() {
- DispatchQueue.global().async {
- if let userDefaults = UserDefaults(suiteName: nameGroupShared) {
- Database.shared.database?.inTransaction({ (fmdb, rollback) in
- var dataShared: [[String: Any]] = []
- if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "SELECT f_pin id, image_id image, first_name || ' ' || ifnull(last_name, '') name FROM BUDDY WHERE f_pin != '\(User.getMyPin() ?? "")' AND f_pin != '-997' AND official_account != '1'") {
- while cursor.next() {
- var dataTemp: [String: Any] = [:]
- for columnIndex in 0..<cursor.columnCount {
- if let columnName = cursor.columnName(for: columnIndex) {
- if let value = cursor.object(forColumn: columnName) {
- if columnName == "image" {
- dataTemp[columnName] = value
- if let imageString = dataTemp[columnName] as? String, !imageString.isEmpty {
- do {
- let documentDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
- let file = documentDir.appendingPathComponent(imageString)
- if FileManager().fileExists(atPath: file.path) {
- if let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: nameGroupShared) {
- let sharedFileURL = appGroupURL.appendingPathComponent(imageString)
- if !FileManager.default.fileExists(atPath: sharedFileURL.path) {
- try? FileManager.default.copyItem(at: file, to: sharedFileURL)
- }
- }
- }
- } catch {
-
- }
- }
- } else {
- dataTemp[columnName] = value
- }
- dataTemp["type"] = 0
- }
- }
- }
- dataShared.append(dataTemp)
- }
- cursor.close()
- }
- if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "SELECT group_id id, image_id image, f_name name FROM GROUPZ WHERE official != 1") {
- while cursor.next() {
- var dataTemp: [String: Any] = [:]
- for columnIndex in 0..<cursor.columnCount {
- if let columnName = cursor.columnName(for: columnIndex) {
- if let value = cursor.object(forColumn: columnName) {
- if columnName == "name" {
- dataTemp[columnName] = "\(value) (Lounge)"
- } else if columnName == "image" {
- dataTemp[columnName] = value
- if let imageString = dataTemp[columnName] as? String, !imageString.isEmpty {
- do {
- let documentDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
- let file = documentDir.appendingPathComponent(imageString)
- if FileManager().fileExists(atPath: file.path) {
- if let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: nameGroupShared) {
- let sharedFileURL = appGroupURL.appendingPathComponent(imageString)
- if !FileManager.default.fileExists(atPath: sharedFileURL.path) {
- try? FileManager.default.copyItem(at: file, to: sharedFileURL)
- }
- }
- }
- } catch {
-
- }
- }
- } else {
- dataTemp[columnName] = value
- }
- dataTemp["type"] = 1
- }
- }
- }
- dataShared.append(dataTemp)
- let group_id = cursor.string(forColumnIndex: 0) ?? ""
- let image_group = cursor.string(forColumnIndex: 1) ?? ""
- let name_group = cursor.string(forColumnIndex: 2) ?? ""
- if let cursorTopic = Database.shared.getRecords(fmdb: fmdb, query: "SELECT chat_id id, thumb image, title name FROM DISCUSSION_FORUM WHERE group_id = '\(group_id)'") {
- while cursorTopic.next() {
- var dataTempTopic: [String: Any] = [:]
- for columnIndex in 0..<cursorTopic.columnCount {
- if let columnName = cursorTopic.columnName(for: columnIndex) {
- if let value = cursorTopic.object(forColumn: columnName) {
- if columnName == "name" {
- dataTempTopic[columnName] = "\(name_group) (\(value))"
- } else if columnName == "image" {
- dataTempTopic[columnName] = "\(value)".isEmpty ? image_group : "\(value)"
- if let imageString = dataTempTopic[columnName] as? String, !imageString.isEmpty {
- do {
- let documentDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
- let file = documentDir.appendingPathComponent(imageString)
- if FileManager().fileExists(atPath: file.path) {
- if let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: nameGroupShared) {
- let sharedFileURL = appGroupURL.appendingPathComponent(imageString)
- if !FileManager.default.fileExists(atPath: sharedFileURL.path) {
- try? FileManager.default.copyItem(at: file, to: sharedFileURL)
- }
- }
- }
- } catch {
-
- }
- }
- } else {
- dataTempTopic[columnName] = value
- }
- dataTempTopic["type"] = 1
- }
- }
- }
- dataShared.append(dataTempTopic)
- }
- cursorTopic.close()
- }
- }
- cursor.close()
- }
- do {
- let jsonData = try JSONSerialization.data(withJSONObject: dataShared, options: .prettyPrinted)
- if let jsonString = String(data: jsonData, encoding: .utf8) {
- userDefaults.set(jsonString, forKey: "shareContacts")
- userDefaults.synchronize()
- }
- } catch {
- print("Error converting to JSON: \(error)")
- }
- })
- }
- }
- }
-
- public static func setCheckEmulator(isActive: Bool) {
- // Utils.bCheckEmulator = isActive
- }
-
- public static func setCheckRootedDevice(isActive: Bool) {
- // Utils.bCheckRooted = isActive
- }
-
- public static func setPreventScreenCapture(isActive: Bool) {
- // Utils.bPreventScreenCapture = isActive
- }
-
- public static func setNameGroupShare(_ name: String) {
- nameGroupShared = name
- }
-
- public static func openImageNexilis(image: UIImage, data: Data? = nil, isGIF: Bool = false) {
- let previewImageVC = PreviewAttachmentImageVideo(nibName: "PreviewAttachmentImageVideo", bundle: Bundle.resourceBundle(for: Nexilis.self))
- previewImageVC.image = image
- previewImageVC.isHiddenTextField = true
- previewImageVC.isGIF = isGIF
- previewImageVC.dataGIF = data
- previewImageVC.modalPresentationStyle = .custom
- previewImageVC.modalTransitionStyle = .crossDissolve
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(previewImageVC, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(previewImageVC, animated: true, completion: nil)
- }
- }
-
- public static func openVideoNexilis(videoURL: URL) {
- let player = AVPlayer(url: videoURL)
- let playerVC = AVPlayerViewController()
- playerVC.modalPresentationStyle = .custom
- playerVC.player = player
- if UIApplication.shared.visibleViewController?.navigationController != nil {
- UIApplication.shared.visibleViewController?.navigationController?.present(playerVC, animated: true, completion: nil)
- } else {
- UIApplication.shared.visibleViewController?.present(playerVC, animated: true, completion: nil)
- }
- }
-
- private static var appNm = "";
- public static func getAppNm() -> String {
- return appNm
- }
-
- private static var nameGroupShared = "group.nexilis.share";
- public static func getnameGroupShared() -> String {
- return nameGroupShared
- }
- }
- extension UINavigationController {
- func defaultStyle() {
- self.view.backgroundColor = self.traitCollection.userInterfaceStyle == .dark ? .black : .white
- self.modalPresentationStyle = .fullScreen
- self.navigationBar.tintColor = .white
- self.navigationBar.barTintColor = self.traitCollection.userInterfaceStyle == .dark ? .blackDarkMode : .mainColor
- self.navigationBar.isTranslucent = false
- self.navigationBar.overrideUserInterfaceStyle = .dark
- self.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]
- self.navigationBar.titleTextAttributes = textAttributes
- }
- }
|