SceneDelegate.swift 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // LoopFollow
  2. // SceneDelegate.swift
  3. import AVFoundation
  4. import UIKit
  5. class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  6. var window: UIWindow?
  7. let synthesizer = AVSpeechSynthesizer()
  8. func scene(_ scene: UIScene, willConnectTo _: UISceneSession, options _: UIScene.ConnectionOptions) {
  9. // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
  10. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
  11. // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
  12. guard let _ = (scene as? UIWindowScene) else { return }
  13. // get the tabBar
  14. guard let tabBarController = window?.rootViewController as? UITabBarController,
  15. let viewControllers = tabBarController.viewControllers
  16. else {
  17. return
  18. }
  19. }
  20. func sceneDidDisconnect(_: UIScene) {
  21. // Called as the scene is being released by the system.
  22. // This occurs shortly after the scene enters the background, or when its session is discarded.
  23. // Release any resources associated with this scene that can be re-created the next time the scene connects.
  24. // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
  25. }
  26. func sceneDidBecomeActive(_: UIScene) {
  27. // Called when the scene has moved from an inactive state to an active state.
  28. // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
  29. }
  30. func scene(_: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
  31. guard URLContexts.contains(where: { $0.url.scheme == AppGroupID.urlScheme && $0.url.host == "la-tap" }) else { return }
  32. // scene(_:openURLContexts:) fires after sceneDidBecomeActive when the app
  33. // foregrounds from background. Post on the next run loop so the view
  34. // hierarchy (including any presented modals) is fully settled.
  35. #if !targetEnvironment(macCatalyst)
  36. DispatchQueue.main.async {
  37. NotificationCenter.default.post(name: .liveActivityDidForeground, object: nil)
  38. }
  39. #endif
  40. }
  41. func sceneWillResignActive(_: UIScene) {
  42. // Called when the scene will move from an active state to an inactive state.
  43. // This may occur due to temporary interruptions (ex. an incoming phone call).
  44. }
  45. func sceneWillEnterForeground(_: UIScene) {
  46. // Called as the scene transitions from the background to the foreground.
  47. // Use this method to undo the changes made on entering the background.
  48. }
  49. func sceneDidEnterBackground(_: UIScene) {
  50. // Called as the scene transitions from the foreground to the background.
  51. // Use this method to save data, release shared resources, and store enough scene-specific state information
  52. // to restore the scene back to its current state.
  53. // Save changes in the application's managed object context when the application transitions to the background.
  54. (UIApplication.shared.delegate as? AppDelegate)?.saveContext()
  55. }
  56. /// Handle the UIApplicationShortcutItem when the user taps on the Home Screen Quick Action. This function toggles the "Speak BG" setting in UserDefaultsRepository, speaks the current state (on/off) using AVSpeechSynthesizer, and updates the Quick Action appearance.
  57. func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem) {
  58. if let bundleIdentifier = Bundle.main.bundleIdentifier {
  59. let expectedType = bundleIdentifier + ".toggleSpeakBG"
  60. if shortcutItem.type == expectedType {
  61. Storage.shared.speakBG.value.toggle()
  62. let message = Storage.shared.speakBG.value ? "BG Speak is now on" : "BG Speak is now off"
  63. let utterance = AVSpeechUtterance(string: message)
  64. synthesizer.speak(utterance)
  65. }
  66. }
  67. }
  68. /// The following method is called when the user taps on the Home Screen Quick Action
  69. func windowScene(_: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler _: @escaping (Bool) -> Void) {
  70. handleShortcutItem(shortcutItem)
  71. }
  72. }