Sources/OrgstarWidgets/UpNextWidget.swift
93 lines · 3171 bytes
1import OrgApp
2import SwiftUI
3import WidgetKit
4
5/// The agenda on the Lock Screen: the next entry (rectangular), the next entry or the count
6/// (inline), today's open entries (circular). On the Mac, the next entry in a small widget.
7struct UpNextWidget: Widget {
8 var body: some WidgetConfiguration {
9 StaticConfiguration(kind: "sh.krz.orgstar.up-next", provider: AgendaProvider()) { entry in
10 UpNextView(entry: entry)
11 }
12 .configurationDisplayName("Up Next")
13 .description("The next agenda entry, or how many are open today.")
14 #if os(macOS)
15 .supportedFamilies([.systemSmall])
16 #else
17 .supportedFamilies([.accessoryRectangular, .accessoryInline, .accessoryCircular])
18 #endif
19 }
20}
21
22struct UpNextView: View {
23 let entry: AgendaEntry
24 @Environment(\.widgetFamily) private var family
25
26 private var next: AgendaSnapshot.Row? { entry.snapshot?.upcoming(at: entry.date).first }
27 private var open: Int { entry.snapshot?.openCount(at: entry.date) ?? 0 }
28
29 var body: some View {
30 content
31 .widgetURL((next?.link ?? .agenda).url)
32 #if os(macOS)
33 .containerBackground(for: .widget) { Color(nsColor: .windowBackgroundColor) }
34 #else
35 .containerBackground(for: .widget) { Color.clear }
36 #endif
37 }
38
39 @ViewBuilder
40 private var content: some View {
41 switch family {
42 case .accessoryInline:
43 if let next {
44 Text("\(next.gutter) \(next.title)")
45 } else {
46 Text(open == 0 ? "Agenda clear" : "\(open) open today")
47 }
48 case .accessoryCircular:
49 ZStack {
50 AccessoryWidgetBackground()
51 VStack(spacing: -2) {
52 Text("\(open)").font(.title2.weight(.semibold)).widgetAccentable()
53 Text("open").font(.caption2)
54 }
55 }
56 .accessibilityLabel("\(open) open today")
57 default:
58 if let next {
59 VStack(alignment: .leading, spacing: 0) {
60 Text(next.gutter).font(.headline).widgetAccentable().lineLimit(1)
61 Text(next.title).font(.body).lineLimit(2)
62 }
63 .frame(maxWidth: .infinity, alignment: .leading)
64 } else {
65 VStack(alignment: .leading, spacing: 0) {
66 Text("Agenda").font(.headline).widgetAccentable()
67 Text(entry.snapshot == nil ? !widgetsLocked ? "Open orgstar" : "Needs orgstar+" : "Nothing left today").font(.body)
68 }
69 .frame(maxWidth: .infinity, alignment: .leading)
70 }
71 }
72 }
73}
74
75#if os(iOS)
76#Preview("Rectangular", as: .accessoryRectangular) {
77 UpNextWidget()
78} timeline: {
79 AgendaEntry(date: .now, snapshot: .sample(.now))
80}
81
82#Preview("Inline", as: .accessoryInline) {
83 UpNextWidget()
84} timeline: {
85 AgendaEntry(date: .now, snapshot: .sample(.now))
86}
87
88#Preview("Circular", as: .accessoryCircular) {
89 UpNextWidget()
90} timeline: {
91 AgendaEntry(date: .now, snapshot: .sample(.now))
92}
93#endif