ViewController.swift 70 KB

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