AppDelegate.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // AppDelegate.swift
  3. // TestQmeraLite
  4. //
  5. // Created by Qindi on 29/11/21.
  6. //
  7. import UIKit
  8. import NexilisLite
  9. import NotificationBannerSwift
  10. struct AppFontName {
  11. static let regular = "Poppins-Regular"
  12. static let bold = "Poppins-SemiBold"
  13. static let italic = "Poppins-MediumItalic"
  14. }
  15. extension UIFontDescriptor.AttributeName {
  16. static let nsctFontUIUsage = UIFontDescriptor.AttributeName(rawValue: "NSCTFontUIUsageAttribute")
  17. }
  18. extension UIFont {
  19. static var isOverrided: Bool = false
  20. static let FONT_SELECT = 0
  21. @objc class func mySystemFont(ofSize size: CGFloat) -> UIFont {
  22. return UIFont(name: AppFontName.regular, size: size)!
  23. }
  24. @objc class func myBoldSystemFont(ofSize size: CGFloat) -> UIFont {
  25. return UIFont(name: AppFontName.bold, size: size)!
  26. }
  27. @objc class func myItalicSystemFont(ofSize size: CGFloat) -> UIFont {
  28. return UIFont(name: AppFontName.italic, size: size)!
  29. }
  30. @objc convenience init(myCoder aDecoder: NSCoder) {
  31. guard
  32. let fontDescriptor = aDecoder.decodeObject(forKey: "UIFontDescriptor") as? UIFontDescriptor,
  33. let fontAttribute = fontDescriptor.fontAttributes[.nsctFontUIUsage] as? String else {
  34. self.init(myCoder: aDecoder)
  35. return
  36. }
  37. var fontName = ""
  38. switch fontAttribute {
  39. case "CTFontRegularUsage":
  40. fontName = AppFontName.regular
  41. case "CTFontEmphasizedUsage", "CTFontBoldUsage":
  42. fontName = AppFontName.bold
  43. case "CTFontObliqueUsage":
  44. fontName = AppFontName.italic
  45. default:
  46. fontName = AppFontName.regular
  47. }
  48. self.init(name: fontName, size: fontDescriptor.pointSize)!
  49. }
  50. class func overrideInitialize() {
  51. guard self == UIFont.self, !isOverrided, FONT_SELECT == 0 else { return }
  52. // Avoid method swizzling run twice and revert to original initialize function
  53. isOverrided = true
  54. if let systemFontMethod = class_getClassMethod(self, #selector(systemFont(ofSize:))),
  55. let mySystemFontMethod = class_getClassMethod(self, #selector(mySystemFont(ofSize:))) {
  56. method_exchangeImplementations(systemFontMethod, mySystemFontMethod)
  57. }
  58. if let boldSystemFontMethod = class_getClassMethod(self, #selector(boldSystemFont(ofSize:))),
  59. let myBoldSystemFontMethod = class_getClassMethod(self, #selector(myBoldSystemFont(ofSize:))) {
  60. method_exchangeImplementations(boldSystemFontMethod, myBoldSystemFontMethod)
  61. }
  62. if let italicSystemFontMethod = class_getClassMethod(self, #selector(italicSystemFont(ofSize:))),
  63. let myItalicSystemFontMethod = class_getClassMethod(self, #selector(myItalicSystemFont(ofSize:))) {
  64. method_exchangeImplementations(italicSystemFontMethod, myItalicSystemFontMethod)
  65. }
  66. if let initCoderMethod = class_getInstanceMethod(self, #selector(UIFontDescriptor.init(coder:))), // Trick to get over the lack of UIFont.init(coder:))
  67. let myInitCoderMethod = class_getInstanceMethod(self, #selector(UIFont.init(myCoder:))) {
  68. method_exchangeImplementations(initCoderMethod, myInitCoderMethod)
  69. }
  70. }
  71. }
  72. @main
  73. class AppDelegate: UIResponder, UIApplicationDelegate {
  74. override init() {
  75. super.init()
  76. UIFont.overrideInitialize()
  77. }
  78. func transparentNavbar(){
  79. // Sets background to a blank/empty image
  80. UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
  81. // Sets shadow (line below the bar) to a blank image
  82. UINavigationBar.appearance().shadowImage = UIImage()
  83. // Sets the translucent background color
  84. UINavigationBar.appearance().backgroundColor = .clear
  85. // Set translucent. (Default value is already true, so this can be removed if desired.)
  86. UINavigationBar.appearance().isTranslucent = true
  87. }
  88. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  89. // Override point for customization after application launch.
  90. // Nexilis.connect(apiKey: "09ED34439CFE435E5D528E73F2D3DC1B23307B56EAB28A4883700D47BB5ADE07", delegate: self, showButton: false)
  91. // Nexilis.connect(apiKey: "E6FF534BDA45A78619488E44AC90592327DDF33ACF6213A58DB55B78300BA8EE", delegate: self, showButton: false)
  92. if !CheckConnection.isConnectedToNetwork() {
  93. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  94. imageView.tintColor = .white
  95. let banner = FloatingNotificationBanner(title: "Check your connection".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  96. banner.show()
  97. } else {
  98. var showButton = false
  99. if PrefsUtil.getCpaasMode() == PrefsUtil.CPAAS_MODE_FLOATING || PrefsUtil.getCpaasMode() == PrefsUtil.CPAAS_MODE_MIX {
  100. showButton = true
  101. }
  102. Nexilis.connect(apiKey: "***REPLACE***WITH***YOUR***ACCOUNT***", delegate: self, showButton: showButton, fromMAB: true) //AA6164AA13F0A6262677AC7443C37E3F6F0A342E8BEE84B700CB59C876166FE0(AWS) //36BCB99AA3885165E64ECC50591C4DA5072A25B2E7BAADA5A1B57E18737FF88C(CBN)
  103. if let response = Nexilis.writeSync(message: getPrefs(key: ""), timeout: 5000) {
  104. if response.mBodies[CoreMessage_TMessageKey.ERRCOD] == "00" {
  105. let data = response.getBody(key: CoreMessage_TMessageKey.DATA)
  106. if let json = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: []) as? [[String: Any?]] {
  107. UserDefaults.standard.removeObject(forKey: "app_builder_url_first_tab")
  108. UserDefaults.standard.removeObject(forKey: "app_builder_url_third_tab")
  109. UserDefaults.standard.removeObject(forKey: "app_builder_custom_tab")
  110. UserDefaults.standard.removeObject(forKey: "app_builder_url_base")
  111. UserDefaults.standard.removeObject(forKey: "app_builder_url_qms")
  112. UserDefaults.standard.removeObject(forKey: "app_builder_icon_dock")
  113. UserDefaults.standard.removeObject(forKey: "app_builder_icon_ss")
  114. UserDefaults.standard.removeObject(forKey: "app_builder_background")
  115. UserDefaults.standard.removeObject(forKey: "app_builder_compressor")
  116. for j in json {
  117. if let firstTab = j["app_builder_url_first_tab"] as? String {
  118. PrefsUtil.setURLFirstTab(value: firstTab)
  119. ViewController.sURL = firstTab
  120. }
  121. if let thirdTab = j["app_builder_url_third_tab"] as? String {
  122. PrefsUtil.setURLThirdTab(value: thirdTab)
  123. ViewController.tab3 = thirdTab
  124. }
  125. if let customTab = j["app_builder_custom_tab"] as? String {
  126. PrefsUtil.setCustomTab(cust: customTab)
  127. }
  128. if let urlBase = j["app_builder_url_base"] as? String {
  129. PrefsUtil.setURLBase(value: urlBase)
  130. }
  131. if let urlQMS = j["app_builder_url_qms"] as? String {
  132. PrefsUtil.setURLQMS(value: urlQMS)
  133. }
  134. if let iconDock = j["app_builder_icon_dock"] as? String {
  135. PrefsUtil.setIconDock(value: iconDock)
  136. }
  137. if let iconSS = j["app_builder_icon_ss"] as? String {
  138. PrefsUtil.setIconSS(value: iconSS)
  139. }
  140. if let background = j["app_builder_background"] as? String {
  141. PrefsUtil.setBackground(value: background)
  142. }
  143. if let background = j["app_builder_compressor"] as? String {
  144. PrefsUtil.setCompressor(value: background)
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }
  151. let tabBarAppearance: UITabBarAppearance = UITabBarAppearance()
  152. tabBarAppearance.configureWithTransparentBackground()
  153. if Bundle.main.displayName == "DigiNetS" {
  154. tabBarAppearance.backgroundColor = .black.withAlphaComponent(0.7)
  155. } else {
  156. tabBarAppearance.backgroundColor = .white.withAlphaComponent(0.9)
  157. }
  158. if #available(iOS 13.0, *) {
  159. UITabBar.appearance().standardAppearance = tabBarAppearance
  160. }
  161. if #available(iOS 15.0, *) {
  162. UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
  163. }
  164. return true
  165. }
  166. // MARK: UISceneSession Lifecycle
  167. func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  168. // Called when a new scene session is being created.
  169. // Use this method to select a configuration to create the new scene with.
  170. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  171. }
  172. func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  173. // Called when the user discards a scene session.
  174. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
  175. // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
  176. }
  177. func getPrefs(key: String) -> TMessage {
  178. let tMessage = NexilisLite.TMessage()
  179. let me = UserDefaults.standard.string(forKey: "me")
  180. tMessage.mCode = "PPR"
  181. tMessage.mStatus = CoreMessage_TMessageUtil.getTID()
  182. tMessage.mBodies[CoreMessage_TMessageKey.F_PIN] = me
  183. tMessage.mBodies[CoreMessage_TMessageKey.KEY] = key
  184. return tMessage
  185. }
  186. }
  187. extension AppDelegate: ConnectDelegate {
  188. func onSuccess(userId: String) {
  189. print(#function, "userId: \(userId)")
  190. }
  191. func onFailed(error: String) {
  192. print(#function, "error: \(error)")
  193. }
  194. }