ViewController.swift 84 KB

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