SceneDelegate.swift 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // SceneDelegate.swift
  3. // LoopFollow
  4. //
  5. // Created by Jon Fawcett on 6/1/20.
  6. // Copyright © 2020 Jon Fawcett. All rights reserved.
  7. //
  8. import UIKit
  9. class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  10. var window: UIWindow?
  11. let appStateController = AppStateController()
  12. func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  13. // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
  14. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
  15. // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
  16. guard let _ = (scene as? UIWindowScene) else { return }
  17. // get the tabBar
  18. guard let tabBarController = window?.rootViewController as? UITabBarController,
  19. let viewControllers = tabBarController.viewControllers
  20. else {
  21. return
  22. }
  23. // set the main controllers' connection to the app sate
  24. // other controllers that need to know app state are setup programatically
  25. for i in 0..<viewControllers.count {
  26. if let vc = viewControllers[i] as? MainViewController {
  27. vc.appStateController = appStateController
  28. }
  29. if let vc = viewControllers[i] as? NightscoutViewController {
  30. vc.appStateController = appStateController
  31. }
  32. if let vc = viewControllers[i] as? SettingsViewController {
  33. vc.appStateController = appStateController
  34. }
  35. if let vc = viewControllers[i] as? AlarmViewController {
  36. vc.appStateController = appStateController
  37. }
  38. if let vc = viewControllers[i] as? SnoozeViewController {
  39. vc.appStateController = appStateController
  40. }
  41. if let vc = viewControllers[i] as? debugViewController {
  42. vc.appStateController = appStateController
  43. }
  44. }
  45. }
  46. func sceneDidDisconnect(_ scene: UIScene) {
  47. // Called as the scene is being released by the system.
  48. // This occurs shortly after the scene enters the background, or when its session is discarded.
  49. // Release any resources associated with this scene that can be re-created the next time the scene connects.
  50. // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
  51. }
  52. func sceneDidBecomeActive(_ scene: UIScene) {
  53. // Called when the scene has moved from an inactive state to an active state.
  54. // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
  55. }
  56. func sceneWillResignActive(_ scene: UIScene) {
  57. // Called when the scene will move from an active state to an inactive state.
  58. // This may occur due to temporary interruptions (ex. an incoming phone call).
  59. }
  60. func sceneWillEnterForeground(_ scene: UIScene) {
  61. // Called as the scene transitions from the background to the foreground.
  62. // Use this method to undo the changes made on entering the background.
  63. }
  64. func sceneDidEnterBackground(_ scene: UIScene) {
  65. // Called as the scene transitions from the foreground to the background.
  66. // Use this method to save data, release shared resources, and store enough scene-specific state information
  67. // to restore the scene back to its current state.
  68. // Save changes in the application's managed object context when the application transitions to the background.
  69. (UIApplication.shared.delegate as? AppDelegate)?.saveContext()
  70. }
  71. }