ViewController.swift 70 KB

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