Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/Intents/SPMailIntents.swift3// Description: App Intents of SP Mail — « Nouveau message », « Ouvrir la boîte de réception », « Rechercher dans SP Mail », « Nombre de non4// lus » — and their App Shortcuts (Siri, Spotlight, Shortcuts; French phrases are the source, English in AppShortcuts.xcstrings). The file5// is compiled into the apps and into the widget extensions (the interactive « Nouveau message » button); intents that open the app run in6// the app's process, where they hand a deep link to the router.7import AppIntents8import SPMailCore9import SPMailSystem1011/// Hands a destination to the running app. In an extension this is never reached: intents with `openAppWhenRun` run in the app.12@MainActor13private func openInApp(_ link: DeepLink) {14 #if !SPM_EXTENSION15 AppRuntime.model.system.router.deliver(link)16 #endif17}1819struct ComposeMessageIntent: AppIntent {20 static let title: LocalizedStringResource = "Nouveau message"21 static let description = IntentDescription("Ouvre un nouveau message dans SP Mail.")22 static let openAppWhenRun = true23 @MainActor func perform() async throws -> some IntentResult {24 openInApp(.compose(nil))25 return .result()26 }27}2829struct OpenInboxIntent: AppIntent {30 static let title: LocalizedStringResource = "Ouvrir la boîte de réception"31 static let description = IntentDescription("Ouvre la boîte de réception de SP Mail.")32 static let openAppWhenRun = true33 @MainActor func perform() async throws -> some IntentResult {34 openInApp(.inbox(mailboxId: nil))35 return .result()36 }37}3839struct SearchMailIntent: AppIntent {40 static let title: LocalizedStringResource = "Rechercher dans SP Mail"41 static let description = IntentDescription("Recherche un texte dans le courrier.")42 static let openAppWhenRun = true43 @Parameter(title: "Recherche", requestValueDialog: "Que voulez-vous rechercher ?") var query: String44 @MainActor func perform() async throws -> some IntentResult {45 openInApp(.search(query.trimmingCharacters(in: .whitespacesAndNewlines)))46 return .result()47 }48}4950/// Answers from the snapshot the app wrote after its last sync (the app is not launched, nothing is fetched). A number, never a sender or51/// a subject — the same information as the icon badge.52struct UnreadCountIntent: AppIntent {53 static let title: LocalizedStringResource = "Nombre de non lus"54 static let description = IntentDescription("Donne le nombre de messages non lus des boîtes de réception, d’après la dernière synchronisation de SP Mail.")55 static let openAppWhenRun = false56 func perform() async throws -> some IntentResult & ReturnsValue<Int> & ProvidesDialog {57 let store = SharedStore.open()58 guard store.state()?.signedIn == true, let snapshot = store.snapshot() else {59 return .result(value: 0, dialog: "Ouvrez SP Mail et connectez-vous pour connaître le nombre de messages non lus.")60 }61 let n = snapshot.totalUnread62 if n == 0 { return .result(value: 0, dialog: "Aucun message non lu.") }63 if n == 1 { return .result(value: 1, dialog: "1 message non lu.") }64 return .result(value: n, dialog: "\(n) messages non lus.")65 }66}6768#if !SPM_EXTENSION69struct SPMailShortcuts: AppShortcutsProvider {70 static var appShortcuts: [AppShortcut] {71 AppShortcut(intent: ComposeMessageIntent(), phrases: ["Nouveau message dans \(.applicationName)", "Écrire un message avec \(.applicationName)"], shortTitle: "Nouveau message", systemImageName: "square.and.pencil")72 AppShortcut(intent: OpenInboxIntent(), phrases: ["Ouvrir la boîte de réception de \(.applicationName)", "Ouvrir \(.applicationName)"], shortTitle: "Boîte de réception", systemImageName: "tray")73 AppShortcut(intent: SearchMailIntent(), phrases: ["Rechercher dans \(.applicationName)", "Chercher un message dans \(.applicationName)"], shortTitle: "Rechercher", systemImageName: "magnifyingglass")74 AppShortcut(intent: UnreadCountIntent(), phrases: ["Nombre de non lus dans \(.applicationName)", "Combien de messages non lus dans \(.applicationName)"], shortTitle: "Non lus", systemImageName: "envelope.badge")75 }76}77#endif78