Sources/OrgstarWidgets/Controls.swift

102 lines · 4191 bytes

11 symbols in this file
  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 ?? "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 ?? "Clocked in" : clock.last.map { "In: \($0)" } ?? "No recent clock",
 27                      systemImage: isOn ? "stopwatch.fill" : "stopwatch")
 28            }
 29            .tint(.orange)
 30        }
 31        .displayName("Clock")
 32        .description("Clocks out of the running clock, or back in to the entry clocked last.")
 33    }
 34}
 35
 36struct ClockValueProvider: ControlValueProvider {
 37    var previewValue: ClockSnapshot { ClockSnapshot(running: nil, startedAt: nil, last: "Review the draft") }
 38
 39    func currentValue() async throws -> ClockSnapshot {
 40        ClockSnapshot.read() ?? ClockSnapshot(running: nil, startedAt: nil, last: nil)
 41    }
 42}
 43
 44/// Capture on the Lock Screen: a button that opens the capture sheet with a chosen template.
 45struct CaptureWidget: Widget {
 46    var body: some WidgetConfiguration {
 47        AppIntentConfiguration(kind: "sh.krz.orgstar.capture", intent: CaptureWidgetConfiguration.self, provider: CaptureProvider()) { entry in
 48            CaptureView(entry: entry)
 49        }
 50        .configurationDisplayName("Capture")
 51        .description("Opens orgstar's capture sheet, with the template you choose.")
 52        .supportedFamilies([.accessoryCircular, .accessoryRectangular])
 53    }
 54}
 55
 56struct CaptureEntry: TimelineEntry {
 57    let date: Date
 58    let template: CaptureTemplateEntity?
 59}
 60
 61struct CaptureProvider: AppIntentTimelineProvider {
 62    func placeholder(in context: Context) -> CaptureEntry {
 63        CaptureEntry(date: .now, template: nil)
 64    }
 65
 66    func snapshot(for configuration: CaptureWidgetConfiguration, in context: Context) async -> CaptureEntry {
 67        CaptureEntry(date: .now, template: configuration.template)
 68    }
 69
 70    func timeline(for configuration: CaptureWidgetConfiguration, in context: Context) async -> Timeline<CaptureEntry> {
 71        Timeline(entries: [CaptureEntry(date: .now, template: configuration.template)], policy: .never)
 72    }
 73}
 74
 75struct CaptureView: View {
 76    let entry: CaptureEntry
 77    @Environment(\.widgetFamily) private var family
 78
 79    var body: some View {
 80        Group {
 81            if family == .accessoryCircular {
 82                ZStack {
 83                    AccessoryWidgetBackground()
 84                    Image(systemName: "square.and.pencil").font(.title2)
 85                }
 86            } else {
 87                HStack(spacing: 8) {
 88                    Image(systemName: "square.and.pencil").font(.title3)
 89                    VStack(alignment: .leading, spacing: 0) {
 90                        Text("Capture").font(.headline)
 91                        Text(entry.template?.name ?? "First template").font(.caption).foregroundStyle(.secondary).lineLimit(1)
 92                    }
 93                    Spacer(minLength: 0)
 94                }
 95            }
 96        }
 97        .widgetURL(AppLink.capture(template: entry.template?.id).url)
 98        .containerBackground(for: .widget) { Color.clear }
 99        .accessibilityLabel("Capture" + (entry.template.map { " with \($0.name)" } ?? ""))
100    }
101}
102#endif