ViewController.swift 66 KB

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