SPB Git

Repository Public

NoteIntents.swift 4.7 KB 126 lines swift

1// SP Notes iOS2// File: App/Intents/NoteIntents.swift3// Description: App Intents of SP Notes (Shortcuts, Siri, Spotlight): create a note, append to a note, search notes, open a note,4//              open the search. They run in the app process on the single application model and write locally first; nothing5//              waits for the network.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import AppIntents10import Foundation1112struct CreateNoteIntent: AppIntent {13    static let title: LocalizedStringResource = "Créer une note"14    static let description = IntentDescription("Crée une note dans SP Notes sans ouvrir l’app.", categoryName: "Notes")15    static let openAppWhenRun = false1617    @Parameter(title: "Titre") var title: String?18    @Parameter(title: "Texte", inputOptions: String.IntentInputOptions(multiline: true), requestValueDialog: "Que veux-tu noter ?") var text: String1920    static var parameterSummary: some ParameterSummary {21        Summary("Créer une note avec \(\.$text)") { \.$title }22    }2324    init() {}25    init(title: String? = nil, text: String) {26        self.title = title27        self.text = text28    }2930    @MainActor31    func perform() async throws -> some IntentResult & ReturnsValue<NoteEntity> & ProvidesDialog {32        let app = try AppRuntime.model()33        let id = try await app.quickCreate(title: title, text: text)34        let entity = try await app.noteEntity(id: id)35        return .result(value: entity, dialog: "La note « \(entity.title) » est créée.")36    }37}3839struct AppendToNoteIntent: AppIntent {40    static let title: LocalizedStringResource = "Ajouter à une note"41    static let description = IntentDescription("Ajoute un paragraphe à la fin d’une note.", categoryName: "Notes")42    static let openAppWhenRun = false4344    @Parameter(title: "Note") var note: NoteEntity45    @Parameter(title: "Texte", inputOptions: String.IntentInputOptions(multiline: true), requestValueDialog: "Que veux-tu ajouter ?") var text: String4647    static var parameterSummary: some ParameterSummary {48        Summary("Ajouter \(\.$text) à \(\.$note)")49    }5051    @MainActor52    func perform() async throws -> some IntentResult & ReturnsValue<NoteEntity> & ProvidesDialog {53        let app = try AppRuntime.model()54        try await app.appendText(text, toNote: note.id)55        let entity = try await app.noteEntity(id: note.id)56        return .result(value: entity, dialog: "C’est ajouté à « \(entity.title) ».")57    }58}5960struct SearchNotesIntent: AppIntent {61    static let title: LocalizedStringResource = "Chercher des notes"62    static let description = IntentDescription("Renvoie les notes qui correspondent à une recherche.", categoryName: "Notes")63    static let openAppWhenRun = false6465    @Parameter(title: "Recherche") var query: String6667    static var parameterSummary: some ParameterSummary {68        Summary("Chercher \(\.$query) dans les notes")69    }7071    @MainActor72    func perform() async throws -> some IntentResult & ReturnsValue<[NoteEntity]> {73        .result(value: try await AppRuntime.model().searchNotes(query))74    }75}7677struct OpenSearchIntent: AppIntent {78    static let title: LocalizedStringResource = "Ouvrir la recherche"79    static let description = IntentDescription("Ouvre SP Notes sur les résultats d’une recherche.", categoryName: "Notes")80    static let openAppWhenRun = true8182    @Parameter(title: "Recherche") var query: String?8384    static var parameterSummary: some ParameterSummary {85        Summary("Ouvrir la recherche de \(\.$query)")86    }8788    @MainActor89    func perform() async throws -> some IntentResult {90        try AppRuntime.model().open(url: DeepLink.search(query ?? "").url)91        return .result()92    }93}9495struct OpenNoteIntent: OpenIntent {96    static let title: LocalizedStringResource = "Ouvrir une note"97    static let description = IntentDescription("Ouvre une note dans SP Notes.", categoryName: "Notes")9899    @Parameter(title: "Note") var target: NoteEntity100101    static var parameterSummary: some ParameterSummary {102        Summary("Ouvrir \(\.$target)")103    }104105    init() {}106    init(target: NoteEntity) { self.target = target }107108    @MainActor109    func perform() async throws -> some IntentResult {110        try AppRuntime.model().open(url: DeepLink.note(target.id).url)111        return .result()112    }113}114115struct NewNoteInAppIntent: AppIntent {116    static let title: LocalizedStringResource = "Nouvelle note"117    static let description = IntentDescription("Ouvre SP Notes sur une note vierge.", categoryName: "Notes")118    static let openAppWhenRun = true119120    @MainActor121    func perform() async throws -> some IntentResult {122        try AppRuntime.model().open(url: DeepLink.newNote.url)123        return .result()124    }125}126
Files main
Menu

Theme