Sources/OrgstarWidgets/TodayWidget.swift
175 lines · 6976 bytes
1import OrgApp
2import SwiftUI
3import WidgetKit
4
5/// Today's agenda on the Home Screen: what's next (small), today's list (medium), today and
6/// tomorrow (large).
7struct TodayWidget: Widget {
8 var body: some WidgetConfiguration {
9 StaticConfiguration(kind: "sh.krz.orgstar.today", provider: AgendaProvider()) { entry in
10 TodayView(entry: entry)
11 }
12 .configurationDisplayName("Today")
13 .description("What's left of today's agenda.")
14 .supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
15 }
16}
17
18struct TodayView: View {
19 let entry: AgendaEntry
20 @Environment(\.widgetFamily) private var family
21 @Environment(\.colorScheme) private var scheme
22
23 var body: some View {
24 Group {
25 if let snapshot = entry.snapshot {
26 content(snapshot)
27 } else {
28 Text(!widgetsLocked ? "Open orgstar to show your agenda here." : "Widgets need orgstar+.")
29 .font(.caption)
30 .foregroundStyle(.secondary)
31 .frame(maxWidth: .infinity, maxHeight: .infinity)
32 }
33 }
34 .widgetURL(AppLink.agenda.url)
35 .containerBackground(for: .widget) {
36 #if os(macOS)
37 entry.snapshot?.color("agenda-background", scheme) ?? Color(nsColor: .windowBackgroundColor)
38 #else
39 entry.snapshot?.color("agenda-background", scheme) ?? Color(uiColor: .systemBackground)
40 #endif
41 }
42 }
43
44 @ViewBuilder
45 private func content(_ snapshot: AgendaSnapshot) -> some View {
46 let upcoming = snapshot.upcoming(at: entry.date)
47 VStack(alignment: .leading, spacing: family == .systemSmall ? 4 : 6) {
48 header(snapshot)
49 switch family {
50 case .systemSmall:
51 rows(Array(upcoming.prefix(3)), snapshot, compact: true, empty: "Nothing left today")
52 case .systemLarge:
53 rows(Array(upcoming.prefix(6)), snapshot, compact: false, empty: "Nothing left today")
54 if let tomorrow = snapshot.day(after: entry.date) {
55 let next = (tomorrow.rows + tomorrow.overdue).filter { !$0.isDone }
56 Divider()
57 Text("Tomorrow").font(.caption.weight(.semibold)).foregroundStyle(.secondary)
58 rows(Array(next.prefix(max(2, 9 - min(upcoming.count, 6)))), snapshot, compact: false, empty: "Nothing scheduled")
59 }
60 default:
61 rows(Array(upcoming.prefix(4)), snapshot, compact: false, empty: "Nothing left today")
62 }
63 Spacer(minLength: 0)
64 }
65 .frame(maxWidth: .infinity, alignment: .leading)
66 }
67
68 private func header(_ snapshot: AgendaSnapshot) -> some View {
69 let open = snapshot.openCount(at: entry.date)
70 let overdue = snapshot.overdueCount(at: entry.date)
71 return HStack(alignment: .firstTextBaseline, spacing: 6) {
72 Text(entry.date, format: family == .systemSmall ? .dateTime.weekday(.abbreviated).day() : .dateTime.weekday(.wide).day().month())
73 .font(.subheadline.weight(.semibold))
74 .foregroundStyle(snapshot.color("agenda-today", scheme) ?? .accentColor)
75 Spacer(minLength: 0)
76 if overdue > 0, family != .systemSmall {
77 Text("\(overdue) overdue")
78 .font(.caption2.weight(.semibold))
79 .foregroundStyle(snapshot.day(at: entry.date)?.overdue.first?.statusColor?.color(scheme) ?? .red)
80 }
81 Text(family == .systemSmall ? "\(open)" : "\(open) open")
82 .font(.caption2.weight(.semibold))
83 .foregroundStyle(.secondary)
84 }
85 }
86
87 @ViewBuilder
88 private func rows(_ rows: [AgendaSnapshot.Row], _ snapshot: AgendaSnapshot, compact: Bool, empty: LocalizedStringKey) -> some View {
89 if rows.isEmpty {
90 Text(empty).font(.caption).foregroundStyle(.secondary)
91 }
92 ForEach(rows) { row in
93 if compact {
94 SmallRow(row: row)
95 } else if let link = row.link {
96 Link(destination: link.url) { WideRow(row: row) }
97 } else {
98 WideRow(row: row)
99 }
100 }
101 }
102}
103
104/// The gutter over the title.
105struct SmallRow: View {
106 let row: AgendaSnapshot.Row
107 @Environment(\.colorScheme) private var scheme
108
109 var body: some View {
110 VStack(alignment: .leading, spacing: 0) {
111 Text(row.gutter)
112 .font(.caption2.weight(.medium).monospacedDigit())
113 .foregroundStyle(row.statusColor?.color(scheme) ?? .secondary)
114 Text(row.title).font(.caption).lineLimit(1)
115 }
116 }
117}
118
119/// The gutter, keyword and priority badges, the title and the category's dot.
120struct WideRow: View {
121 let row: AgendaSnapshot.Row
122 @Environment(\.colorScheme) private var scheme
123 @ScaledMetric(relativeTo: .caption2) private var gutterWidth: CGFloat = 72
124 @ScaledMetric(relativeTo: .caption2) private var priorityRing: CGFloat = 16
125 @ScaledMetric(relativeTo: .caption) private var categoryDot: CGFloat = 6
126
127 var body: some View {
128 HStack(spacing: 6) {
129 Text(row.gutter)
130 .font(.caption2.weight(.medium).monospacedDigit())
131 .foregroundStyle(row.statusColor?.color(scheme) ?? .secondary)
132 .lineLimit(1)
133 .frame(width: gutterWidth, alignment: .leading)
134 if let todo = row.todo {
135 let color = row.todoColor?.color(scheme) ?? .secondary
136 Text(todo)
137 .font(.caption2.weight(.bold))
138 .foregroundStyle(color)
139 .padding(.horizontal, 4)
140 .padding(.vertical, 1)
141 .background(color.opacity(0.14), in: Capsule())
142 }
143 if let priority = row.priority {
144 let color = row.priorityColor?.color(scheme) ?? .orange
145 Text(priority)
146 .font(.caption2.weight(.bold))
147 .foregroundStyle(color)
148 .frame(width: priorityRing, height: priorityRing)
149 .overlay(Circle().strokeBorder(color, lineWidth: 1))
150 }
151 Text(row.title).font(.caption).lineLimit(1)
152 Spacer(minLength: 0)
153 Circle().fill(row.categoryColor?.color(scheme) ?? .secondary).frame(width: categoryDot, height: categoryDot)
154 }
155 }
156}
157
158#Preview("Small", as: .systemSmall) {
159 TodayWidget()
160} timeline: {
161 AgendaEntry(date: .now, snapshot: .sample(.now))
162}
163
164#Preview("Medium", as: .systemMedium) {
165 TodayWidget()
166} timeline: {
167 AgendaEntry(date: .now, snapshot: .sample(.now))
168}
169
170#Preview("Large", as: .systemLarge) {
171 TodayWidget()
172} timeline: {
173 AgendaEntry(date: .now, snapshot: .sample(.now))
174 AgendaEntry(date: .now, snapshot: nil)
175}