Sources/OrgstarMobile/CaptureIntent.swift
49 lines · 2101 bytes
4 symbols in this file
1#if os(iOS)
2import AppIntents
3import OrgApp
4import OrgCore
5
6/// Shortcuts' "Capture to Orgstar": files text with a capture template without the capture
7/// sheet. Prompts in the template take their defaults, and `%c` is empty: reading the
8/// pasteboard would ask for permission on every run.
9struct CaptureIntent: AppIntent {
10 static let title: LocalizedStringResource = "Capture to orgstar"
11 static let description = IntentDescription("Files text with one of your capture templates.")
12
13 @Parameter(title: "Text")
14 var text: String
15
16 @Parameter(title: "Template Key", description: "A template's key in capture.toml; the first template when empty.")
17 var template: String?
18
19 struct Failure: Error, CustomLocalizedStringResourceConvertible {
20 let message: String
21 var localizedStringResource: LocalizedStringResource { "\(message)" }
22 }
23
24 @MainActor
25 func perform() async throws -> some IntentResult & ReturnsValue<String> & ProvidesDialog {
26 guard ProAccess.shared.isUnlocked else { throw Failure(message: "Shortcuts actions need orgstar+") }
27 let model = MobileModel.shared
28 guard let workspace = model.workspace else { throw Failure(message: model.failure ?? "orgstar isn't ready") }
29 let placed = model.session.capture(
30 text, templateKey: template, templates: CaptureTemplates.load().templates, workspace: workspace, clock: model.clock,
31 clipboard: "", userName: ""
32 )
33 switch placed {
34 case .failure(let error):
35 throw Failure(message: error.message)
36 case .success(let chosen):
37 for path in model.session.dirtyPaths { _ = model.session.save(path) }
38 let message = "Captured with \(chosen.name)"
39 return .result(value: message, dialog: "\(message)")
40 }
41 }
42}
43
44struct OrgstarShortcuts: AppShortcutsProvider {
45 static var appShortcuts: [AppShortcut] {
46 AppShortcut(intent: CaptureIntent(), phrases: ["Capture to \(.applicationName)"], shortTitle: "Capture", systemImageName: "tray.and.arrow.down")
47 }
48}
49#endif