SceneDelegate.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. DispatchQueue.main.async {
  36. NotificationCenter.default.post(name: .liveActivityDidForeground, object: nil)
  37. }
  38. }
  39. func sceneWillResignActive(_: UIScene) {
  40. // Called when the scene will move from an active state to an inactive state.
  41. // This may occur due to temporary interruptions (ex. an incoming phone call).
  42. }
  43. func sceneWillEnterForeground(_: UIScene) {
  44. // Called as the scene transitions from the background to the foreground.
  45. // Use this method to undo the changes made on entering the background.
  46. }
  47. func sceneDidEnterBackground(_: UIScene) {
  48. // Called as the scene transitions from the foreground to the background.
  49. // Use this method to save data, release shared resources, and store enough scene-specific state information
  50. // to restore the scene back to its current state.
  51. // Save changes in the application's managed object context when the application transitions to the background.
  52. (UIApplication.shared.delegate as? AppDelegate)?.saveContext()
  53. }
  54. /// 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.
  55. func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem) {
  56. if let bundleIdentifier = Bundle.main.bundleIdentifier {
  57. let expectedType = bundleIdentifier + ".toggleSpeakBG"
  58. if shortcutItem.type == expectedType {
  59. Storage.shared.speakBG.value.toggle()
  60. let message = Storage.shared.speakBG.value ? "BG Speak is now on" : "BG Speak is now off"
  61. let utterance = AVSpeechUtterance(string: message)
  62. synthesizer.speak(utterance)
  63. }
  64. }
  65. }
  66. /// The following method is called when the user taps on the Home Screen Quick Action
  67. func windowScene(_: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler _: @escaping (Bool) -> Void) {
  68. handleShortcutItem(shortcutItem)
  69. }
  70. }