SceneDelegate.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // LoopFollow
  2. // SceneDelegate.swift
  3. // Created by Jon Fawcett.
  4. import AVFoundation
  5. import UIKit
  6. class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  7. var window: UIWindow?
  8. let synthesizer = AVSpeechSynthesizer()
  9. func scene(_ scene: UIScene, willConnectTo _: UISceneSession, options _: UIScene.ConnectionOptions) {
  10. // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
  11. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
  12. // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
  13. guard let _ = (scene as? UIWindowScene) else { return }
  14. // get the tabBar
  15. guard let tabBarController = window?.rootViewController as? UITabBarController,
  16. let viewControllers = tabBarController.viewControllers
  17. else {
  18. return
  19. }
  20. }
  21. func sceneDidDisconnect(_: UIScene) {
  22. // Called as the scene is being released by the system.
  23. // This occurs shortly after the scene enters the background, or when its session is discarded.
  24. // Release any resources associated with this scene that can be re-created the next time the scene connects.
  25. // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
  26. }
  27. func sceneDidBecomeActive(_: UIScene) {
  28. // Called when the scene has moved from an inactive state to an active state.
  29. // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
  30. }
  31. func sceneWillResignActive(_: UIScene) {
  32. // Called when the scene will move from an active state to an inactive state.
  33. // This may occur due to temporary interruptions (ex. an incoming phone call).
  34. }
  35. func sceneWillEnterForeground(_: UIScene) {
  36. // Called as the scene transitions from the background to the foreground.
  37. // Use this method to undo the changes made on entering the background.
  38. }
  39. func sceneDidEnterBackground(_: UIScene) {
  40. // Called as the scene transitions from the foreground to the background.
  41. // Use this method to save data, release shared resources, and store enough scene-specific state information
  42. // to restore the scene back to its current state.
  43. // Save changes in the application's managed object context when the application transitions to the background.
  44. (UIApplication.shared.delegate as? AppDelegate)?.saveContext()
  45. }
  46. // 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.
  47. func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem) {
  48. if let bundleIdentifier = Bundle.main.bundleIdentifier {
  49. let expectedType = bundleIdentifier + ".toggleSpeakBG"
  50. if shortcutItem.type == expectedType {
  51. Storage.shared.speakBG.value.toggle()
  52. let message = Storage.shared.speakBG.value ? "BG Speak is now on" : "BG Speak is now off"
  53. let utterance = AVSpeechUtterance(string: message)
  54. synthesizer.speak(utterance)
  55. }
  56. }
  57. }
  58. // The following method is called when the user taps on the Home Screen Quick Action
  59. func windowScene(_: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler _: @escaping (Bool) -> Void) {
  60. handleShortcutItem(shortcutItem)
  61. }
  62. }