ViewController.swift 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. //
  2. // ViewController.swift
  3. // TestQmeraLite
  4. //
  5. // Created by Qindi on 29/11/21.
  6. //
  7. import UIKit
  8. import NexilisLite
  9. import AVKit
  10. import AVFoundation
  11. import SwiftUI
  12. import Speech
  13. import Alamofire
  14. import WebKit
  15. class ViewController: UITabBarController, UITabBarControllerDelegate, SettingMABDelegate, WKNavigationDelegate {
  16. let playerController = AVPlayerViewController()
  17. static var sURL = "https://www.google.com"
  18. static var tab3 = "0"
  19. public var isShow: Bool = false
  20. public static var chatButton = UIButton()
  21. public static var callButton = UIButton()
  22. public static var ccButton = UIButton()
  23. public static var streamingButton = UIButton()
  24. public static var postButton = UIButton()
  25. public static var middleButton = UIButton()
  26. var buttonChatGR : UITapGestureRecognizer?
  27. var buttonCallGR : UITapGestureRecognizer?
  28. var buttonCCGR : UITapGestureRecognizer?
  29. var buttonStreamGR : UITapGestureRecognizer?
  30. var floating : FloatingButton?
  31. var firstTab : FirstTabViewController?
  32. var secondTab : SecondTabViewController?
  33. var thirdTab : ThirdTabViewController?
  34. var fourthTab : FourthTabViewController?
  35. let emptyTab = EmptyTabViewController()
  36. public static var isTab1 = true
  37. public static var isTab2 = false
  38. public static var isTab3 = false
  39. public static var isTab4 = false
  40. public static var isExpandButton = false
  41. public static var alwaysHideButton = false
  42. static var listPullFB: [String] = []
  43. static var datePullFB: Date?
  44. let welcomeVC = UIViewController()
  45. let privacyPolicyVC = UIViewController()
  46. var termVC: UIViewController?
  47. let welcomeDesc = UILabel()
  48. let termText = "Read our Terms of Service. Tap \"Agree and Continue\" to accept Terms of Service.".localized()
  49. let term = "Terms of Service.".localized()
  50. var firstLoad = true
  51. let privacyWV = WKWebView()
  52. let indicatorCounterFB = UIView()
  53. let labelCounterFB = UILabel()
  54. public static var def: ViewController?
  55. override func viewDidLoad() {
  56. super.viewDidLoad()
  57. DispatchQueue.main.async { [self] in
  58. while !Utils.getFinishInitPrefsr() {
  59. print("WAITING PREFS DONE")
  60. }
  61. startView()
  62. }
  63. }
  64. func startView() {
  65. let topBorder = CALayer()
  66. topBorder.backgroundColor = UIColor.gray.withAlphaComponent(0.5).cgColor
  67. topBorder.frame = CGRect(x: 0, y: 0, width: tabBar.frame.size.width, height: 1)
  68. tabBar.layer.addSublayer(topBorder)
  69. ViewController.def = self
  70. title = Bundle.main.displayName
  71. let customTab = PrefsUtil.getCustomTab().split(separator: ",")
  72. let cpaasMode = PrefsUtil.getCpaasMode()
  73. var tabs : [UIViewController] = []
  74. firstTab = storyboard?.instantiateViewController(withIdentifier: "firstTabVC") as? FirstTabViewController
  75. secondTab = storyboard?.instantiateViewController(withIdentifier: "secondTabVC") as? SecondTabViewController
  76. thirdTab = storyboard?.instantiateViewController(withIdentifier: "thirdTabVC") as? ThirdTabViewController
  77. fourthTab = storyboard?.instantiateViewController(withIdentifier: "fourthTabVC") as? FourthTabViewController
  78. self.delegate = self
  79. firstTab?.tabBarItem = UITabBarItem(title: "", image: resizeImage(image: UIImage(named: "tab_1_icon")!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal), selectedImage: resizeImage(image: UIImage(named: "tab_1_icon")!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal).withTintColor(.mainColor))
  80. secondTab?.tabBarItem = UITabBarItem(title: "", image: resizeImage(image: UIImage(named: "tab_2_icon")!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal), selectedImage: resizeImage(image: UIImage(named: "tab_2_icon")!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal).withTintColor(.mainColor))
  81. thirdTab?.tabBarItem = UITabBarItem(title: "", image: resizeImage(image: UIImage(named: "tab_3_icon")!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal), selectedImage: resizeImage(image: UIImage(named: "tab_3_icon")!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal).withTintColor(.mainColor))
  82. fourthTab?.tabBarItem = UITabBarItem(title: "", image: resizeImage(image: UIImage(named: "tab_4_icon")!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal), selectedImage: resizeImage(image: UIImage(named: "tab_4_icon")!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal).withTintColor(.mainColor))
  83. var i = 0
  84. var j = 0
  85. while j < customTab.count {
  86. if(((i == 1 && customTab.count == 3) || i == 2) &&
  87. (cpaasMode == PrefsUtil.CPAAS_MODE_DOCKED || cpaasMode == PrefsUtil.CPAAS_MODE_MIX)){
  88. tabs.append(emptyTab)
  89. }
  90. else {
  91. switch(customTab[j]){
  92. case "1":
  93. tabs.append(firstTab!)
  94. case "2":
  95. tabs.append(secondTab!)
  96. case "3":
  97. tabs.append(thirdTab!)
  98. case "4":
  99. tabs.append(fourthTab!)
  100. default:
  101. break
  102. }
  103. j += 1
  104. }
  105. i += 1
  106. }
  107. if(cpaasMode == PrefsUtil.CPAAS_MODE_BURGER){
  108. navigationController?.setNavigationBarHidden(false, animated: false)
  109. //print("cpaas mode burger")
  110. let childrenMenu : [UIAction] = [
  111. UIAction(title: "Contact Center", handler: {[weak self](_) in
  112. self?.ccTap()
  113. }),
  114. UIAction(title: "Chat", handler: {[weak self](_) in
  115. self?.chatTap()
  116. }),
  117. UIAction(title: "Call", handler: {[weak self](_) in
  118. self?.callTap()
  119. }),
  120. UIAction(title: "New Post", handler: {[weak self](_) in
  121. self?.postTap()
  122. }),
  123. UIAction(title: "Live Streaming", handler: {[weak self](_) in
  124. self?.streamTap()
  125. }),
  126. // UIAction(title: "Settings", handler: {[weak self](_) in
  127. // self?.settingTap()
  128. // }),
  129. ]
  130. let menu = UIMenu(title: "", children: childrenMenu)
  131. if !PrefsUtil.getIconDock().isEmpty {
  132. DispatchQueue.global().async {
  133. ViewController.getDataImageFromUrl(from: URL(string: PrefsUtil.getUrlDock()!)!) { data, response, error in
  134. guard let data = data, error == nil else { return }
  135. // always update the UI from the main thread
  136. DispatchQueue.main.async() { [self] in
  137. navigationItem.rightBarButtonItem = UIBarButtonItem(image: resizeImage(image: UIImage(data: data)!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal), primaryAction: .none, menu: menu)
  138. }
  139. }
  140. }
  141. } else {
  142. navigationItem.rightBarButtonItem = UIBarButtonItem(image: resizeImage(image: UIImage(named: "pb_button", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal), primaryAction: .none, menu: menu)
  143. }
  144. }
  145. if((cpaasMode == PrefsUtil.CPAAS_MODE_DOCKED || cpaasMode == PrefsUtil.CPAAS_MODE_MIX)){
  146. createMidFloatingButton()
  147. navigationController?.setNavigationBarHidden(true, animated: false)
  148. ViewController.pullActionButton()
  149. }
  150. // if((cpaasMode == PrefsUtil.CPAAS_MODE_FLOATING || cpaasMode == PrefsUtil.CPAAS_MODE_MIX)) {
  151. // let rect = CGRect(x: self.view.bounds.width - 100, y: self.view.bounds.height / 2, width: 50, height: 250)
  152. // floating = FloatingButton()
  153. // floating?.frame = rect
  154. // floating?.isShow = true
  155. //
  156. // view.addSubview(floating!)
  157. // navigationController?.setNavigationBarHidden(true, animated: false)
  158. // }
  159. self.setViewControllers(tabs, animated: false)
  160. if(cpaasMode == PrefsUtil.CPAAS_MODE_DOCKED || cpaasMode == PrefsUtil.CPAAS_MODE_MIX){
  161. if(customTab.count == 3){
  162. self.tabBar.items?[1].isEnabled = false
  163. }
  164. self.tabBar.items?[2].isEnabled = false
  165. }
  166. let center: NotificationCenter = NotificationCenter.default
  167. center.addObserver(self, selector: #selector(checkCounter), name: NSNotification.Name(rawValue: Nexilis.listenerReceiveChat), object: nil)
  168. center.addObserver(self, selector: #selector(checkCounter), name: NSNotification.Name(rawValue: "reloadTabChats"), object: nil)
  169. checkCounter()
  170. willappear()
  171. }
  172. static func getiPhoneModel() -> String {
  173. var systemInfo = utsname()
  174. uname(&systemInfo)
  175. let machineMirror = Mirror(reflecting: systemInfo.machine)
  176. let identifier = machineMirror.children.reduce("") { identifier, element in
  177. guard let value = element.value as? Int8, value != 0 else { return identifier }
  178. return identifier + String(UnicodeScalar(UInt8(value)))
  179. }
  180. var model = ""
  181. if let modelName = mapToDevice(identifier: identifier) {
  182. model = modelName
  183. } else {
  184. model = "Unknown"
  185. }
  186. return model
  187. }
  188. static func mapToDevice(identifier: String) -> String? {
  189. // Add mappings for iPhone models as needed
  190. switch identifier {
  191. case "iPhone1,1", "iPhone1,2", "iPhone2,1", "iPhone3,1", "iPhone3,2", "iPhone3,3", "iPhone4,1", "iPhone5,1", "iPhone5,2", "iPhone5,3", "iPhone5,4", "iPhone6,1", "iPhone6,2", "iPhone7,1", "iPhone7,2", "iPhone8,1", "iPhone8,2", "iPhone8,4", "iPhone9,1", "iPhone9,2", "iPhone9,3", "iPhone9,4", "iPhone10,1", "iPhone10,2", "iPhone10,4", "iPhone10,5": return "iPhone under X"
  192. default: return "iPhone X or newer"
  193. }
  194. }
  195. func getPrefs(key: String) -> TMessage {
  196. let tMessage = NexilisLite.TMessage()
  197. let me = UserDefaults.standard.string(forKey: "me")
  198. tMessage.mCode = "PPR"
  199. tMessage.mStatus = CoreMessage_TMessageUtil.getTID()
  200. tMessage.mBodies[CoreMessage_TMessageKey.F_PIN] = me
  201. tMessage.mBodies[CoreMessage_TMessageKey.KEY] = key
  202. return tMessage
  203. }
  204. @objc func checkCounter() {
  205. DispatchQueue.main.async { [self] in
  206. if self.viewControllers?.firstIndex(of: secondTab!) != nil {
  207. let counter = queryCountCounter()
  208. let indexSecondTab = self.viewControllers?.firstIndex(of: secondTab!)
  209. let viewSecondTab = self.tabBar.items?[indexSecondTab!].value(forKey: "view") as! UIView
  210. if counter > 0 {
  211. if !indicatorCounterFB.isDescendant(of: viewSecondTab) {
  212. let viewSecondTab = self.tabBar.items?[indexSecondTab!].value(forKey: "view") as! UIView
  213. indicatorCounterFB.backgroundColor = .red
  214. indicatorCounterFB.layer.cornerRadius = 7.5
  215. indicatorCounterFB.clipsToBounds = true
  216. viewSecondTab.addSubview(indicatorCounterFB)
  217. indicatorCounterFB.anchor(top: viewSecondTab.topAnchor, right: viewSecondTab.rightAnchor, paddingRight: 20, height: 15, minWidth: 15, maxWidth: 20)
  218. indicatorCounterFB.addSubview(labelCounterFB)
  219. labelCounterFB.anchor(left: indicatorCounterFB.leftAnchor, right: indicatorCounterFB.rightAnchor, paddingLeft: 5, paddingRight: 5, centerX: indicatorCounterFB.centerXAnchor, centerY: indicatorCounterFB.centerYAnchor)
  220. labelCounterFB.font = .systemFont(ofSize: 10)
  221. labelCounterFB.textColor = .white
  222. }
  223. if counter > 99 {
  224. labelCounterFB.text = "99+"
  225. } else {
  226. labelCounterFB.text = "\(counter)"
  227. }
  228. } else {
  229. if indicatorCounterFB.isDescendant(of: viewSecondTab) {
  230. indicatorCounterFB.removeFromSuperview()
  231. }
  232. }
  233. }
  234. }
  235. }
  236. private func queryCountCounter() -> Int32 {
  237. var counter: Int32?
  238. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  239. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "SELECT SUM(counter) FROM MESSAGE_SUMMARY"), cursor.next() {
  240. counter = cursor.int(forColumnIndex: 0)
  241. cursor.close()
  242. }
  243. })
  244. return counter ?? 0
  245. }
  246. // override func viewDidLayoutSubviews() {
  247. // super.viewDidLayoutSubviews()
  248. // tabBar.frame.size.height = 65
  249. // tabBar.frame.origin.y = view.frame.height - 65
  250. // }
  251. func settingDelegate() {
  252. if self.viewControllers?.firstIndex(of: fourthTab!) == nil {
  253. let vc = fourthTab!
  254. vc.notInTab = true
  255. self.navigationController?.show(vc, sender: nil)
  256. } else {
  257. self.selectedIndex = (self.viewControllers?.firstIndex(of: fourthTab!))!
  258. }
  259. }
  260. static var alertChangeProfile = LibAlertController()
  261. public static func checkIsChangePerson() -> Bool {
  262. let isChangeProfile = Utils.getSetProfile()
  263. if !isChangeProfile {
  264. alertChangeProfile.dismiss(animated: false)
  265. alertChangeProfile = LibAlertController(title: "Change Profile".localized(), message: "You must change your name to use this feature".localized().localized(), preferredStyle: .alert)
  266. alertChangeProfile.addAction(UIAlertAction(title: "Cancel".localized(), style: .destructive, handler: {_ in
  267. if ViewController.def?.viewControllers?.firstIndex(of: (ViewController.def?.firstTab)!) == ViewController.def?.selectedIndex {
  268. ViewController.def?.firstTab?.webView?.evaluateJavaScript("if(resumeAll){resumeAll();}")
  269. }
  270. if ViewController.def?.viewControllers?.firstIndex(of: (ViewController.def?.thirdTab)!) == ViewController.def?.selectedIndex {
  271. ViewController.def?.thirdTab?.webView?.evaluateJavaScript("if(resumeAll){resumeAll();}")
  272. }
  273. }))
  274. alertChangeProfile.addAction(UIAlertAction(title: "OK".localized(), style: UIAlertAction.Style.default, handler: {(_) in
  275. ViewController.resetTabSelected()
  276. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "signupsignin") as! SignUpSignIn
  277. controller.forceLogin = true
  278. controller.isDismiss = { newThumb in
  279. FirstTabViewController.forceRefresh = true
  280. ThirdTabViewController.forceRefresh = true
  281. FirstTabViewController.showModal = false
  282. ThirdTabViewController.showModal = false
  283. }
  284. let navigationController = UINavigationController(rootViewController: controller)
  285. navigationController.modalPresentationStyle = .fullScreen
  286. navigationController.navigationBar.tintColor = .white
  287. navigationController.navigationBar.barTintColor = .mainColor
  288. navigationController.navigationBar.isTranslucent = false
  289. navigationController.navigationBar.overrideUserInterfaceStyle = .dark
  290. navigationController.navigationBar.barStyle = .black
  291. let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]
  292. UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, for: .normal)
  293. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  294. navigationController.navigationBar.titleTextAttributes = textAttributes
  295. navigationController.view.backgroundColor = .mainColor
  296. ViewController.def?.show(b: false)
  297. ViewController.def?.thirdTab?.webView?.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  298. ViewController.def?.firstTab?.webView?.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  299. let rootVC = UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.rootViewController
  300. if rootVC?.presentedViewController == nil {
  301. rootVC?.present(navigationController, animated: true, completion: nil)
  302. } else {
  303. rootVC?.presentedViewController?.present(navigationController, animated: true, completion: nil)
  304. }
  305. }))
  306. let rootVC = UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.rootViewController
  307. if rootVC?.presentedViewController == nil {
  308. rootVC?.present(alertChangeProfile, animated: true, completion: nil)
  309. } else {
  310. rootVC?.presentedViewController?.present(alertChangeProfile, animated: true, completion: nil)
  311. }
  312. return false
  313. }
  314. return true
  315. }
  316. public static func resetTabSelected(){
  317. ViewController.isTab1 = true
  318. ViewController.isTab2 = false
  319. ViewController.isTab3 = false
  320. ViewController.isTab4 = false
  321. // if ViewController.isTab1 {
  322. // ViewController.imageTab1.image = UIImage(named: "tab_1_nexilis")!
  323. // }
  324. // else {
  325. // ViewController.imageTab1.image = UIImage(named: "tab_1_nexilis_off")!
  326. // }
  327. // if ViewController.isTab2 {
  328. // ViewController.imageTab2.image = UIImage(named: "tab_2_nexilis")!
  329. // }
  330. // else {
  331. // ViewController.imageTab2.image = UIImage(named: "tab_2_nexilis_off")!
  332. // }
  333. // if ViewController.isTab3 {
  334. // ViewController.imageTab3.image = UIImage(named: "tab_3_nexilis")!
  335. // }
  336. // else {
  337. // ViewController.imageTab3.image = UIImage(named: "tab_3_nexilis_off")!
  338. // }
  339. // if ViewController.isTab4 {
  340. // ViewController.imageTab4.image = UIImage(named: "tab_4_nexilis")!
  341. // }
  342. // else {
  343. // ViewController.imageTab4.image = UIImage(named: "tab_4_nexilis_off")!
  344. // }
  345. }
  346. override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
  347. let cpaasMode = PrefsUtil.getCpaasMode()
  348. let customTab = PrefsUtil.getCustomTab().split(separator: ",")
  349. if(cpaasMode == PrefsUtil.CPAAS_MODE_DOCKED || cpaasMode == PrefsUtil.CPAAS_MODE_MIX){
  350. if(customTab.count == 2){
  351. ViewController.isTab1 = item == tabBar.items?[0]
  352. ViewController.isTab2 = item == tabBar.items?[2]
  353. }
  354. else if(customTab.count == 3){
  355. ViewController.isTab1 = item == tabBar.items?[0]
  356. ViewController.isTab2 = item == tabBar.items?[2]
  357. ViewController.isTab3 = item == tabBar.items?[3]
  358. }
  359. else if(customTab.count == 4){
  360. ViewController.isTab1 = item == tabBar.items?[0]
  361. ViewController.isTab2 = item == tabBar.items?[1]
  362. ViewController.isTab3 = item == tabBar.items?[3]
  363. ViewController.isTab4 = item == tabBar.items?[4]
  364. }
  365. }
  366. else{
  367. ViewController.isTab1 = item == tabBar.items?[0]
  368. ViewController.isTab2 = item == tabBar.items?[1]
  369. if(customTab.count > 2){
  370. ViewController.isTab3 = item == tabBar.items?[2]
  371. }
  372. if(customTab.count > 3){
  373. ViewController.isTab4 = item == tabBar.items?[3]
  374. }
  375. }
  376. }
  377. func createMidFloatingButton(){
  378. var minYIpX: CGFloat = 0
  379. let iPhoneModel = ViewController.getiPhoneModel()
  380. if iPhoneModel == "iPhone X or newer" {
  381. minYIpX += 20
  382. }
  383. ViewController.chatButton = UIButton(frame: CGRect(x: self.view.bounds.width / 2 - 22.5 , y: self.view.bounds.height - 80 - minYIpX, width: 45, height: 45))
  384. ViewController.chatButton.setBackgroundImage(UIImage(named: "pb_button_chat", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal), for: .normal)
  385. ViewController.chatButton.layer.shadowColor = UIColor.black.cgColor
  386. ViewController.chatButton.layer.shadowOpacity = 0.1
  387. ViewController.chatButton.layer.shadowOffset = CGSize(width: 4, height: 4)
  388. ViewController.chatButton.addTarget(self, action: #selector(chatTap), for: .touchUpInside)
  389. ViewController.callButton = UIButton(frame: CGRect(x: self.view.bounds.width / 2 - 22.5 , y: self.view.bounds.height - 80 - minYIpX, width: 45, height: 45))
  390. ViewController.callButton.setBackgroundImage(UIImage(named: "pb_button_call", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal), for: .normal)
  391. ViewController.callButton.layer.shadowColor = UIColor.black.cgColor
  392. ViewController.callButton.layer.shadowOpacity = 0.1
  393. ViewController.callButton.layer.shadowOffset = CGSize(width: 4, height: 4)
  394. ViewController.callButton.addTarget(self, action: #selector(callTap), for: .touchUpInside)
  395. ViewController.ccButton = UIButton(frame: CGRect(x: self.view.bounds.width / 2 - 22.5 , y: self.view.bounds.height - 80 - minYIpX, width: 45, height: 45))
  396. ViewController.ccButton.setBackgroundImage(UIImage(named: "pb_button_cc", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal), for: .normal)
  397. ViewController.ccButton.layer.shadowColor = UIColor.black.cgColor
  398. ViewController.ccButton.layer.shadowOpacity = 0.1
  399. ViewController.ccButton.layer.shadowOffset = CGSize(width: 4, height: 4)
  400. ViewController.ccButton.addTarget(self, action: #selector(ccTap), for: .touchUpInside)
  401. ViewController.streamingButton = UIButton(frame: CGRect(x: self.view.bounds.width / 2 - 22.5 , y: self.view.bounds.height - 80 - minYIpX, width: 45, height: 45))
  402. ViewController.streamingButton.setBackgroundImage(UIImage(named: "pb_button_stream", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal), for: .normal)
  403. ViewController.streamingButton.layer.shadowColor = UIColor.black.cgColor
  404. ViewController.streamingButton.layer.shadowOpacity = 0.1
  405. ViewController.streamingButton.layer.shadowOffset = CGSize(width: 4, height: 4)
  406. ViewController.streamingButton.addTarget(self, action: #selector(streamTap), for: .touchUpInside)
  407. ViewController.postButton = UIButton(frame: CGRect(x: self.view.bounds.width / 2 - 22.5 , y: self.view.bounds.height - 80 - minYIpX, width: 45, height: 45))
  408. ViewController.postButton.setBackgroundImage(UIImage(named: "pb_button_post", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal), for: .normal)
  409. ViewController.postButton.layer.shadowColor = UIColor.black.cgColor
  410. ViewController.postButton.layer.shadowOpacity = 0.1
  411. ViewController.postButton.layer.shadowOffset = CGSize(width: 4, height: 4)
  412. ViewController.postButton.addTarget(self, action: #selector(postTap), for: .touchUpInside)
  413. let buttonCenterX = self.view.bounds.width / 2
  414. let buttonCenterY = self.view.bounds.height - self.tabBar.bounds.height
  415. //print("buttonCenterX \(buttonCenterX)")
  416. //print("buttonCenterY \(buttonCenterY)")
  417. ViewController.middleButton = UIButton(frame: CGRect(x: buttonCenterX - 40 , y: buttonCenterY - 40, width: 80, height: 80))
  418. if !PrefsUtil.getIconDock().isEmpty {
  419. if PrefsUtil.getIconDock().contains(".gif") {
  420. ViewController.middleButton.sd_setBackgroundImage(with: URL(string: PrefsUtil.getIconCenterAnim()!), for: .normal, completed: { (image, error, cacheType, imageURL) in
  421. if let error = error {
  422. print("Error loading image: \(error.localizedDescription)")
  423. } else {
  424. print("Image loaded successfully")
  425. }
  426. })
  427. } else {
  428. DispatchQueue.global().async {
  429. ViewController.getDataImageFromUrl(from: URL(string: PrefsUtil.getUrlDock()!)!) { data, response, error in
  430. guard let data = data, error == nil else { return }
  431. // always update the UI from the main thread
  432. DispatchQueue.main.async() {
  433. ViewController.middleButton.setBackgroundImage(UIImage(data: data), for: .normal)
  434. }
  435. }
  436. }
  437. }
  438. } else {
  439. ViewController.middleButton.setBackgroundImage(UIImage(named: "pb_button", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), for: .normal)
  440. }
  441. ViewController.middleButton.layer.shadowColor = UIColor.black.cgColor
  442. ViewController.middleButton.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
  443. ViewController.middleButton.layer.shadowOpacity = 1.0
  444. ViewController.middleButton.layer.shadowRadius = 5.0
  445. ViewController.middleButton.layer.masksToBounds = false
  446. ViewController.middleButton.layer.cornerRadius = 4.0
  447. ViewController.middleButton.addTarget(self, action: #selector(middleBtnTapped), for: .touchUpInside)
  448. let longPressMidButton = UILongPressGestureRecognizer(target: self, action: #selector(longPressMidBtn(gestureRecognizer:)))
  449. ViewController.middleButton.addGestureRecognizer(longPressMidButton)
  450. self.view.addSubview(ViewController.chatButton)
  451. self.view.addSubview(ViewController.callButton)
  452. self.view.addSubview(ViewController.ccButton)
  453. self.view.addSubview(ViewController.postButton)
  454. self.view.addSubview(ViewController.streamingButton)
  455. self.view.addSubview(ViewController.middleButton)
  456. ViewController.hideDockedButton()
  457. }
  458. @objc func longPressMidBtn(gestureRecognizer: UILongPressGestureRecognizer) {
  459. if gestureRecognizer.state == .began {
  460. if self.viewControllers?.firstIndex(of: fourthTab!) == nil {
  461. let vc = fourthTab!
  462. vc.notInTab = true
  463. self.navigationController?.show(vc, sender: nil)
  464. } else {
  465. self.selectedIndex = (self.viewControllers?.firstIndex(of: fourthTab!))!
  466. }
  467. }
  468. }
  469. func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
  470. if(viewController == secondTab){
  471. if(!ViewController.checkIsChangePerson()){
  472. return false
  473. }
  474. }
  475. return true
  476. }
  477. func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
  478. if viewController != secondTab {
  479. let idxTabChat = self.viewControllers?.firstIndex(where: {$0 == secondTab})
  480. if idxTabChat != nil {
  481. let vcTabChats = self.viewControllers![idxTabChat!] as! SecondTabViewController
  482. if vcTabChats.searchController.isActive {
  483. vcTabChats.searchController.isActive = false
  484. }
  485. }
  486. }
  487. let cpaasMode = PrefsUtil.getCpaasMode()
  488. var childrenMenu = [UIAction]()
  489. if(cpaasMode == PrefsUtil.CPAAS_MODE_BURGER){
  490. //print("cpaas mode burger")
  491. childrenMenu.append(contentsOf: [
  492. UIAction(title: "Contact Center", handler: {[weak self](_) in
  493. self?.ccTap()
  494. }),
  495. UIAction(title: "Chat", handler: {[weak self](_) in
  496. self?.chatTap()
  497. }),
  498. UIAction(title: "Call", handler: {[weak self](_) in
  499. self?.callTap()
  500. }),
  501. UIAction(title: "New Post", handler: {[weak self](_) in
  502. self?.postTap()
  503. }),
  504. UIAction(title: "Live Streaming", handler: {[weak self](_) in
  505. self?.streamTap()
  506. }),
  507. // UIAction(title: "Settings", handler: {[weak self](_) in
  508. // self?.settingTap()
  509. // }),
  510. ])
  511. if let vc = viewController as? SecondTabViewController {
  512. childrenMenu.append(contentsOf: vc.childrenMenu)
  513. }
  514. let menu = UIMenu(title: "", children: childrenMenu)
  515. if !PrefsUtil.getIconDock().isEmpty {
  516. DispatchQueue.global().async {
  517. ViewController.getDataImageFromUrl(from: URL(string: PrefsUtil.getUrlDock()!)!) { data, response, error in
  518. guard let data = data, error == nil else { return }
  519. // always update the UI from the main thread
  520. DispatchQueue.main.async() { [self] in
  521. navigationItem.rightBarButtonItem = UIBarButtonItem(image: resizeImage(image: UIImage(data: data)!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal), primaryAction: .none, menu: menu)
  522. }
  523. }
  524. }
  525. } else {
  526. navigationItem.rightBarButtonItem = UIBarButtonItem(image: resizeImage(image: UIImage(named: "pb_button", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal), primaryAction: .none, menu: menu)
  527. }
  528. }
  529. }
  530. override func viewWillAppear(_ animated: Bool) {
  531. if !firstLoad {
  532. willappear()
  533. } else {
  534. firstLoad = false
  535. }
  536. }
  537. func willappear() {
  538. let acceptTerm = PrefsUtil.getTerms()
  539. let enable_privacy_policy = PrefsUtil.getEnablePrivacyPolicy()
  540. if !acceptTerm {
  541. showWelocomeView()
  542. return
  543. } else if enable_privacy_policy && !PrefsUtil.getAgreePrivacyPolicy() {
  544. showPrivacyPolicyView()
  545. return
  546. } else {
  547. if !Utils.getForceAnonymous() && !Utils.getSetProfile() {
  548. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "changeDevice") as! ChangeDeviceViewController
  549. controller.forceLogin = true
  550. let navigationController = UINavigationController(rootViewController: controller)
  551. navigationController.navigationBar.tintColor = .white
  552. navigationController.navigationBar.barTintColor = .mainColor
  553. navigationController.navigationBar.isTranslucent = false
  554. navigationController.navigationBar.overrideUserInterfaceStyle = .dark
  555. navigationController.navigationBar.barStyle = .black
  556. let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]
  557. UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, for: .normal)
  558. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  559. navigationController.navigationBar.titleTextAttributes = textAttributes
  560. navigationController.view.backgroundColor = .mainColor
  561. navigationController.modalPresentationStyle = .fullScreen
  562. navigationController.modalTransitionStyle = .crossDissolve
  563. self.present(navigationController, animated: true)
  564. return
  565. }
  566. }
  567. self.selectedViewController?.viewWillAppear(false)
  568. }
  569. func showPrivacyPolicyView() {
  570. if let privacyView = privacyPolicyVC.view {
  571. let bgImage = UIImageView()
  572. privacyView.addSubview(bgImage)
  573. bgImage.anchor(top: privacyView.topAnchor, left: privacyView.leftAnchor, bottom: privacyView.bottomAnchor, right: privacyView.rightAnchor)
  574. bgImage.backgroundColor = .white
  575. DispatchQueue.global().async {
  576. if let listBg = PrefsUtil.getBackground() {
  577. if listBg.isEmpty {
  578. return
  579. }
  580. var bgChoosen = ""
  581. let arrayBg = listBg.split(separator: ",")
  582. bgChoosen = String(arrayBg[Int.random(in: 0..<arrayBg.count)])
  583. ViewController.getDataImageFromUrl(from: URL(string: PrefsUtil.getURLBase() + "/dashboardv2/uploads/background/" + bgChoosen)!) { data, response, error in
  584. guard let data = data, error == nil else { return }
  585. // always update the UI from the main thread
  586. DispatchQueue.main.async() {
  587. bgImage.image = UIImage(data: data)!
  588. }
  589. }
  590. }
  591. }
  592. let containerButton = UIView()
  593. privacyView.addSubview(containerButton)
  594. containerButton.anchor(left: privacyView.safeAreaLayoutGuide.leftAnchor, bottom: privacyView.safeAreaLayoutGuide.bottomAnchor, paddingLeft: 10, paddingBottom: 10, minHeight: 40)
  595. containerButton.rightAnchor.constraint(lessThanOrEqualTo: privacyView.safeAreaLayoutGuide.rightAnchor, constant: -10).isActive = true
  596. containerButton.isUserInteractionEnabled = true
  597. let tapgestureAgree = UITapGestureRecognizer(target: self, action: #selector(tappedOnAgreePrivacy(_ :)))
  598. tapgestureAgree.numberOfTapsRequired = 1
  599. containerButton.addGestureRecognizer(tapgestureAgree)
  600. let imageAgree = UIImageView()
  601. imageAgree.image = UIImage(systemName: "arrow.forward.circle")
  602. imageAgree.tintColor = .black
  603. containerButton.addSubview(imageAgree)
  604. imageAgree.anchor(left: containerButton.leftAnchor, centerY: containerButton.centerYAnchor, width: 40, height: 40)
  605. let titleAgree = UILabel()
  606. titleAgree.text = "Agree and Continue".localized()
  607. titleAgree.textColor = .black
  608. containerButton.addSubview(titleAgree)
  609. titleAgree.anchor(left: imageAgree.rightAnchor, right: containerButton.rightAnchor, paddingLeft: 5, centerY: containerButton.centerYAnchor)
  610. privacyWV.isOpaque = false
  611. privacyWV.backgroundColor = UIColor.clear
  612. privacyWV.scrollView.backgroundColor = UIColor.clear
  613. //print("URL: \(PrefsUtil.getURLPrivacyPolicy())")
  614. let lang = UserDefaults.standard.string(forKey: "i18n_language")
  615. var urlPrivacyPolicy = PrefsUtil.getURLPrivacyPolicy()
  616. if lang == "id" {
  617. urlPrivacyPolicy = urlPrivacyPolicy.replacingOccurrences(of: "/en/", with: "/id/")
  618. }
  619. privacyWV.load(URLRequest(url: URL(string: urlPrivacyPolicy)!))
  620. privacyView.addSubview(privacyWV)
  621. privacyWV.navigationDelegate = self
  622. privacyWV.anchor(top: privacyView.safeAreaLayoutGuide.topAnchor, left: privacyView.leftAnchor, right: privacyView.rightAnchor, height: privacyView.bounds.height - 80)
  623. }
  624. privacyPolicyVC.modalPresentationStyle = .fullScreen
  625. privacyPolicyVC.modalTransitionStyle = .crossDissolve
  626. self.present(privacyPolicyVC, animated: true)
  627. }
  628. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  629. let js = "(function() { document.body.style.background='transparent'; })();"
  630. privacyWV.evaluateJavaScript(js) { (_, error) in
  631. //print(error as Any)
  632. }
  633. }
  634. func showWelocomeView() {
  635. if let viewWelcome = welcomeVC.view {
  636. let bgImage = UIImageView()
  637. viewWelcome.addSubview(bgImage)
  638. bgImage.anchor(top: viewWelcome.topAnchor, left: viewWelcome.leftAnchor, bottom: viewWelcome.bottomAnchor, right: viewWelcome.rightAnchor)
  639. bgImage.backgroundColor = .white
  640. DispatchQueue.global().async {
  641. if let listBg = PrefsUtil.getBackground() {
  642. if listBg.isEmpty {
  643. return
  644. }
  645. var bgChoosen = ""
  646. let arrayBg = listBg.split(separator: ",")
  647. bgChoosen = String(arrayBg[Int.random(in: 0..<arrayBg.count)])
  648. ViewController.getDataImageFromUrl(from: URL(string: PrefsUtil.getURLBase() + "/dashboardv2/uploads/background/" + bgChoosen)!) { data, response, error in
  649. guard let data = data, error == nil else { return }
  650. // always update the UI from the main thread
  651. DispatchQueue.main.async() {
  652. bgImage.image = UIImage(data: data)!
  653. }
  654. }
  655. }
  656. }
  657. let welcomeTitle = UILabel()
  658. welcomeTitle.text = "Welcome to".localized() + " " + (Bundle.main.displayName ?? "")
  659. welcomeTitle.font = .systemFont(ofSize: 25, weight: .bold)
  660. welcomeTitle.numberOfLines = 0
  661. viewWelcome.addSubview(welcomeTitle)
  662. welcomeTitle.anchor(top: viewWelcome.safeAreaLayoutGuide.topAnchor, left: viewWelcome.safeAreaLayoutGuide.leftAnchor, right: viewWelcome.safeAreaLayoutGuide.rightAnchor, paddingTop: 10, paddingLeft: 10, paddingRight: 10)
  663. let logoImage = UIImageView()
  664. logoImage.image = UIImage(named: getHighResolutionAppIconName() ?? "")
  665. viewWelcome.addSubview(logoImage)
  666. logoImage.anchor(centerX: viewWelcome.centerXAnchor, centerY: viewWelcome.centerYAnchor, width: 200, height: 200)
  667. let containerButton = UIView()
  668. viewWelcome.addSubview(containerButton)
  669. containerButton.anchor(left: viewWelcome.safeAreaLayoutGuide.leftAnchor, bottom: viewWelcome.safeAreaLayoutGuide.bottomAnchor, paddingLeft: 10, paddingBottom: 10, minHeight: 40)
  670. containerButton.rightAnchor.constraint(lessThanOrEqualTo: viewWelcome.safeAreaLayoutGuide.rightAnchor, constant: -10).isActive = true
  671. containerButton.isUserInteractionEnabled = true
  672. let tapgestureAgree = UITapGestureRecognizer(target: self, action: #selector(tappedOnAgree(_ :)))
  673. tapgestureAgree.numberOfTapsRequired = 1
  674. containerButton.addGestureRecognizer(tapgestureAgree)
  675. let imageAgree = UIImageView()
  676. imageAgree.image = UIImage(systemName: "arrow.forward.circle")
  677. imageAgree.tintColor = .black
  678. containerButton.addSubview(imageAgree)
  679. imageAgree.anchor(left: containerButton.leftAnchor, centerY: containerButton.centerYAnchor, width: 40, height: 40)
  680. let titleAgree = UILabel()
  681. titleAgree.text = "Agree and Continue".localized()
  682. titleAgree.textColor = .black
  683. containerButton.addSubview(titleAgree)
  684. titleAgree.anchor(left: imageAgree.rightAnchor, right: containerButton.rightAnchor, paddingLeft: 5, centerY: containerButton.centerYAnchor)
  685. let formattedText = String.format(strings: [term], inString: termText)
  686. welcomeDesc.isUserInteractionEnabled = true
  687. welcomeDesc.attributedText = formattedText
  688. welcomeDesc.numberOfLines = 0
  689. viewWelcome.addSubview(welcomeDesc)
  690. welcomeDesc.anchor(left: viewWelcome.safeAreaLayoutGuide.leftAnchor, bottom: containerButton.topAnchor, right: viewWelcome.rightAnchor, paddingLeft: 10, paddingBottom: 10, paddingRight: 10)
  691. let tapgesture = UITapGestureRecognizer(target: self, action: #selector(tappedOnLabelTerms(_ :)))
  692. tapgesture.numberOfTapsRequired = 1
  693. welcomeDesc.addGestureRecognizer(tapgesture)
  694. }
  695. welcomeVC.modalPresentationStyle = .fullScreen
  696. welcomeVC.modalTransitionStyle = .crossDissolve
  697. self.present(welcomeVC, animated: true)
  698. }
  699. func getHighResolutionAppIconName() -> String? {
  700. guard let infoPlist = Bundle.main.infoDictionary else { return nil }
  701. guard let bundleIcons = infoPlist["CFBundleIcons"] as? NSDictionary else { return nil }
  702. guard let bundlePrimaryIcon = bundleIcons["CFBundlePrimaryIcon"] as? NSDictionary else { return nil }
  703. guard let bundleIconFiles = bundlePrimaryIcon["CFBundleIconFiles"] as? NSArray else { return nil }
  704. guard let appIcon = bundleIconFiles.lastObject as? String else { return nil }
  705. return appIcon
  706. }
  707. func showWebviewTerm() {
  708. termVC = UIViewController()
  709. if let viewTerm = termVC!.view {
  710. let webView = WKWebView()
  711. let lang = UserDefaults.standard.string(forKey: "i18n_language")
  712. var urlTerm = "https://newuniverse.io/newuniverse-tos"
  713. if lang == "id" {
  714. urlTerm = "https://newuniverse.io/newuniverse-tos-id"
  715. }
  716. let url = URL (string: urlTerm)
  717. let requestObj = URLRequest(url: url!)
  718. webView.load(requestObj)
  719. viewTerm.addSubview(webView)
  720. webView.anchor(top: viewTerm.safeAreaLayoutGuide.topAnchor, left: viewTerm.safeAreaLayoutGuide.leftAnchor, bottom: viewTerm.safeAreaLayoutGuide.bottomAnchor, right: viewTerm.safeAreaLayoutGuide.rightAnchor)
  721. }
  722. let navigationController = UINavigationController(rootViewController: termVC!)
  723. navigationController.navigationBar.tintColor = .mainColor
  724. navigationController.navigationBar.barTintColor = .white
  725. navigationController.navigationBar.isTranslucent = false
  726. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.mainColor]
  727. navigationController.navigationBar.titleTextAttributes = textAttributes
  728. navigationController.view.backgroundColor = .white
  729. termVC!.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close".localized(), style: .plain, target: self, action: #selector(closeTerm))
  730. welcomeVC.present(navigationController, animated: true)
  731. }
  732. @objc func closeTerm() {
  733. termVC!.dismiss(animated: true)
  734. }
  735. @objc func tappedOnLabelTerms(_ gesture: UITapGestureRecognizer) {
  736. let termString = termText as NSString
  737. let termRange = termString.range(of: term)
  738. let tapLocation = gesture.location(in: welcomeDesc)
  739. let index = welcomeDesc.indexOfAttributedTextCharacterAtPoint(point: tapLocation)
  740. if checkRange(termRange, contain: index) == true {
  741. showWebviewTerm()
  742. return
  743. }
  744. }
  745. @objc func tappedOnAgree(_ gesture: UITapGestureRecognizer) {
  746. PrefsUtil.setTerms(value: true)
  747. welcomeVC.dismiss(animated: true)
  748. }
  749. @objc func tappedOnAgreePrivacy(_ gesture: UITapGestureRecognizer) {
  750. PrefsUtil.setAgreePrivacyPolicy(value: true)
  751. privacyPolicyVC.dismiss(animated: true)
  752. }
  753. func checkRange(_ range: NSRange, contain index: Int) -> Bool {
  754. return index > range.location && index < range.location + range.length
  755. }
  756. @objc func middleBtnTapped() {
  757. ViewController.expandButton()
  758. }
  759. public func show(b: Bool) {
  760. if(!b){
  761. if(ViewController.isExpandButton){
  762. ViewController.expandButton()
  763. }
  764. }
  765. }
  766. @objc func ccTap() {
  767. //print("ccTap")
  768. if(ViewController.checkIsChangePerson()){
  769. show(b: false)
  770. ViewController.def?.thirdTab?.webView?.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  771. ViewController.def?.firstTab?.webView?.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  772. if ViewController.listPullFB.count != 0 && ViewController.listPullFB.count > 2 {
  773. let package_id = ViewController.listPullFB[2].split(separator: "|")[0]
  774. var app_id = ""
  775. if ViewController.listPullFB[2].split(separator: "|").count > 1 {
  776. app_id = String(ViewController.listPullFB[2].split(separator: "|")[1])
  777. }
  778. if package_id.contains("_fb") {
  779. let indexTap = Int(String(package_id.split(separator: "_")[1]).substring(from: 2, to: 2))!
  780. if indexTap == 99 {
  781. openTabPost()
  782. } else {
  783. if indexTap == 2 {
  784. let url = package_id.components(separatedBy: "_")[2]
  785. Nexilis.buttonClicked(index: indexTap, id: url)
  786. } else {
  787. Nexilis.buttonClicked(index: indexTap)
  788. }
  789. }
  790. } else {
  791. if !app_id.isEmpty {
  792. Nexilis.buttonClicked(index: 0, id: app_id)
  793. }
  794. }
  795. } else {
  796. Nexilis.buttonClicked(index: 9)
  797. }
  798. }
  799. }
  800. @objc func streamTap() {
  801. //print("streamTap")
  802. if(ViewController.checkIsChangePerson()){
  803. show(b: false)
  804. ViewController.def?.thirdTab?.webView?.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  805. ViewController.def?.firstTab?.webView?.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  806. if ViewController.listPullFB.count != 0 && ViewController.listPullFB.count > 4 {
  807. let package_id = ViewController.listPullFB[4].split(separator: "|")[0]
  808. var app_id = ""
  809. if ViewController.listPullFB[4].split(separator: "|").count > 1 {
  810. app_id = String(ViewController.listPullFB[4].split(separator: "|")[1])
  811. }
  812. if package_id.contains("_fb") {
  813. let indexTap = Int(String(package_id.split(separator: "_")[1]).substring(from: 2, to: 2))!
  814. if indexTap == 99 {
  815. openTabPost()
  816. } else {
  817. if indexTap == 2 {
  818. let url = package_id.components(separatedBy: "_")[2]
  819. Nexilis.buttonClicked(index: indexTap, id: url)
  820. } else {
  821. Nexilis.buttonClicked(index: indexTap)
  822. }
  823. }
  824. } else {
  825. if !app_id.isEmpty {
  826. Nexilis.buttonClicked(index: 0, id: app_id)
  827. }
  828. }
  829. } else {
  830. Nexilis.buttonClicked(index: 8)
  831. }
  832. }
  833. }
  834. @objc func callTap() {
  835. //print("callTap")
  836. if(ViewController.checkIsChangePerson()){
  837. show(b: false)
  838. ViewController.def?.thirdTab?.webView?.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  839. ViewController.def?.firstTab?.webView?.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  840. if ViewController.listPullFB.count != 0 && ViewController.listPullFB.count > 1 {
  841. let package_id = ViewController.listPullFB[1].split(separator: "|")[0]
  842. var app_id = ""
  843. if ViewController.listPullFB[1].split(separator: "|").count > 1 {
  844. app_id = String(ViewController.listPullFB[1].split(separator: "|")[1])
  845. }
  846. if package_id.contains("_fb") {
  847. let indexTap = Int(String(package_id.split(separator: "_")[1]).substring(from: 2, to: 2))!
  848. if indexTap == 99 {
  849. openTabPost()
  850. } else {
  851. if indexTap == 2 {
  852. let url = package_id.components(separatedBy: "_")[2]
  853. Nexilis.buttonClicked(index: indexTap, id: url)
  854. } else {
  855. Nexilis.buttonClicked(index: indexTap)
  856. }
  857. }
  858. } else {
  859. if !app_id.isEmpty {
  860. Nexilis.buttonClicked(index: 0, id: app_id)
  861. }
  862. }
  863. } else {
  864. Nexilis.buttonClicked(index: 7)
  865. }
  866. }
  867. }
  868. @objc func chatTap() {
  869. //print("chatTap")
  870. // APIS.openWhiteboard()
  871. // return
  872. if(ViewController.checkIsChangePerson()){
  873. show(b: false)
  874. ViewController.def?.thirdTab?.webView?.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  875. ViewController.def?.firstTab?.webView?.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  876. if ViewController.listPullFB.count != 0 && ViewController.listPullFB.count > 0 {
  877. let package_id = ViewController.listPullFB[0].split(separator: "|")[0]
  878. var app_id = ""
  879. if ViewController.listPullFB[0].split(separator: "|").count > 1 {
  880. app_id = String(ViewController.listPullFB[0].split(separator: "|")[1])
  881. }
  882. if package_id.contains("_fb") {
  883. let indexTap = Int(String(package_id.split(separator: "_")[1]).substring(from: 2, to: 2))!
  884. if indexTap == 99 {
  885. openTabPost()
  886. } else {
  887. if indexTap == 2 {
  888. let url = package_id.components(separatedBy: "_")[2]
  889. Nexilis.buttonClicked(index: indexTap, id: url)
  890. } else {
  891. Nexilis.buttonClicked(index: indexTap)
  892. }
  893. }
  894. } else {
  895. if !app_id.isEmpty {
  896. Nexilis.buttonClicked(index: 0, id: app_id)
  897. }
  898. }
  899. } else {
  900. Nexilis.buttonClicked(index: 6)
  901. }
  902. }
  903. }
  904. @objc func postTap() {
  905. if(ViewController.checkIsChangePerson()){
  906. show(b: false)
  907. ViewController.def?.thirdTab?.webView?.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  908. ViewController.def?.firstTab?.webView?.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  909. if ViewController.listPullFB.count != 0 && ViewController.listPullFB.count > 3 {
  910. let package_id = ViewController.listPullFB[3].split(separator: "|")[0]
  911. var app_id = ""
  912. if ViewController.listPullFB[3].split(separator: "|").count > 1 {
  913. app_id = String(ViewController.listPullFB[3].split(separator: "|")[1])
  914. }
  915. if package_id.contains("_fb") {
  916. let indexTap = Int(String(package_id.split(separator: "_")[1]).substring(from: 2, to: 2))!
  917. if indexTap == 99 {
  918. openTabPost()
  919. } else {
  920. if indexTap == 2 {
  921. let url = package_id.components(separatedBy: "_")[2]
  922. Nexilis.buttonClicked(index: indexTap, id: url)
  923. } else {
  924. Nexilis.buttonClicked(index: indexTap)
  925. }
  926. }
  927. } else {
  928. if !app_id.isEmpty {
  929. Nexilis.buttonClicked(index: 0, id: app_id)
  930. }
  931. }
  932. } else {
  933. openTabPost()
  934. }
  935. }
  936. }
  937. func openTabPost() {
  938. let customTab = PrefsUtil.getCustomTab().split(separator: ",")
  939. let cpaasMode = PrefsUtil.getCpaasMode()
  940. var i = 0
  941. var j = 0
  942. //print("custom tab post tap = \(customTab)")
  943. while j < customTab.count {
  944. if(((i == 1 && customTab.count == 3) || i == 2) &&
  945. (cpaasMode == PrefsUtil.CPAAS_MODE_DOCKED || cpaasMode == PrefsUtil.CPAAS_MODE_MIX)){
  946. }
  947. else {
  948. if customTab[j] == "3" {
  949. break
  950. }
  951. j += 1
  952. }
  953. i += 1
  954. }
  955. if(j < customTab.count){
  956. self.selectedIndex = i
  957. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(1), execute: {
  958. let me = UserDefaults.standard.string(forKey: "me")
  959. let address = "\(PrefsUtil.getURLBase())nexilis/pages/tab5-new-post?f_pin=\(me ?? "")"
  960. self.thirdTab?.webView.evaluateJavaScript("{window.localStorage.setItem('currentTab','\(ViewController.tab3)')}")
  961. self.thirdTab?.webView.evaluateJavaScript("window.location = '\(address)'")
  962. })
  963. }
  964. }
  965. static func pullActionButton() {
  966. if datePullFB == nil || Int(Date().timeIntervalSince(datePullFB!)) >= 60 {
  967. datePullFB = Date()
  968. DispatchQueue.global().async {
  969. if !PrefsUtil.getCustomButtons().isEmpty {
  970. DispatchQueue.main.async { [self] in
  971. let customButtons = PrefsUtil.getCustomButtons().components(separatedBy: ",")
  972. let customIcons = PrefsUtil.getCustomFBIcon().components(separatedBy: ",")
  973. for i in 0..<customButtons.count {
  974. let package_id = customButtons[i]
  975. let app_id = ""
  976. let icon = customIcons[i]
  977. listPullFB.append("\(package_id)|\(app_id)")
  978. DispatchQueue.global().async {
  979. ViewController.getDataImageFromUrl(from: URL(string: "https://nexilis.io/dashboardv2/uploads/fb_icon/\(icon)")!) { data, response, error in
  980. guard let data = data, error == nil else { return }
  981. // always update the UI from the main thread
  982. DispatchQueue.main.async() {
  983. if i == 0 {
  984. ViewController.chatButton.setBackgroundImage(UIImage(data: data), for: .normal)
  985. } else if i == 1 {
  986. ViewController.callButton.setBackgroundImage(UIImage(data: data), for: .normal)
  987. } else if i == 2 {
  988. ViewController.ccButton.setBackgroundImage(UIImage(data: data), for: .normal)
  989. } else if i == 3 {
  990. ViewController.postButton.setBackgroundImage(UIImage(data: data), for: .normal)
  991. } else if i == 4 {
  992. ViewController.streamingButton.setBackgroundImage(UIImage(data: data), for: .normal)
  993. }
  994. }
  995. }
  996. }
  997. }
  998. }
  999. return
  1000. }
  1001. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.pullFloatingButton(), timeout: 30 * 1000) {
  1002. if response.isOk() {
  1003. let data = response.getBody(key: CoreMessage_TMessageKey.DATA, default_value: "")
  1004. if !data.isEmpty {
  1005. if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions()) as? [AnyObject] {
  1006. DispatchQueue.main.async { [self] in
  1007. listPullFB.removeAll()
  1008. if jsonArray.count != 0 {
  1009. var count = 0
  1010. for json in jsonArray {
  1011. let package_id = json["package_act"] as! String
  1012. let app_id = (json["app_id"] as? String) ?? ""
  1013. let icon = (json["icon"] as? String) ?? ""
  1014. listPullFB.append("\(package_id)|\(app_id)")
  1015. if count == 0 {
  1016. if !icon.isEmpty {
  1017. DispatchQueue.global().async {
  1018. ViewController.getDataImageFromUrl(from: URL(string: "https://newuniverse.io/get_file?account=\(Nexilis.sAPIKey)&image=\(icon)")!) { data, response, error in
  1019. guard let data = data, error == nil else { return }
  1020. // always update the UI from the main thread
  1021. DispatchQueue.main.async() {
  1022. ViewController.chatButton.setBackgroundImage(UIImage(data: data), for: .normal)
  1023. }
  1024. }
  1025. }
  1026. } else {
  1027. ViewController.chatButton.setBackgroundImage(UIImage(named: "pb_button_chat", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal), for: .normal)
  1028. }
  1029. } else if count == 1 {
  1030. if !icon.isEmpty {
  1031. DispatchQueue.global().async {
  1032. ViewController.getDataImageFromUrl(from: URL(string: "https://newuniverse.io/get_file?account=\(Nexilis.sAPIKey)&image=\(icon)")!) { data, response, error in
  1033. guard let data = data, error == nil else { return }
  1034. // always update the UI from the main thread
  1035. DispatchQueue.main.async() {
  1036. ViewController.callButton.setBackgroundImage(UIImage(data: data), for: .normal)
  1037. }
  1038. }
  1039. }
  1040. } else {
  1041. ViewController.callButton.setBackgroundImage(UIImage(named: "pb_button_chat", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal), for: .normal)
  1042. }
  1043. } else if count == 2 {
  1044. if !icon.isEmpty {
  1045. DispatchQueue.global().async {
  1046. ViewController.getDataImageFromUrl(from: URL(string: "https://newuniverse.io/get_file?account=\(Nexilis.sAPIKey)&image=\(icon)")!) { data, response, error in
  1047. guard let data = data, error == nil else { return }
  1048. // always update the UI from the main thread
  1049. DispatchQueue.main.async() {
  1050. ViewController.ccButton.setBackgroundImage(UIImage(data: data), for: .normal)
  1051. }
  1052. }
  1053. }
  1054. } else {
  1055. ViewController.ccButton.setBackgroundImage(UIImage(named: "pb_button_chat", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal), for: .normal)
  1056. }
  1057. } else if count == 3 {
  1058. if !icon.isEmpty {
  1059. DispatchQueue.global().async {
  1060. ViewController.getDataImageFromUrl(from: URL(string: "https://newuniverse.io/get_file?account=\(Nexilis.sAPIKey)&image=\(icon)")!) { data, response, error in
  1061. guard let data = data, error == nil else { return }
  1062. // always update the UI from the main thread
  1063. DispatchQueue.main.async() {
  1064. ViewController.postButton.setBackgroundImage(UIImage(data: data), for: .normal)
  1065. }
  1066. }
  1067. }
  1068. } else {
  1069. ViewController.postButton.setBackgroundImage(UIImage(named: "pb_button_chat", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal), for: .normal)
  1070. }
  1071. } else if count == 4 {
  1072. if !icon.isEmpty {
  1073. DispatchQueue.global().async {
  1074. ViewController.getDataImageFromUrl(from: URL(string: "https://newuniverse.io/get_file?account=\(Nexilis.sAPIKey)&image=\(icon)")!) { data, response, error in
  1075. guard let data = data, error == nil else { return }
  1076. // always update the UI from the main thread
  1077. DispatchQueue.main.async() {
  1078. ViewController.streamingButton.setBackgroundImage(UIImage(data: data), for: .normal)
  1079. }
  1080. }
  1081. }
  1082. } else {
  1083. ViewController.streamingButton.setBackgroundImage(UIImage(named: "pb_button_chat", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal), for: .normal)
  1084. }
  1085. }
  1086. count += 1
  1087. }
  1088. }
  1089. }
  1090. }
  1091. }
  1092. }
  1093. }
  1094. }
  1095. }
  1096. }
  1097. public static func showDockedButton() {
  1098. ViewController.chatButton.isHidden = false
  1099. ViewController.callButton.isHidden = false
  1100. ViewController.ccButton.isHidden = false
  1101. ViewController.streamingButton.isHidden = false
  1102. ViewController.postButton.isHidden = false
  1103. }
  1104. public static func hideDockedButton() {
  1105. ViewController.chatButton.isHidden = true
  1106. ViewController.callButton.isHidden = true
  1107. ViewController.ccButton.isHidden = true
  1108. ViewController.streamingButton.isHidden = true
  1109. ViewController.postButton.isHidden = true
  1110. }
  1111. public static func expandButton() {
  1112. let cpaasMode = PrefsUtil.getCpaasMode()
  1113. if cpaasMode != PrefsUtil.CPAAS_MODE_DOCKED && cpaasMode != PrefsUtil.CPAAS_MODE_MIX {
  1114. return
  1115. }
  1116. if ViewController.alwaysHideButton && !ViewController.isExpandButton {
  1117. return
  1118. }
  1119. var minYIpX: CGFloat = 0
  1120. let iPhoneModel = getiPhoneModel()
  1121. if iPhoneModel == "iPhone X or newer" {
  1122. minYIpX += 20
  1123. }
  1124. if ViewController.isExpandButton {
  1125. ViewController.isExpandButton = false
  1126. let xChatPosition = ViewController.chatButton.frame.origin.x + 90
  1127. let yChatPosition = ViewController.chatButton.frame.origin.y + 20
  1128. let xCallPosition = ViewController.callButton.frame.origin.x + 55
  1129. let yCallPosition = ViewController.callButton.frame.origin.y + 70
  1130. let xCCPosition = ViewController.ccButton.frame.origin.x
  1131. let yCCPosition = ViewController.ccButton.frame.origin.y + 90
  1132. let xPostPosition = ViewController.postButton.frame.origin.x - 55
  1133. let yPostPosition = ViewController.postButton.frame.origin.y + 70
  1134. let xStreamingPosition = ViewController.streamingButton.frame.origin.x - 90
  1135. let yStreamingPosition = ViewController.streamingButton.frame.origin.y + 20
  1136. UIView.animate(withDuration: 0.5, animations: {
  1137. // if !ViewController.isBlue {
  1138. // ViewController.middleButton.transform = CGAffineTransform(rotationAngle: 0)
  1139. // }
  1140. ViewController.chatButton.frame.origin = CGPoint(x: xChatPosition, y: yChatPosition + minYIpX)
  1141. ViewController.callButton.frame.origin = CGPoint(x: xCallPosition, y: yCallPosition + minYIpX)
  1142. ViewController.ccButton.frame.origin = CGPoint(x: xCCPosition, y: yCCPosition + minYIpX)
  1143. ViewController.streamingButton.frame.origin = CGPoint(x: xStreamingPosition, y: yStreamingPosition + minYIpX)
  1144. ViewController.postButton.frame.origin = CGPoint(x: xPostPosition, y: yPostPosition + minYIpX)
  1145. })
  1146. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
  1147. if !ViewController.isExpandButton {
  1148. ViewController.hideDockedButton()
  1149. }
  1150. })
  1151. } else {
  1152. ViewController.isExpandButton = true
  1153. ViewController.showDockedButton()
  1154. let xChatPosition = ViewController.chatButton.frame.origin.x - 90
  1155. let yChatPosition = ViewController.chatButton.frame.origin.y - 20
  1156. let xCallPosition = ViewController.callButton.frame.origin.x - 55
  1157. let yCallPosition = ViewController.callButton.frame.origin.y - 70
  1158. let xCCPosition = ViewController.ccButton.frame.origin.x
  1159. let yCCPosition = ViewController.ccButton.frame.origin.y - 90
  1160. let xPostPosition = ViewController.postButton.frame.origin.x + 55
  1161. let yPostPosition = ViewController.postButton.frame.origin.y - 70
  1162. let xStreamingPosition = ViewController.streamingButton.frame.origin.x + 90
  1163. let yStreamingPosition = ViewController.streamingButton.frame.origin.y - 20
  1164. UIView.animate(withDuration: 0.5, animations: {
  1165. // if !ViewController.isBlue{
  1166. // ViewController.middleButton.transform = CGAffineTransform(rotationAngle: .pi / 2)
  1167. // }
  1168. ViewController.chatButton.frame.origin = CGPoint(x: xChatPosition, y: yChatPosition - minYIpX)
  1169. ViewController.callButton.frame.origin = CGPoint(x: xCallPosition, y: yCallPosition - minYIpX)
  1170. ViewController.ccButton.frame.origin = CGPoint(x: xCCPosition, y: yCCPosition - minYIpX)
  1171. ViewController.streamingButton.frame.origin = CGPoint(x: xStreamingPosition, y: yStreamingPosition - minYIpX)
  1172. ViewController.postButton.frame.origin = CGPoint(x: xPostPosition, y: yPostPosition - minYIpX)
  1173. })
  1174. }
  1175. }
  1176. public static func removeMiddleButton() {
  1177. // ViewController.chatButton.removeFromSuperview()
  1178. // ViewController.callButton.removeFromSuperview()
  1179. // ViewController.ccButton.removeFromSuperview()
  1180. // ViewController.streamingButton.removeFromSuperview()
  1181. // ViewController.postButton.removeFromSuperview()
  1182. ViewController.middleButton.isHidden = true
  1183. }
  1184. public static func getDataImageFromUrl(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
  1185. URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
  1186. }
  1187. }
  1188. class EmptyTabViewController: UIViewController {
  1189. override func viewDidLoad() {
  1190. }
  1191. }
  1192. extension Bundle {
  1193. // Name of the app - title under the icon.
  1194. var displayName: String? {
  1195. return object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ??
  1196. object(forInfoDictionaryKey: "CFBundleName") as? String
  1197. }
  1198. }
  1199. extension BinaryInteger {
  1200. var degreesToRadians: CGFloat { CGFloat(self) * .pi / 180 }
  1201. }
  1202. extension FloatingPoint {
  1203. var degreesToRadians: Self { self * .pi / 180 }
  1204. var radiansToDegrees: Self { self * 180 / .pi }
  1205. }
  1206. extension UIImage {
  1207. class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
  1208. let rect: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  1209. UIGraphicsBeginImageContextWithOptions(size, false, 0)
  1210. color.setFill()
  1211. UIRectFill(rect)
  1212. let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
  1213. UIGraphicsEndImageContext()
  1214. return image
  1215. }
  1216. }
  1217. extension String {
  1218. static func format(strings: [String],
  1219. italicFont: UIFont = UIFont.italicSystemFont(ofSize: 12),
  1220. italicColor: UIColor = UIColor.systemGreen,
  1221. inString string: String,
  1222. font: UIFont = UIFont.systemFont(ofSize: 12),
  1223. color: UIColor = UIColor.black) -> NSAttributedString {
  1224. let attributedString =
  1225. NSMutableAttributedString(string: string,
  1226. attributes: [
  1227. NSAttributedString.Key.font: font,
  1228. NSAttributedString.Key.foregroundColor: color])
  1229. let italicFontAttribute = [NSAttributedString.Key.font: italicFont, NSAttributedString.Key.foregroundColor: italicColor]
  1230. for italic in strings {
  1231. attributedString.addAttributes(italicFontAttribute, range: (string as NSString).range(of: italic))
  1232. }
  1233. return attributedString
  1234. }
  1235. }
  1236. extension UILabel {
  1237. func indexOfAttributedTextCharacterAtPoint(point: CGPoint) -> Int {
  1238. assert(self.attributedText != nil, "This method is developed for attributed string")
  1239. let textStorage = NSTextStorage(attributedString: self.attributedText!)
  1240. let layoutManager = NSLayoutManager()
  1241. textStorage.addLayoutManager(layoutManager)
  1242. let textContainer = NSTextContainer(size: self.frame.size)
  1243. textContainer.lineFragmentPadding = 0
  1244. textContainer.maximumNumberOfLines = self.numberOfLines
  1245. textContainer.lineBreakMode = self.lineBreakMode
  1246. layoutManager.addTextContainer(textContainer)
  1247. let index = layoutManager.characterIndex(for: point, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
  1248. return index
  1249. }
  1250. }