Sources/OrgstarWidgets/Controls.swift

100 lines · 4172 bytes

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