Sources/OrgstarWidgets/AgendaTimeline.swift
108 lines · 5055 bytes
1import OrgApp
2import SwiftUI
3import WidgetKit
4
5/// Whether the app lacks Pro, which the widgets need. iOS and the App Store Mac app share the flag
6/// with the widgets; the Developer ID Mac app stops writing their data instead.
7var widgetsLocked: Bool {
8 #if os(iOS) || APPSTORE
9 !ProAccess.isUnlockedNow
10 #else
11 false
12 #endif
13}
14
15struct AgendaEntry: TimelineEntry {
16 let date: Date
17 /// Nil until the app has written one.
18 let snapshot: AgendaSnapshot?
19}
20
21/// The app's agenda snapshot, with an entry for each time what's left of the day changes.
22struct AgendaProvider: TimelineProvider {
23 func placeholder(in context: Context) -> AgendaEntry {
24 AgendaEntry(date: .now, snapshot: .sample(.now))
25 }
26
27 func getSnapshot(in context: Context, completion: @escaping (AgendaEntry) -> Void) {
28 let snapshot = Self.load()
29 completion(AgendaEntry(date: .now, snapshot: snapshot ?? (context.isPreview ? .sample(.now) : nil)))
30 }
31
32 func getTimeline(in context: Context, completion: @escaping (Timeline<AgendaEntry>) -> Void) {
33 completion(Self.timeline(Self.load(), now: .now))
34 }
35
36 static func load() -> AgendaSnapshot? {
37 guard !widgetsLocked else { return nil }
38 return AgendaSnapshot.file().flatMap(AgendaSnapshot.read)
39 }
40
41 static func timeline(_ snapshot: AgendaSnapshot?, now: Date, calendar: Calendar = .current) -> Timeline<AgendaEntry> {
42 let changes = snapshot?.changes(after: now, calendar: calendar)
43 ?? [calendar.date(byAdding: .day, value: 1, to: calendar.startOfDay(for: now))!]
44 let entries = ([now] + changes).map { AgendaEntry(date: $0, snapshot: snapshot) }
45 return Timeline(entries: entries, policy: .atEnd)
46 }
47}
48
49extension AgendaSnapshot.Hex {
50 func color(_ scheme: ColorScheme) -> Color {
51 Self.parse(scheme == .dark ? dark : light) ?? .primary
52 }
53
54 /// `#rrggbb` or `#rrggbbaa`.
55 static func parse(_ hex: String) -> Color? {
56 let digits = hex.dropFirst()
57 guard hex.first == "#", digits.count == 6 || digits.count == 8, let value = UInt64(digits, radix: 16) else { return nil }
58 let full = digits.count == 6 ? value << 8 | 0xFF : value
59 func part(_ shift: UInt64) -> Double { Double(full >> shift & 0xFF) / 255 }
60 return Color(.sRGB, red: part(24), green: part(16), blue: part(8), opacity: part(0))
61 }
62}
63
64extension AgendaSnapshot {
65 /// Nil where the theme leaves `key` to the system in `scheme`.
66 func color(_ key: String, _ scheme: ColorScheme) -> Color? {
67 colors[key].flatMap { AgendaSnapshot.Hex.parse(scheme == .dark ? $0.dark : $0.light) }
68 }
69
70 /// A day for the gallery and previews.
71 static func sample(_ now: Date, calendar: Calendar = .current) -> AgendaSnapshot {
72 let midnight = calendar.startOfDay(for: now)
73 func at(_ hour: Int, _ minute: Int = 0) -> Date { midnight.addingTimeInterval(Double(hour * 3600 + minute * 60)) }
74 let red = Hex(light: "#d70015", dark: "#ff6961")
75 let orange = Hex(light: "#c93400", dark: "#ff9f0a")
76 let gray = Hex(light: "#6c6c70", dark: "#98989d")
77 let blue = Hex(light: "#0a66d8", dark: "#409cff")
78 func row(
79 _ title: String, _ gutter: String, status: String = "normal", todo: String? = nil, todoColor: Hex? = nil,
80 priority: String? = nil, category: String = "work", start: Date? = nil, end: Date? = nil
81 ) -> Row {
82 Row(
83 id: title, gutter: gutter, status: status, title: title, category: category, todo: todo, priority: priority, isDone: false,
84 isEvent: false, start: start, end: end, path: nil, offset: nil,
85 statusColor: status == "overdue" ? red : status == "due-soon" ? orange : gray, todoColor: todoColor,
86 priorityColor: priority == nil ? nil : red, categoryColor: Hex(light: "#2e6b8f", dark: "#7fb8d9")
87 )
88 }
89 let today = Day(
90 day: 0, date: midnight, header: "Today",
91 overdue: [row("Send the forms", "3d late", status: "overdue", todo: "TODO", todoColor: red, category: "home")],
92 rows: [
93 row("Standup", "09:30", start: at(9, 30)),
94 row("Review the draft", "11:00–12:00", todo: "NEXT", todoColor: blue, priority: "A", start: at(11), end: at(12)),
95 row("Dentist", "14:00", category: "home", start: at(14)),
96 row("File expenses", "Due today", status: "due-soon", todo: "TODO", todoColor: red),
97 ]
98 )
99 let tomorrow = Day(
100 day: 1, date: calendar.date(byAdding: .day, value: 1, to: midnight)!, header: "Tomorrow", overdue: [],
101 rows: [row("Plan the week", "Scheduled", todo: "TODO", todoColor: red)]
102 )
103 return AgendaSnapshot(
104 generated: now, days: [today, tomorrow],
105 colors: ["agenda-today": Hex(light: "#007aff", dark: "#0a84ff"), "agenda-time": gray, "foreground": Hex(light: "#1f2328", dark: "#e6edf3")]
106 )
107 }
108}