Sources/OrgstarWidgets/TodayWidget.swift

design-themes-widgets
orgstar/Sources/OrgstarWidgets/TodayWidget.swift history · blame · raw

189 lines · 7683 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                .widgetAccentable()
 76            Spacer(minLength: 0)
 77            if overdue > 0, family != .systemSmall {
 78                Text("\(overdue) overdue")
 79                    .font(.caption2.weight(.semibold))
 80                    .foregroundStyle(snapshot.day(at: entry.date)?.overdue.first?.statusColor?.color(scheme) ?? .red)
 81            }
 82            Text(family == .systemSmall ? "\(open)" : "\(open) open")
 83                .font(.caption2.weight(.semibold))
 84                .foregroundStyle(.secondary)
 85        }
 86    }
 87
 88    @ViewBuilder
 89    private func rows(_ rows: [AgendaSnapshot.Row], _ snapshot: AgendaSnapshot, compact: Bool, empty: LocalizedStringKey) -> some View {
 90        if rows.isEmpty {
 91            Text(empty).font(.caption).foregroundStyle(.secondary)
 92        }
 93        ForEach(rows) { row in
 94            if compact {
 95                SmallRow(row: row)
 96            } else if let link = row.link {
 97                Link(destination: link.url) { WideRow(row: row) }
 98            } else {
 99                WideRow(row: row)
100            }
101        }
102    }
103}
104
105/// The gutter over the title.
106struct SmallRow: View {
107    let row: AgendaSnapshot.Row
108    @Environment(\.colorScheme) private var scheme
109
110    var body: some View {
111        VStack(alignment: .leading, spacing: 0) {
112            Text(row.gutter)
113                .font(.caption2.weight(.medium).monospacedDigit())
114                .foregroundStyle(row.statusColor?.color(scheme) ?? .secondary)
115            Text(row.title).font(.caption).lineLimit(1)
116        }
117        .accessibilityElement(children: .combine)
118    }
119}
120
121/// The gutter, keyword and priority badges, the title and the category's dot.
122struct WideRow: View {
123    let row: AgendaSnapshot.Row
124    @Environment(\.colorScheme) private var scheme
125    @ScaledMetric(relativeTo: .caption2) private var gutterWidth: CGFloat = 72
126    @ScaledMetric(relativeTo: .caption2) private var priorityRing: CGFloat = 16
127    @ScaledMetric(relativeTo: .caption) private var categoryDot: CGFloat = 6
128
129    var body: some View {
130        HStack(spacing: 6) {
131            Text(row.gutter)
132                .font(.caption2.weight(.medium).monospacedDigit())
133                .foregroundStyle(row.statusColor?.color(scheme) ?? .secondary)
134                .lineLimit(1)
135                .frame(width: gutterWidth, alignment: .leading)
136            if let todo = row.todo {
137                let color = row.todoColor?.color(scheme) ?? .secondary
138                Text(todo)
139                    .font(.caption2.weight(.bold))
140                    .foregroundStyle(color)
141                    .padding(.horizontal, 4)
142                    .padding(.vertical, 1)
143                    .background(color.opacity(0.14), in: Capsule())
144            }
145            if let priority = row.priority {
146                let color = row.priorityColor?.color(scheme) ?? .orange
147                Text(priority)
148                    .font(.caption2.weight(.bold))
149                    .foregroundStyle(color)
150                    .frame(width: priorityRing, height: priorityRing)
151                    .overlay(Circle().strokeBorder(color, lineWidth: 1))
152            }
153            Text(row.title).font(.caption).lineLimit(1)
154            Spacer(minLength: 0)
155            Circle().fill(row.categoryColor?.color(scheme) ?? .secondary).frame(width: categoryDot, height: categoryDot)
156        }
157        .accessibilityElement(children: .ignore)
158        .accessibilityLabel(label)
159    }
160
161    /// The time or status, keyword, priority, title and category, as one phrase.
162    private var label: Text {
163        var parts = [Text(verbatim: row.gutter)]
164        if let todo = row.todo { parts.append(Text(verbatim: todo)) }
165        if let priority = row.priority { parts.append(Text("Priority \(priority)")) }
166        parts.append(Text(verbatim: row.title))
167        if !row.category.isEmpty { parts.append(Text(verbatim: row.category)) }
168        return parts.dropFirst().reduce(parts[0]) { $0 + Text(verbatim: ", ") + $1 }
169    }
170}
171
172#Preview("Small", as: .systemSmall) {
173    TodayWidget()
174} timeline: {
175    AgendaEntry(date: .now, snapshot: .sample(.now))
176}
177
178#Preview("Medium", as: .systemMedium) {
179    TodayWidget()
180} timeline: {
181    AgendaEntry(date: .now, snapshot: .sample(.now))
182}
183
184#Preview("Large", as: .systemLarge) {
185    TodayWidget()
186} timeline: {
187    AgendaEntry(date: .now, snapshot: .sample(.now))
188    AgendaEntry(date: .now, snapshot: nil)
189}