Sources/OrgApp/WidgetData.swift
131 lines · 4527 bytes
12 symbols in this file
1import Foundation
2import OrgCore
3import OrgPresentation
4
5/// What the widgets and controls show besides the agenda, written by the app into the app
6/// group's container as `AgendaSnapshot` is.
7public protocol WidgetData: Codable, Equatable, Sendable {
8 static var name: String { get }
9}
10
11extension WidgetData {
12 /// `Widgets/<name>` in the app group's container; nil without the group.
13 public static func file(group: String = CaptureInbox.group) -> URL? {
14 AgendaSnapshot.file(group: group)?.deletingLastPathComponent().appendingPathComponent(name)
15 }
16
17 public static func read(from file: URL? = Self.file()) -> Self? {
18 guard let file, let data = try? Data(contentsOf: file) else { return nil }
19 return try? AgendaSnapshot.decoder.decode(Self.self, from: data)
20 }
21
22 /// Writes unless the file holds the same; true when it wrote.
23 @discardableResult
24 public func write(to file: URL? = Self.file()) throws -> Bool {
25 guard let file, Self.read(from: file) != self else { return false }
26 try FileManager.default.createDirectory(at: file.deletingLastPathComponent(), withIntermediateDirectories: true)
27 try AgendaSnapshot.encoder.encode(self).write(to: file, options: .atomic)
28 return true
29 }
30}
31
32/// The agenda files' habits, whose consistency graphs the widget draws for its own day.
33public struct HabitSnapshot: WidgetData {
34 public static let name = "habits.json"
35
36 public struct Entry: Codable, Equatable, Sendable, Identifiable {
37 public var title: String
38 public var path: String
39 public var offset: Int
40 public var habit: Habit
41 public var id: String { "\(path):\(offset)" }
42
43 public init(title: String, path: String, offset: Int, habit: Habit) {
44 self.title = title
45 self.path = path
46 self.offset = offset
47 self.habit = habit
48 }
49
50 public var link: AppLink { .entry(path: path, offset: offset) }
51 }
52
53 public var habits: [Entry]
54 /// `habit-clear`, `habit-ready`, `habit-alert` and `habit-overdue`.
55 public var colors: [String: AgendaSnapshot.Hex]
56
57 public init(habits: [Entry], colors: [String: AgendaSnapshot.Hex]) {
58 self.habits = habits
59 self.colors = colors
60 }
61
62 /// `habits`, colored by `theme`.
63 public init(habits: [Entry], theme: ThemeSpec) {
64 var colors: [String: AgendaSnapshot.Hex] = [:]
65 for key in ["habit-clear", "habit-ready", "habit-alert", "habit-overdue"] {
66 colors[key] = AgendaSnapshot.Hex { theme.color(key, dark: $0) }
67 }
68 self.init(habits: habits, colors: colors)
69 }
70}
71
72/// When the flashcard positions a review would show fall due, for the Flashcards widget to
73/// count at each of its times.
74public struct FlashcardSnapshot: WidgetData {
75 public static let name = "flashcards.json"
76
77 public var due: [Date]
78
79 public init(due: [Date]) {
80 self.due = due.sorted()
81 }
82
83 public init(cards: [Flashcards.Card]) {
84 self.init(due: cards.flatMap(Flashcards.dueDates))
85 }
86
87 /// Positions due before `date`.
88 public func count(at date: Date) -> Int { due.prefix { $0 < date }.count }
89
90 /// The times after `date` the count goes up, at most `limit` of them.
91 public func changes(after date: Date, limit: Int = 24) -> [Date] {
92 Array(Set(due.filter { $0 >= date }.map { $0.addingTimeInterval(1) })).sorted().prefix(limit).map { $0 }
93 }
94}
95
96/// The running clock and the entry Clock In Last would clock into, for the clock control.
97public struct ClockSnapshot: WidgetData {
98 public static let name = "clock.json"
99
100 public var running: String?
101 public var startedAt: Date?
102 /// The most recent entry clocked, `org-clock-in-last`'s.
103 public var last: String?
104
105 public init(running: String?, startedAt: Date?, last: String?) {
106 self.running = running
107 self.startedAt = startedAt
108 self.last = last
109 }
110}
111
112/// The capture templates' keys and names, for choosing one in a widget's or control's settings.
113public struct TemplateList: WidgetData {
114 public static let name = "templates.json"
115
116 public struct Template: Codable, Equatable, Sendable {
117 public var key: String
118 public var name: String
119
120 public init(key: String, name: String) {
121 self.key = key
122 self.name = name
123 }
124 }
125
126 public var templates: [Template]
127
128 public init(templates: [Template]) {
129 self.templates = templates
130 }
131}