ViewController.swift 63 KB

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