ViewController.swift 84 KB

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