Sources/OrgApp/WidgetIntents.swift
104 lines · 3571 bytes
1#if os(iOS)
2import AppIntents
3import Foundation
4
5/// A capture template, chosen in a widget's or control's settings from the list the app wrote.
6public struct CaptureTemplateEntity: AppEntity {
7 public static let typeDisplayRepresentation: TypeDisplayRepresentation = "Capture Template"
8 public static let defaultQuery = Query()
9
10 public var id: String
11 public var name: String
12
13 public init(id: String, name: String) {
14 self.id = id
15 self.name = name
16 }
17
18 public var displayRepresentation: DisplayRepresentation { DisplayRepresentation(title: "\(name)") }
19
20 public struct Query: EntityQuery {
21 public init() {}
22
23 private var all: [CaptureTemplateEntity] {
24 (TemplateList.read()?.templates ?? []).map { CaptureTemplateEntity(id: $0.key, name: $0.name) }
25 }
26
27 public func entities(for identifiers: [String]) async throws -> [CaptureTemplateEntity] {
28 all.filter { identifiers.contains($0.id) }
29 }
30
31 public func suggestedEntities() async throws -> [CaptureTemplateEntity] { all }
32 }
33}
34
35/// Opens the capture sheet, with a template or the first one.
36public struct OpenCaptureIntent: AppIntent {
37 public static let title: LocalizedStringResource = "Capture"
38 public static let description = IntentDescription("Opens orgstar's capture sheet.")
39 public static let openAppWhenRun = true
40
41 @Parameter(title: "Template")
42 public var template: CaptureTemplateEntity?
43
44 /// Shows the capture sheet; set by the app at launch.
45 @MainActor public static var handler: (@MainActor @Sendable (String?) -> Void)?
46
47 public init() {}
48
49 public init(template: CaptureTemplateEntity?) {
50 self.template = template
51 }
52
53 @MainActor
54 public func perform() async throws -> some IntentResult {
55 Self.handler?(template?.id)
56 return .result()
57 }
58}
59
60/// The clock control: off clocks out, on clocks in to the entry clocked last
61/// (`org-clock-in-last`). It runs in the app's process.
62public struct ClockToggleIntent: SetValueIntent, LiveActivityIntent {
63 public static let title: LocalizedStringResource = "Clock In or Out"
64 public static let description = IntentDescription("Clocks out of the running clock, or in to the entry clocked last.")
65 public static let isDiscoverable = false
66
67 @Parameter(title: "Clocked In")
68 public var value: Bool
69
70 /// Clocks in (true) or out (false); set by the app at launch.
71 @MainActor public static var handler: (@MainActor @Sendable (Bool) async -> Void)?
72
73 public init() {}
74
75 public func perform() async throws -> some IntentResult {
76 guard ProAccess.isUnlockedNow else { return .result() }
77 let handler = await MainActor.run { Self.handler }
78 await handler?(value)
79 return .result()
80 }
81}
82
83/// The capture control's settings.
84public struct CaptureControlConfiguration: ControlConfigurationIntent {
85 public static let title: LocalizedStringResource = "Capture"
86 public static let description = IntentDescription("The template to capture with.")
87
88 @Parameter(title: "Template")
89 public var template: CaptureTemplateEntity?
90
91 public init() {}
92}
93
94/// The Lock Screen capture widget's settings.
95public struct CaptureWidgetConfiguration: WidgetConfigurationIntent {
96 public static let title: LocalizedStringResource = "Capture"
97 public static let description = IntentDescription("The template to capture with.")
98
99 @Parameter(title: "Template")
100 public var template: CaptureTemplateEntity?
101
102 public init() {}
103}
104#endif