Sources/OrgstarWatchWidgets/WatchWidgets.swift
132 lines · 5106 bytes
1#if os(watchOS)
2import OrgWatchData
3import SwiftUI
4import WidgetKit
5
6/// Complications: the next agenda entry, and the running clock.
7@main
8struct OrgstarWatchWidgets: WidgetBundle {
9 var body: some Widget {
10 NextEntryComplication()
11 ClockComplication()
12 }
13}
14
15struct WatchEntry: TimelineEntry {
16 let date: Date
17 let agenda: WatchAgenda?
18}
19
20/// The agenda the watch app last got, with an entry at each change of what's next.
21struct WatchProvider: TimelineProvider {
22 func placeholder(in context: Context) -> WatchEntry { WatchEntry(date: .now, agenda: nil) }
23
24 func getSnapshot(in context: Context, completion: @escaping (WatchEntry) -> Void) {
25 completion(WatchEntry(date: .now, agenda: WatchAgenda.read()))
26 }
27
28 func getTimeline(in context: Context, completion: @escaping (Timeline<WatchEntry>) -> Void) {
29 let agenda = WatchAgenda.read()
30 let now = Date()
31 var dates = [now]
32 for row in agenda?.rows ?? [] {
33 if let end = row.end ?? row.start, end > now { dates.append(end.addingTimeInterval(60)) }
34 }
35 if let midnight = Calendar.current.date(byAdding: .day, value: 1, to: Calendar.current.startOfDay(for: now)) { dates.append(midnight) }
36 let entries = Set(dates).sorted().map { WatchEntry(date: $0, agenda: agenda) }
37 completion(Timeline(entries: entries, policy: .never))
38 }
39}
40
41struct NextEntryComplication: Widget {
42 var body: some WidgetConfiguration {
43 StaticConfiguration(kind: "sh.krz.orgstar.watch.next", provider: WatchProvider()) { entry in
44 NextEntryView(entry: entry).containerBackground(for: .widget) { Color.clear }
45 }
46 .configurationDisplayName("Up Next")
47 .description("The next agenda entry, or how many are open today.")
48 .supportedFamilies([.accessoryRectangular, .accessoryInline, .accessoryCircular, .accessoryCorner])
49 }
50}
51
52struct NextEntryView: View {
53 let entry: WatchEntry
54 @Environment(\.widgetFamily) private var family
55
56 private var next: WatchAgenda.Entry? { entry.agenda?.next(at: entry.date) }
57 private var open: Int { entry.agenda?.openCount(at: entry.date) ?? 0 }
58
59 var body: some View {
60 switch family {
61 case .accessoryInline:
62 Text(next.map { "\($0.gutter) \($0.title)" } ?? (open == 0 ? "Agenda clear" : "\(open) open today"))
63 case .accessoryCircular:
64 ZStack {
65 AccessoryWidgetBackground()
66 VStack(spacing: -2) {
67 Text("\(open)").font(.title3.weight(.semibold)).widgetAccentable()
68 Text("open").font(.caption2)
69 }
70 }
71 case .accessoryCorner:
72 Text("\(open)").font(.title3.weight(.semibold)).widgetLabel { Text(next?.title ?? "Agenda clear") }
73 default:
74 VStack(alignment: .leading, spacing: 0) {
75 Text(next?.gutter ?? "Agenda").font(.headline).widgetAccentable().lineLimit(1)
76 Text(next?.title ?? (entry.agenda == nil ? "Open orgstar" : "Nothing left today")).lineLimit(2)
77 }
78 .frame(maxWidth: .infinity, alignment: .leading)
79 }
80 }
81}
82
83struct ClockComplication: Widget {
84 var body: some WidgetConfiguration {
85 StaticConfiguration(kind: "sh.krz.orgstar.watch.clock", provider: WatchProvider()) { entry in
86 ClockView(entry: entry).containerBackground(for: .widget) { Color.clear }
87 }
88 .configurationDisplayName("Clock")
89 .description("The running clock and its time so far.")
90 .supportedFamilies([.accessoryRectangular, .accessoryInline, .accessoryCircular])
91 }
92}
93
94struct ClockView: View {
95 let entry: WatchEntry
96 @Environment(\.widgetFamily) private var family
97
98 var body: some View {
99 let clock = entry.agenda?.clock
100 switch family {
101 case .accessoryInline:
102 if let clock { Text("\(clock.heading) \(clock.startedAt, style: .timer)") } else { Text("Not clocked in") }
103 case .accessoryCircular:
104 ZStack {
105 AccessoryWidgetBackground()
106 if let clock {
107 Text(clock.startedAt, style: .timer).font(.caption.monospacedDigit()).multilineTextAlignment(.center).widgetAccentable()
108 } else {
109 Image(systemName: "stopwatch").font(.title3)
110 }
111 }
112 default:
113 VStack(alignment: .leading, spacing: 0) {
114 if let clock {
115 Text(clock.startedAt, style: .timer).font(.headline.monospacedDigit()).widgetAccentable()
116 Text(clock.heading).lineLimit(2)
117 } else {
118 Text("Clock").font(.headline).widgetAccentable()
119 Text("Not clocked in").foregroundStyle(.secondary)
120 }
121 }
122 .frame(maxWidth: .infinity, alignment: .leading)
123 }
124 }
125}
126#else
127/// The complications build for watchOS only; elsewhere the product does nothing.
128@main
129enum OrgstarWatchWidgets {
130 static func main() {}
131}
132#endif