Repository Public
1// SP Notes iOS2// File: App/Intents/AppRuntime.swift3// Description: The single application model of the process, reachable from code that does not live in the view hierarchy4// (App Intents, background tasks, notification taps). `Boot` and every system entry point go through this holder,5// so a second `AppModel` — and a second database connection — is never created in the same process.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import SPNotesCore1112@MainActor13enum AppRuntime {14 /// Created on first use, once per process. A failure to open the database is kept and shown by the root view15 /// (`StoreFailureScreen`); `retry()` tries again after a transient failure (locked file, full disk).16 private(set) static var result: Result<AppModel, Error> = make()1718 private static func make() -> Result<AppModel, Error> {19 let result = Result { try AppModel() }20 if case .success(let model) = result { didCreate(model) }21 return result22 }2324 /// Opens the database again after a failure. Never replaces a model that exists.25 @discardableResult26 static func retry() -> Result<AppModel, Error> {27 if case .failure = result { result = make() }28 return result29 }3031 static func model() throws -> AppModel { try result.get() }3233 /// Makes the shared readers (note picker, entity queries) use the app's store. Cheap; called before the model exists.34 static func install() {35 SharedNotesStore.useAppStore { try await MainActor.run { try AppRuntime.model().store } }36 // Widget buttons and task intents run in the app when it is alive: they use its repositories and its routing.37 SharedTaskActions.useAppTasks { id, done in38 let tasks = try await MainActor.run { try AppRuntime.model().repositories.tasks }39 try await tasks.setDone(id: id, done)40 }41 SystemOpen.useApp { url in await MainActor.run { _ = (try? AppRuntime.model())?.open(url: url) } }42 }4344 private(set) static var spotlight: SpotlightIndexer?45 private(set) static var taskSpotlight: SpotlightTaskIndexer?46 /// Due-date notifications of the tasks (2.0); nil only when the database could not be opened.47 private(set) static var taskNotifications: TaskNotifications?4849 private static func didCreate(_ model: AppModel) {50 install()51 // 2.0: the screens of SPNotesFeatures, listed by the shell when the server offers them52 FeatureSections.install(app: model)53 // Background launches (intent, background task, notification) have no root view to start the observers.54 model.start()55 model.installSystemIntegration()56 // Throw-away stores (tests) are not indexed in the system search.57 if !model.configuration.inMemoryStore {58 let indexer = SpotlightIndexer(store: model.store)59 spotlight = indexer60 Task { await indexer.start() }61 let tasks = SpotlightTaskIndexer(store: model.store)62 taskSpotlight = tasks63 Task { await tasks.start() }64 }65 let notifications = TaskNotifications(store: model.store, tasks: model.repositories.tasks)66 taskNotifications = notifications67 notifications.start()68 Task { await model.rescheduleReminders() }69 }70}7172extension AppModel {73 /// Mirrors the pending reminders of the store into the notification centre (they are lost when the app is reinstalled or74 /// restored, and reminders received from another device were never scheduled here). Does not ask for permission.75 func rescheduleReminders(now: Date = Date()) async {76 guard let upcoming = try? await store.upcomingReminders(from: now) else { return }77 for reminder in upcoming {78 var title = ""79 if let pageId = reminder.pageId, let summary = try? await store.summary(id: pageId) {80 if summary.deletedAt != nil { continue }81 title = AppModel.displayTitle(summary.title)82 }83 await reminders.schedule(reminder, noteTitle: title)84 }85 }86}87