Sources/OrgApp/WidgetIntents.swift

524b5afe95ec3f3e3d1daba761a9e181f09cf88a
orgstar/Sources/OrgApp/WidgetIntents.swift history · blame · raw

103 lines · 3507 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        let handler = await MainActor.run { Self.handler }
 77        await handler?(value)
 78        return .result()
 79    }
 80}
 81
 82/// The capture control's settings.
 83public struct CaptureControlConfiguration: ControlConfigurationIntent {
 84    public static let title: LocalizedStringResource = "Capture"
 85    public static let description = IntentDescription("The template to capture with.")
 86
 87    @Parameter(title: "Template")
 88    public var template: CaptureTemplateEntity?
 89
 90    public init() {}
 91}
 92
 93/// The Lock Screen capture widget's settings.
 94public struct CaptureWidgetConfiguration: WidgetConfigurationIntent {
 95    public static let title: LocalizedStringResource = "Capture"
 96    public static let description = IntentDescription("The template to capture with.")
 97
 98    @Parameter(title: "Template")
 99    public var template: CaptureTemplateEntity?
100
101    public init() {}
102}
103#endif