SPB Git

Repository Public

QuickActions.swift 2.6 KB 52 lines swift

1// SP Notes iOS2// File: App/QuickActions.swift3// Description: Home Screen Quick Actions (long press on the app icon): « Nouvelle note », « Capture », « Dictée », « Rechercher »,4//              declared statically in Info.plist (`UIApplicationShortcutItems`). Each one is a `spnotes://` link handed to5//              `AppModel.open(url:)`. They arrive through the window scene delegate below — at a cold launch in the connection6//              options, otherwise in `performActionFor` — which the app delegate names in its scene configuration; SwiftUI7//              keeps creating the windows.8//9// Author: Simon-Pierre Boucher10// Contact: contact@spboucher.ai11import UIKit1213enum QuickAction: String, CaseIterable, Sendable {14    case newNote = "ai.spboucher.spnotes.ios.quick.new"15    case capture = "ai.spboucher.spnotes.ios.quick.capture"16    case dictation = "ai.spboucher.spnotes.ios.quick.dictation"17    case search = "ai.spboucher.spnotes.ios.quick.search"1819    /// The action of a shortcut item type; nil for anything that is not ours.20    init?(type: String) { self.init(rawValue: type) }2122    /// Where the action leads.23    var url: URL {24        switch self {25        case .newNote: return SystemLinks.newNote26        case .capture: return SystemLinks.capture()27        case .dictation: return SystemLinks.capture(kind: "dictation")28        case .search: return SystemLinks.search29        }30    }3132    /// Runs the action of a shortcut item type on the application model. False when the type is unknown or the link was refused.33    @MainActor @discardableResult34    static func perform(type: String, on model: AppModel? = try? AppRuntime.model()) -> Bool {35        guard let action = QuickAction(type: type), let model else { return false }36        return model.open(url: action.url)37    }38}3940/// The delegate of every window scene. It only receives the Quick Actions; links, Handoff and Spotlight stay with SwiftUI41/// (`onOpenURL`, `onContinueUserActivity` in `SPNotesApp`).42final class SceneDelegate: NSObject, UIWindowSceneDelegate {43    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {44        // Cold launch from a Quick Action: the route waits in the model until the root view is there.45        if let type = connectionOptions.shortcutItem?.type { QuickAction.perform(type: type) }46    }4748    func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {49        completionHandler(QuickAction.perform(type: shortcutItem.type))50    }51}52
Files main
Menu

Theme