Sources/OrgstarWidgets/TodayWidget.swift

172 lines · 6651 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("Open orgstar to show your agenda here.")
 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: String) -> 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
124    var body: some View {
125        HStack(spacing: 6) {
126            Text(row.gutter)
127                .font(.caption2.weight(.medium).monospacedDigit())
128                .foregroundStyle(row.statusColor?.color(scheme) ?? .secondary)
129                .lineLimit(1)
130                .frame(width: 72, alignment: .leading)
131            if let todo = row.todo {
132                let color = row.todoColor?.color(scheme) ?? .secondary
133                Text(todo)
134                    .font(.system(size: 9, weight: .bold))
135                    .foregroundStyle(color)
136                    .padding(.horizontal, 4)
137                    .padding(.vertical, 1)
138                    .background(color.opacity(0.14), in: Capsule())
139            }
140            if let priority = row.priority {
141                let color = row.priorityColor?.color(scheme) ?? .orange
142                Text(priority)
143                    .font(.system(size: 9, weight: .bold))
144                    .foregroundStyle(color)
145                    .frame(width: 14, height: 14)
146                    .overlay(Circle().strokeBorder(color, lineWidth: 1))
147            }
148            Text(row.title).font(.caption).lineLimit(1)
149            Spacer(minLength: 0)
150            Circle().fill(row.categoryColor?.color(scheme) ?? .secondary).frame(width: 6, height: 6)
151        }
152    }
153}
154
155#Preview("Small", as: .systemSmall) {
156    TodayWidget()
157} timeline: {
158    AgendaEntry(date: .now, snapshot: .sample(.now))
159}
160
161#Preview("Medium", as: .systemMedium) {
162    TodayWidget()
163} timeline: {
164    AgendaEntry(date: .now, snapshot: .sample(.now))
165}
166
167#Preview("Large", as: .systemLarge) {
168    TodayWidget()
169} timeline: {
170    AgendaEntry(date: .now, snapshot: .sample(.now))
171    AgendaEntry(date: .now, snapshot: nil)
172}