You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

373 lines
16 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // AppDelegate.swift
  3. // GMERemittance
  4. //
  5. // Created by Fm-user on 11/30/17.
  6. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import Firebase
  10. import FirebaseAuth
  11. import UserNotifications
  12. import FirebaseInstanceID
  13. import FirebaseMessaging
  14. import BRYXBanner
  15. import Fabric
  16. import Crashlytics
  17. import AlamofireNetworkActivityLogger
  18. import IQKeyboardManagerSwift
  19. import LGSideMenuController
  20. import Localize_Swift
  21. let server: Server = .stagging
  22. @UIApplicationMain
  23. class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
  24. var window: UIWindow?
  25. let gcmMessageIDKey = "gcm_message_id"
  26. var firstTranscationStatusForHome:Bool?
  27. var firstTranscationStatusForProfileForm:Bool?
  28. var cmRegistrationId: String?
  29. var notificationSet: Box<Bool?> = Box(nil)
  30. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  31. UIApplication.shared.applicationIconBadgeNumber = 0
  32. firstTranscationStatusForHome = true
  33. firstTranscationStatusForProfileForm = true
  34. NetworkActivityLogger.shared.startLogging()
  35. NetworkActivityLogger.shared.level = .debug
  36. IQKeyboardManager.shared.enable = true
  37. FirebaseApp.configure()
  38. Fabric.with([Crashlytics.self])
  39. Fabric.sharedSDK().debug = true
  40. Messaging.messaging().delegate = self
  41. registerForNotification(application: application)
  42. setUUID()
  43. setupNavBar()
  44. setupStatusBar()
  45. setEntryPoint()
  46. return true
  47. }
  48. private func setupLanguage() {
  49. let language = UserDefaults.standard.string(forKey: AppConstants.firstTimeLanguageIsSet)
  50. if !(language != nil) == nil, let preferedDeviceLanguage = Locale.preferredLanguages.elementAt(index: 0), let _ = preferedDeviceLanguage.components(separatedBy: "-").first {
  51. // if Localize.availableLanguages().contains(lang) {
  52. // Localize.setCurrentLanguage(lang)
  53. // }else {
  54. Localize.setCurrentLanguage(language ?? "en")
  55. // }
  56. }
  57. }
  58. private func setUUID() {
  59. if UserDefaults.standard.string(forKey: AppConstants.uuid) == nil {
  60. let uuid = UUID().uuidString
  61. UserDefaults.standard.set(uuid, forKey: AppConstants.uuid)
  62. }
  63. }
  64. private func setupStatusBar() {
  65. // let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
  66. // if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
  67. // statusBar.backgroundColor = UIColor.blue
  68. // }
  69. }
  70. private func setupNavBar() {
  71. let appearance = UINavigationBar.appearance()
  72. appearance.backIndicatorImage = #imageLiteral(resourceName: "backIconBlack")
  73. appearance.backIndicatorTransitionMaskImage = #imageLiteral(resourceName: "backIconBlack")
  74. appearance.tintColor = UIColor.black
  75. }
  76. private func setEntryPoint() {
  77. let _default = UserDefaults.standard
  78. if let loginStatus = _default.string(forKey: UserKeys.accessCode) {
  79. // user is logged in
  80. let mainWireFrame = MainWireframe.shared
  81. // self.window?.rootViewController =
  82. let tabBarViewController = mainWireFrame?.getMainView()
  83. let sidemenuVc = UIStoryboard(name: "SideMenu", bundle: nil).instantiateViewController(withIdentifier: "SideMenuViewController") as! SideMenuViewController
  84. //
  85. let sideMenuController = LGSideMenuController(rootViewController: tabBarViewController, leftViewController: sidemenuVc, rightViewController: nil)
  86. //
  87. sideMenuController.rootViewLayerShadowColor = UIColor(white: 0.9, alpha: 0.6)
  88. sideMenuController.rootViewLayerShadowRadius = 8.0
  89. sideMenuController.leftViewPresentationStyle = .scaleFromBig
  90. sideMenuController.leftViewWidth = UIScreen.main.bounds.width - 70.0
  91. sideMenuController.leftViewBackgroundBlurEffect = UIBlurEffect(style: .regular)
  92. window?.backgroundColor = sidemenuVc.view.backgroundColor
  93. self.window?.rootViewController = sideMenuController
  94. }else {
  95. // go to splashscreen
  96. let splashWireframe = SplashScreenWireframe()
  97. let nav = UINavigationController.init(rootViewController: splashWireframe.getMainView())
  98. self.window?.rootViewController = nav
  99. }
  100. }
  101. private func registerForNotification(application: UIApplication) {
  102. let settings: UIUserNotificationSettings =
  103. UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  104. application.registerForRemoteNotifications()
  105. application.registerUserNotificationSettings(settings)
  106. }
  107. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  108. if userInfo[gcmMessageIDKey] != nil {
  109. }
  110. }
  111. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  112. fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  113. // if userInfo[gcmMessageIDKey] != nil {
  114. //// print("Message ID completionHandler: \(messageID)")
  115. // }
  116. //
  117. // if
  118. // let aps = userInfo["aps"] as? NSDictionary,
  119. // let alert = aps["alert"] as? NSDictionary,
  120. // let _ = alert["body"] as? String,
  121. // let _ = alert["title"] as? String {
  122. // let codeID = userInfo["code"] as? String
  123. // self.redirectWithPushNotification(notificationCode: codeID!)
  124. // }
  125. // completionHandler(UIBackgroundFetchResult.newData)
  126. }
  127. func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
  128. cmRegistrationId = fcmToken
  129. notificationSet.value = true
  130. print(fcmToken)
  131. }
  132. func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
  133. print(remoteMessage.appData)
  134. }
  135. public func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
  136. }
  137. @available(iOS 10.0, *)
  138. func userNotificationCenter(_ center: UNUserNotificationCenter,
  139. willPresent notification: UNNotification,
  140. withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  141. var userInfo = notification.request.content.userInfo
  142. if userInfo[gcmMessageIDKey] != nil {
  143. // print("Message ID: def \(messageID)")
  144. }
  145. /**
  146. redirection of page after foreground push notification
  147. */
  148. if
  149. let aps = userInfo["aps"] as? NSDictionary,
  150. let alert = aps["alert"] as? NSDictionary,
  151. let body = alert["body"] as? String,
  152. let title = alert["title"] as? String {
  153. // print("Title: \(title) \nBody:\(body)")
  154. let codeID = userInfo["code"] as? String
  155. let banner = Banner(title: title, subtitle: body, image: #imageLiteral(resourceName: "ic_gme"), backgroundColor: UIColor(red:0.93, green:0.11, blue:0.14, alpha:1.0))
  156. banner.didTapBlock = {
  157. // print("Banner was tapped on \(Date())!")
  158. self.redirectWithPushNotification(notificationCode: codeID!)
  159. }
  160. banner.show(duration: 5.0)
  161. }
  162. if userInfo["code"] != nil {
  163. // print("codeAP delegate: \(codeID)")
  164. }
  165. // Change this to your preferred presentation option
  166. completionHandler([])
  167. }
  168. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  169. // print("Unable to register for remote notifications: \(error.localizedDescription)")
  170. }
  171. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  172. }
  173. /**
  174. method for redirection of page when push notification tap
  175. */
  176. func redirectWithPushNotification(notificationCode: String){
  177. let nav = UINavigationController()
  178. switch notificationCode {
  179. case "500":
  180. InviteViewController.notificationCode = "p500"
  181. let storyboard = UIStoryboard(name: "Invite", bundle: nil)
  182. let viewController = storyboard.instantiateViewController(withIdentifier :"InviteViewController") as! InviteViewController
  183. let navController = UINavigationController.init(rootViewController: viewController)
  184. if let window = self.window, let rootViewController = window.rootViewController {
  185. var currentController = rootViewController
  186. while let presentedController = currentController.presentedViewController {
  187. currentController = presentedController
  188. }
  189. currentController.present(navController, animated: true, completion: nil)
  190. }
  191. case "200":
  192. TranscationStatementViewController.notificationCode = "p200"
  193. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  194. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  195. nav.viewControllers = [transcationStatementViewController]
  196. window?.rootViewController = nav
  197. }
  198. case "201":
  199. MoneyRequestViewController.notificationCode = "p201"
  200. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  201. if let moneyRequestViewController = storyboard.instantiateViewController(withIdentifier: "MoneyRequestViewController") as? MoneyRequestViewController {
  202. nav.viewControllers = [moneyRequestViewController]
  203. window?.rootViewController = nav
  204. }
  205. case "202":
  206. // redirectViewController(storyBoardName: "WalletTransfer", identifier: "WalletTransactionListViewController")
  207. WalletTransactionListViewController.notificationCode = "p202"
  208. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  209. if let walletTransactionListViewController = storyboard.instantiateViewController(withIdentifier: "WalletTransactionListViewController") as? WalletTransactionListViewController {
  210. walletTransactionListViewController.walletStatus = "walletBorrow"
  211. nav.viewControllers = [walletTransactionListViewController]
  212. window?.rootViewController = nav
  213. }
  214. case "203":
  215. TranscationStatementViewController.notificationCode = "p203"
  216. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  217. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  218. nav.viewControllers = [transcationStatementViewController]
  219. window?.rootViewController = nav
  220. }
  221. case "204":
  222. TranscationStatementViewController.notificationCode = "p204"
  223. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  224. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  225. nav.viewControllers = [transcationStatementViewController]
  226. window?.rootViewController = nav
  227. }
  228. case "205":
  229. // redirectViewController(storyBoardName: "WalletTransfer", identifier: "walletViewController")
  230. WalletTransactionListViewController.notificationCode = "p205"
  231. let storyboard = UIStoryboard.init(name: "WalletTransfer", bundle: Bundle.main)
  232. if let walletTransactionListViewController = storyboard.instantiateViewController(withIdentifier: "WalletTransactionListViewController") as? WalletTransactionListViewController {
  233. walletTransactionListViewController.walletStatus = "walletTransfer"
  234. nav.viewControllers = [walletTransactionListViewController]
  235. window?.rootViewController = nav
  236. }
  237. case "206":
  238. TranscationStatementViewController.notificationCode = "p200"
  239. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  240. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  241. nav.viewControllers = [transcationStatementViewController]
  242. window?.rootViewController = nav
  243. }
  244. case "207":
  245. TranscationStatementViewController.notificationCode = "p200"
  246. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  247. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  248. nav.viewControllers = [transcationStatementViewController]
  249. window?.rootViewController = nav
  250. }
  251. case "208":
  252. TranscationStatementViewController.notificationCode = "p208"
  253. let storyboard = UIStoryboard.init(name: "TransactionStatement", bundle: Bundle.main)
  254. if let transcationStatementViewController = storyboard.instantiateViewController(withIdentifier: "TranscationStatementViewController") as? TranscationStatementViewController {
  255. nav.viewControllers = [transcationStatementViewController]
  256. window?.rootViewController = nav
  257. }
  258. case "700":
  259. // redirectViewController(storyBoardName: "Reward", identifier: "RewardViewController")
  260. RewardViewController.notificationCode = "p700"
  261. let storyboard = UIStoryboard.init(name: "Reward", bundle: Bundle.main)
  262. if let rewardViewController = storyboard.instantiateViewController(withIdentifier: "RewardViewController") as? RewardViewController {
  263. nav.viewControllers = [rewardViewController]
  264. window?.rootViewController = nav
  265. }
  266. case "750":
  267. return
  268. //redirectViewController(storyBoardName: "Home", identifier: "CommentsViewController")
  269. default:
  270. return
  271. }
  272. }
  273. }