SPB Git

Repository Public

TaskIntents.swift 6.0 KB 131 lines swift

1// SP Notes iOS2// File: App/Intents/TaskIntents.swift3// Description: App Intents of 2.0 (Shortcuts, Siri): create a task, capture into the inbox, ask the notes. They run in the app4//              process on the single application model, write locally first (they work offline) and never wait for the5//              network. « Terminer une tâche » lives in WidgetShared/TaskEntity.swift (shared with the widget).6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import AppIntents10import Foundation11import SPNotesCore12import SPNotesDesign13import SwiftUI1415enum TaskPriorityOption: String, AppEnum {16    case low, medium, high1718    static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Priorité")19    static let caseDisplayRepresentations: [TaskPriorityOption: DisplayRepresentation] = [20        .low: "Basse", .medium: "Moyenne", .high: "Haute",21    ]2223    var core: SPNotesAPI.TaskPriority { SPNotesAPI.TaskPriority(rawValue: rawValue) ?? .medium }24}2526struct CreateTaskIntent: AppIntent {27    static let title: LocalizedStringResource = "Créer une tâche"28    static let description = IntentDescription("Crée une tâche dans SP Notes sans ouvrir l’app. Fonctionne hors ligne.", categoryName: "Tâches")29    static let openAppWhenRun = false3031    @Parameter(title: "Titre", requestValueDialog: "Quelle tâche veux-tu ajouter ?") var title: String32    /// A date at midnight means "that day" (no time); any other time is kept.33    @Parameter(title: "Échéance") var due: Date?34    @Parameter(title: "Priorité") var priority: TaskPriorityOption?3536    static var parameterSummary: some ParameterSummary {37        Summary("Créer la tâche \(\.$title)") { \.$due; \.$priority }38    }3940    init() {}41    init(title: String, due: Date? = nil, priority: TaskPriorityOption? = nil) {42        self.title = title43        self.due = due44        self.priority = priority45    }4647    @MainActor48    func perform() async throws -> some IntentResult & ReturnsValue<TaskEntity> & ProvidesDialog & ShowsSnippetView {49        let app = try AppRuntime.model()50        let task = try await app.createTask(title: title, due: due, priority: priority?.core ?? .medium)51        let shown = WidgetTask(task)52        return .result(value: TaskEntity(shown), dialog: "La tâche « \(task.title) » est créée.") { TaskSnippetView(task: shown) }53    }54}5556extension AppModel {57    /// Creates a task from a system entry point. A due date at midnight (local) is a bare day; any other time is an instant.58    func createTask(title: String, due: Date?, priority: SPNotesAPI.TaskPriority = .medium, calendar: Calendar = .current) async throws -> TaskItem {59        guard let due else { return try await repositories.tasks.create(title: title, priority: priority) }60        let time = calendar.dateComponents([.hour, .minute, .second], from: due)61        if time.hour == 0, time.minute == 0, time.second == 0 {62            return try await repositories.tasks.create(title: title, dueDay: TaskDueRule.dayString(of: due, calendar: calendar), priority: priority)63        }64        return try await repositories.tasks.create(title: title, dueAt: due, priority: priority)65    }66}6768/// What Siri and Shortcuts show after « Créer une tâche ».69struct TaskSnippetView: View {70    let task: WidgetTask7172    var body: some View {73        HStack(alignment: .top, spacing: NVSpace.md) {74            Image(systemName: "circle").font(.title3).foregroundStyle(task.isHighPriority ? NVColor.danger : NVColor.accent).accessibilityHidden(true)75            VStack(alignment: .leading, spacing: NVSpace.xxs) {76                Text(task.title).font(WidgetFont.title).foregroundStyle(NVColor.fg)77                if let due = task.dueAt {78                    TaskDueText(due: due, overdue: task.isOverdue).font(.footnote).foregroundStyle(task.isOverdue ? NVColor.dangerText : NVColor.fgMuted)79                }80                if task.isHighPriority { Text("Priorité haute").font(.caption).foregroundStyle(NVColor.dangerText) }81            }82            Spacer(minLength: 0)83        }84        .padding(NVSpace.lg)85    }86}8788struct CaptureToInboxIntent: AppIntent {89    static let title: LocalizedStringResource = "Capturer dans la boîte de réception"90    static let description = IntentDescription("Envoie un texte dans la boîte de réception de SP Notes, sans ouvrir l’app.", categoryName: "Capture")91    static let openAppWhenRun = false9293    @Parameter(title: "Texte", inputOptions: String.IntentInputOptions(multiline: true), requestValueDialog: "Que veux-tu capturer ?") var text: String9495    static var parameterSummary: some ParameterSummary {96        Summary("Capturer \(\.$text) dans la boîte de réception")97    }9899    init() {}100    init(text: String) { self.text = text }101102    @MainActor103    func perform() async throws -> some IntentResult & ReturnsValue<NoteEntity> & ProvidesDialog {104        let app = try AppRuntime.model()105        let id = try await app.capture(CaptureDraft(kind: CaptureKindName.note, text: text))106        return .result(value: try await app.noteEntity(id: id), dialog: "C’est dans ta boîte de réception.")107    }108}109110struct AskNotesIntent: AppIntent {111    static let title: LocalizedStringResource = "Demander à mes notes"112    static let description = IntentDescription("Ouvre SP Notes sur l’assistant avec ta question, prête à être envoyée.", categoryName: "Assistant")113    static let openAppWhenRun = true114115    @Parameter(title: "Question", inputOptions: String.IntentInputOptions(multiline: true), requestValueDialog: "Que veux-tu demander à tes notes ?") var question: String116117    static var parameterSummary: some ParameterSummary {118        Summary("Demander \(\.$question) à mes notes")119    }120121    init() {}122    init(question: String) { self.question = question }123124    /// The answer is never produced here: the app opens on the assistant with the question, and the user sends it.125    @MainActor126    func perform() async throws -> some IntentResult {127        AskRequests.shared.ask(question, app: try AppRuntime.model())128        return .result()129    }130}131
Files main
Menu

Theme