SceneDelegate.swift 3.7 KB

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