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.
105 lines
3.4 KiB
105 lines
3.4 KiB
//
|
|
// AppDelegate.swift
|
|
// LeCountdown
|
|
//
|
|
// Created by Laurent Morvillier on 24/01/2023.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import AVFoundation
|
|
import FirebaseCore
|
|
|
|
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()
|
|
self._activateAudioSession()
|
|
|
|
Sound.computeSoundDurationsIfNecessary()
|
|
|
|
Conductor.maestro.cleanup()
|
|
|
|
return true
|
|
}
|
|
|
|
fileprivate func _initSchemaIfNeeded() {
|
|
if !Preferences.cloudKitSchemaInitialized {
|
|
do {
|
|
try PersistenceController.shared.container.initializeCloudKitSchema()
|
|
Preferences.cloudKitSchemaInitialized = true
|
|
} catch {
|
|
print("initializeCloudKitSchema error: \(error)")
|
|
}
|
|
}
|
|
}
|
|
|
|
fileprivate func _activateAudioSession() {
|
|
do {
|
|
let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
|
|
try audioSession.setCategory(.playback)
|
|
try audioSession.setActive(true)
|
|
} catch {
|
|
Logger.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 {
|
|
|
|
print("didReceive notification")
|
|
let timerId = self._timerId(notificationId: response.notification.request.identifier)
|
|
// Conductor.maestro.cancelCountdown(id: timerId)
|
|
Conductor.maestro.cancelSoundPlayer(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: CountdownScheduler.notificationIdSeparator)
|
|
if components.count == 2 {
|
|
return components[0]
|
|
} else {
|
|
fatalError("bad notification format : \(notificationId)")
|
|
}
|
|
}
|
|
|
|
}
|
|
|