You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
LeCountdown/LeCountdown/AppDelegate.swift

119 lines
3.9 KiB

//
// AppDelegate.swift
// LeCountdown
//
// Created by Laurent Morvillier on 24/01/2023.
//
import Foundation
import UIKit
import AVFoundation
import Firebase
class AppDelegate : NSObject, UIApplicationDelegate {
override init() {
super.init()
NotificationCenter.default.addObserver(self, selector: #selector(_contextDidChange), name: NSNotification.Name.NSManagedObjectContextDidSave, object: nil)
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
UNUserNotificationCenter.current().delegate = self
self._initSchemaIfNeeded()
Conductor.maestro.activateAudioSession()
Sound.computeSoundDurationsIfNecessary()
Conductor.maestro.cleanup()
if Preferences.installDate == nil {
Preferences.installDate = Date()
}
return true
}
func applicationWillEnterForeground(_ application: UIApplication) {
// self._activateAudioSession()
}
func applicationWillTerminate(_ application: UIApplication) {
Logger.log("applicationWillTerminate")
FileLogger.log("applicationWillTerminate")
Conductor.removeLiveActivities()
// Conductor.maestro.removeLiveActivities()
}
func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
Conductor.maestro.memoryWarningReceived = true
Logger.log("applicationDidReceiveMemoryWarning")
FileLogger.log("applicationDidReceiveMemoryWarning")
}
fileprivate func _initSchemaIfNeeded() {
if !Preferences.cloudKitSchemaInitialized {
do {
try PersistenceController.shared.container.initializeCloudKitSchema()
Preferences.cloudKitSchemaInitialized = true
} catch {
print("initializeCloudKitSchema error: \(error)")
}
}
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if userActivity.interaction == nil {
Logger.log("restorationHandler called! interaction is nil")
return false
}
return true
}
@objc fileprivate func _contextDidChange(_ notification: Notification) {
TimerShortcuts.updateAppShortcutParameters()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
Logger.log("didReceive notification")
FileLogger.log("userNotificationCenter didReceive > cancelling sound player")
if let timerId = self._timerId(notificationId: response.notification.request.identifier) {
Conductor.maestro.cancelSoundPlayers(id: timerId)
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("willPresent notification")
// completionHandler([.sound])
// let timerId = self._timerId(notificationId: notification.request.identifier)
// Conductor.maestro.notifyUser(countdownId: timerId)
}
fileprivate func _timerId(notificationId: String) -> TimerID? {
let components = notificationId.components(separatedBy: Conductor.notificationIdSeparator)
if components.count == 2 {
return components[0]
} else {
FileLogger.log("Couldn't parse notification Id: \(notificationId)")
return nil
}
}
}