Sources/OrgApp/WidgetData.swift

524b5afe95ec3f3e3d1daba761a9e181f09cf88a
orgstar/Sources/OrgApp/WidgetData.swift history · blame · raw

107 lines · 3716 bytes

  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/// The running clock and the entry Clock In Last would clock into, for the clock control.
 73public struct ClockSnapshot: WidgetData {
 74    public static let name = "clock.json"
 75
 76    public var running: String?
 77    public var startedAt: Date?
 78    /// The most recent entry clocked, `org-clock-in-last`'s.
 79    public var last: String?
 80
 81    public init(running: String?, startedAt: Date?, last: String?) {
 82        self.running = running
 83        self.startedAt = startedAt
 84        self.last = last
 85    }
 86}
 87
 88/// The capture templates' keys and names, for choosing one in a widget's or control's settings.
 89public struct TemplateList: WidgetData {
 90    public static let name = "templates.json"
 91
 92    public struct Template: Codable, Equatable, Sendable {
 93        public var key: String
 94        public var name: String
 95
 96        public init(key: String, name: String) {
 97            self.key = key
 98            self.name = name
 99        }
100    }
101
102    public var templates: [Template]
103
104    public init(templates: [Template]) {
105        self.templates = templates
106    }
107}