Sources/OrgstarWidgets/Controls.swift
103 lines · 4332 bytes
1#if os(iOS)
2import AppIntents
3import OrgApp
4import SwiftUI
5import WidgetKit
6
7/// Capture from Control Center, the Lock Screen or the Action button, with a chosen template.
8struct CaptureControl: ControlWidget {
9 var body: some ControlWidgetConfiguration {
10 AppIntentControlConfiguration(kind: "sh.krz.orgstar.capture-control", intent: CaptureControlConfiguration.self) { configuration in
11 ControlWidgetButton(action: OpenCaptureIntent(template: configuration.template)) {
12 Label(configuration.template?.name ?? String(localized: "Capture"), systemImage: "square.and.pencil")
13 }
14 }
15 .displayName("Capture")
16 .description("Opens orgstar's capture sheet, with the template you choose.")
17 }
18}
19
20/// The clock: on while one runs, showing its entry; off shows the entry clocked last, which
21/// turning it on clocks in to.
22struct ClockControl: ControlWidget {
23 var body: some ControlWidgetConfiguration {
24 StaticControlConfiguration(kind: "sh.krz.orgstar.clock-control", provider: ClockValueProvider()) { clock in
25 ControlWidgetToggle("Clock", isOn: clock.running != nil, action: ClockToggleIntent()) { isOn in
26 Label(isOn ? clock.running ?? String(localized: "Clocked in")
27 : clock.last.map { String(localized: "Clock In to \($0)") } ?? String(localized: "No recent clock"),
28 systemImage: isOn ? "stopwatch.fill" : "stopwatch")
29 }
30 .tint(.orange)
31 }
32 .displayName("Clock")
33 .description("Clocks out of the running clock, or back in to the entry clocked last.")
34 }
35}
36
37struct ClockValueProvider: ControlValueProvider {
38 var previewValue: ClockSnapshot { ClockSnapshot(running: nil, startedAt: nil, last: "Review the draft") }
39
40 func currentValue() async throws -> ClockSnapshot {
41 ClockSnapshot.read() ?? ClockSnapshot(running: nil, startedAt: nil, last: nil)
42 }
43}
44
45/// Capture on the Lock Screen: a button that opens the capture sheet with a chosen template.
46struct CaptureWidget: Widget {
47 var body: some WidgetConfiguration {
48 AppIntentConfiguration(kind: "sh.krz.orgstar.capture", intent: CaptureWidgetConfiguration.self, provider: CaptureProvider()) { entry in
49 CaptureView(entry: entry)
50 }
51 .configurationDisplayName("Capture")
52 .description("Opens orgstar's capture sheet, with the template you choose.")
53 .supportedFamilies([.accessoryCircular, .accessoryRectangular])
54 }
55}
56
57struct CaptureEntry: TimelineEntry {
58 let date: Date
59 let template: CaptureTemplateEntity?
60}
61
62struct CaptureProvider: AppIntentTimelineProvider {
63 func placeholder(in context: Context) -> CaptureEntry {
64 CaptureEntry(date: .now, template: nil)
65 }
66
67 func snapshot(for configuration: CaptureWidgetConfiguration, in context: Context) async -> CaptureEntry {
68 CaptureEntry(date: .now, template: configuration.template)
69 }
70
71 func timeline(for configuration: CaptureWidgetConfiguration, in context: Context) async -> Timeline<CaptureEntry> {
72 Timeline(entries: [CaptureEntry(date: .now, template: configuration.template)], policy: .never)
73 }
74}
75
76struct CaptureView: View {
77 let entry: CaptureEntry
78 @Environment(\.widgetFamily) private var family
79
80 var body: some View {
81 Group {
82 if family == .accessoryCircular {
83 ZStack {
84 AccessoryWidgetBackground()
85 Image(systemName: "square.and.pencil").font(.title2)
86 }
87 } else {
88 HStack(spacing: 8) {
89 Image(systemName: "square.and.pencil").font(.title3)
90 VStack(alignment: .leading, spacing: 0) {
91 Text("Capture").font(.headline)
92 Text(entry.template?.name ?? String(localized: "First template")).font(.caption).foregroundStyle(.secondary).lineLimit(1)
93 }
94 Spacer(minLength: 0)
95 }
96 }
97 }
98 .widgetURL(AppLink.capture(template: entry.template?.id).url)
99 .containerBackground(for: .widget) { Color.clear }
100 .accessibilityLabel(entry.template.map { Text("Capture with \($0.name)") } ?? Text("Capture"))
101 }
102}
103#endif