AppDelegate.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. for j in json {
  108. if let firstTab = j["app_builder_url_first_tab"] as? String {
  109. PrefsUtil.setURLFirstTab(value: firstTab)
  110. ViewController.sURL = firstTab
  111. }
  112. if let thirdTab = j["app_builder_url_third_tab"] as? String {
  113. PrefsUtil.setURLThirdTab(value: thirdTab)
  114. ViewController.tab3 = thirdTab
  115. }
  116. if let customTab = j["app_builder_custom_tab"] as? String {
  117. PrefsUtil.setCustomTab(cust: customTab)
  118. }
  119. if let urlBase = j["app_builder_url_base"] as? String {
  120. PrefsUtil.setURLBase(value: urlBase)
  121. }
  122. if let urlQMS = j["app_builder_url_qms"] as? String {
  123. PrefsUtil.setURLQMS(value: urlQMS)
  124. }
  125. if let iconDock = j["app_builder_icon_dock"] as? String {
  126. PrefsUtil.setIconDock(value: iconDock)
  127. }
  128. if let iconSS = j["app_builder_icon_ss"] as? String {
  129. PrefsUtil.setIconSS(value: iconSS)
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. let tabBarAppearance: UITabBarAppearance = UITabBarAppearance()
  137. tabBarAppearance.configureWithDefaultBackground()
  138. if Bundle.main.displayName == "DigiNetS" {
  139. tabBarAppearance.backgroundColor = .black.withAlphaComponent(0.7)
  140. }
  141. if #available(iOS 13.0, *) {
  142. UITabBar.appearance().standardAppearance = tabBarAppearance
  143. }
  144. if #available(iOS 15.0, *) {
  145. UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
  146. }
  147. return true
  148. }
  149. // MARK: UISceneSession Lifecycle
  150. func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  151. // Called when a new scene session is being created.
  152. // Use this method to select a configuration to create the new scene with.
  153. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  154. }
  155. func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  156. // Called when the user discards a scene session.
  157. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
  158. // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
  159. }
  160. func getPrefs(key: String) -> TMessage {
  161. let tMessage = NexilisLite.TMessage()
  162. let me = UserDefaults.standard.string(forKey: "me")
  163. tMessage.mCode = "PPR"
  164. tMessage.mStatus = CoreMessage_TMessageUtil.getTID()
  165. tMessage.mBodies[CoreMessage_TMessageKey.F_PIN] = me
  166. tMessage.mBodies[CoreMessage_TMessageKey.KEY] = key
  167. return tMessage
  168. }
  169. }
  170. extension AppDelegate: ConnectDelegate {
  171. func onSuccess(userId: String) {
  172. print(#function, "userId: \(userId)")
  173. }
  174. func onFailed(error: String) {
  175. print(#function, "error: \(error)")
  176. }
  177. }