AppDelegate.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  79. var showButton = false
  80. if PrefsUtil.getCpaasMode() == PrefsUtil.CPAAS_MODE_FLOATING || PrefsUtil.getCpaasMode() == PrefsUtil.CPAAS_MODE_MIX {
  81. showButton = true
  82. }
  83. Nexilis.connect(apiKey: "***REPLACE***WITH***YOUR***ACCOUNT***", delegate: self, showButton: showButton, fromMAB: true) //AA6164AA13F0A6262677AC7443C37E3F6F0A342E8BEE84B700CB59C876166FE0(AWS) //1C4FA430BC9F44484AFBE99C8974D70B68CE7891DBE83505CBF01205DE51FC18(CBN)
  84. let tabBarAppearance: UITabBarAppearance = UITabBarAppearance()
  85. tabBarAppearance.configureWithTransparentBackground()
  86. if Bundle.main.displayName == "DigiNetS" {
  87. tabBarAppearance.backgroundColor = .black.withAlphaComponent(0.7)
  88. } else {
  89. tabBarAppearance.backgroundColor = .white.withAlphaComponent(0.9)
  90. }
  91. if #available(iOS 13.0, *) {
  92. UITabBar.appearance().standardAppearance = tabBarAppearance
  93. }
  94. if #available(iOS 15.0, *) {
  95. UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
  96. }
  97. UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
  98. let cancelButtonAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
  99. UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes , for: .normal)
  100. registerForPushNotifications()
  101. return true
  102. }
  103. // MARK: UISceneSession Lifecycle
  104. func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  105. // Called when a new scene session is being created.
  106. // Use this method to select a configuration to create the new scene with.
  107. return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  108. }
  109. func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  110. Nexilis.destroyAll()
  111. // Called when the user discards a scene session.
  112. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
  113. // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
  114. }
  115. private func registerForPushNotifications() {
  116. if #available(iOS 10.0, *) {
  117. let center = UNUserNotificationCenter.current()
  118. center.delegate = self
  119. center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
  120. if error == nil{
  121. DispatchQueue.main.async {
  122. UIApplication.shared.registerForRemoteNotifications()
  123. }
  124. }
  125. }
  126. }
  127. else {
  128. UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
  129. UIApplication.shared.registerForRemoteNotifications()
  130. }
  131. }
  132. }
  133. extension AppDelegate: ConnectDelegate, UNUserNotificationCenterDelegate {
  134. func onSuccess(userId: String) {
  135. print(#function, "userId: \(userId)")
  136. }
  137. func onFailed(error: String) {
  138. print(#function, "error: \(error)")
  139. }
  140. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  141. let deviceTokenString = deviceToken.map { String(format: "%02x", $0) }.joined()
  142. print("TOKEN: \(deviceTokenString)")
  143. }
  144. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  145. print(error)
  146. }
  147. func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  148. let userInfo = response.notification.request.content.userInfo
  149. if let opposite = userInfo["opposite"] as? String {
  150. let user = User.getDataCanNil(pin: opposite)
  151. if user != nil {
  152. openEditorPersonal(opposite: opposite)
  153. } else {
  154. let group = Group.getData(group_id: opposite)
  155. let topic = Topic.getData(topic_id: opposite)
  156. if group != nil || topic != nil {
  157. openEditorGroup(opposite: opposite)
  158. }
  159. }
  160. }
  161. UIApplication.shared.applicationIconBadgeNumber = 0
  162. UNUserNotificationCenter.current().removeAllDeliveredNotifications()
  163. completionHandler()
  164. }
  165. func openEditorPersonal(opposite: String) {
  166. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  167. editorPersonalVC.hidesBottomBarWhenPushed = true
  168. editorPersonalVC.unique_l_pin = opposite
  169. editorPersonalVC.fromNotification = true
  170. let onGoingCC = UserDefaults.standard.string(forKey: "onGoingCC") ?? ""
  171. if !onGoingCC.isEmpty {
  172. let compalintId = onGoingCC.components(separatedBy: ",")[2]
  173. let fPinCC = onGoingCC.isEmpty ? "" : onGoingCC.components(separatedBy: ",")[1]
  174. editorPersonalVC.isContactCenter = true
  175. editorPersonalVC.fPinContacCenter = fPinCC
  176. editorPersonalVC.complaintId = compalintId
  177. editorPersonalVC.onGoingCC = true
  178. editorPersonalVC.isRequestContactCenter = false
  179. }
  180. let navigationController = UINavigationController(rootViewController: editorPersonalVC)
  181. navigationController.modalPresentationStyle = .fullScreen
  182. navigationController.navigationBar.tintColor = .white
  183. navigationController.navigationBar.barTintColor = .mainColor
  184. navigationController.navigationBar.isTranslucent = false
  185. navigationController.navigationBar.overrideUserInterfaceStyle = .dark
  186. navigationController.navigationBar.barStyle = .black
  187. let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white]
  188. UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, for: .normal)
  189. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  190. navigationController.navigationBar.titleTextAttributes = textAttributes
  191. navigationController.view.backgroundColor = .mainColor
  192. if UIApplication.shared.visibleViewController?.navigationController != nil {
  193. UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
  194. } else {
  195. UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
  196. }
  197. }
  198. func openEditorGroup(opposite: String) {
  199. let editorGroupVC = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "editorGroupVC") as! EditorGroup
  200. editorGroupVC.hidesBottomBarWhenPushed = true
  201. editorGroupVC.unique_l_pin = opposite
  202. editorGroupVC.fromNotification = true
  203. let navigationController = UINavigationController(rootViewController: editorGroupVC)
  204. navigationController.modalPresentationStyle = .fullScreen
  205. navigationController.navigationBar.tintColor = .white
  206. navigationController.navigationBar.barTintColor = .mainColor
  207. navigationController.navigationBar.isTranslucent = false
  208. navigationController.navigationBar.overrideUserInterfaceStyle = .dark
  209. navigationController.navigationBar.barStyle = .black
  210. let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white]
  211. UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, for: .normal)
  212. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  213. navigationController.navigationBar.titleTextAttributes = textAttributes
  214. navigationController.view.backgroundColor = .mainColor
  215. if UIApplication.shared.visibleViewController?.navigationController != nil {
  216. UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
  217. } else {
  218. UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
  219. }
  220. }
  221. }