ViewController.swift 61 KB

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