Repository Public
1// SP Notes iOS2// File: App/AppDelegate.swift3// Description: The UIKit side of the app's life cycle: registration of the background tasks (before the end of launch) and the4// notification centre delegate — a reminder shows as a banner while the app is in the foreground, and tapping it5// opens its note; a task notification (2.0) opens the task's note or runs its action — and the scene6// configuration that names `SceneDelegate` (Home Screen Quick Actions).7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import UIKit11import UserNotifications1213final class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {14 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {15 AppRuntime.install()16 BackgroundSync.register()17 UNUserNotificationCenter.current().delegate = self18 return true19 }2021 /// Every window scene gets `SceneDelegate` (Home Screen Quick Actions); SwiftUI still builds the windows of the scene.22 func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {23 let configuration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)24 if connectingSceneSession.role == .windowApplication { configuration.delegateClass = SceneDelegate.self }25 return configuration26 }2728 /// The note a reminder notification belongs to.29 static func noteId(in strings: [String: String]) -> String? {30 guard let id = strings[ReminderScheduler.noteIdKey], DeepLink.isValidNoteID(id) else { return nil }31 return id.lowercased()32 }3334 // The completion-handler forms are used on purpose: the system calls them off the main actor.35 nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification,36 withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {37 completionHandler([.banner, .list, .sound])38 }3940 nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,41 withCompletionHandler completionHandler: @escaping () -> Void) {42 // Only the text values leave this callback (the notification objects are not Sendable).43 var strings: [String: String] = [:]44 for (key, value) in response.notification.request.content.userInfo {45 if let key = key as? String, let value = value as? String { strings[key] = value }46 }47 let action = response.actionIdentifier48 // 2.0: a task notification (tap, « Terminer », « Reporter à demain »). The system is told the work is done only when it49 // is: the two actions run in the background, without opening the app.50 if let task = TaskNotifications.response(action: action, category: response.notification.request.content.categoryIdentifier, userInfo: strings) {51 nonisolated(unsafe) let done = completionHandler52 Task { @MainActor in53 if let model = try? AppRuntime.model(), let url = await AppRuntime.taskNotifications?.perform(task) { model.open(url: url) }54 done()55 }56 return57 }58 if action != UNNotificationDismissActionIdentifier {59 Task { @MainActor in60 if let noteId = Self.noteId(in: strings) { (try? AppRuntime.model())?.open(url: DeepLink.note(noteId).url) }61 }62 }63 completionHandler()64 }65}66