Commit 8d1ab6bd94
Verified · cmc
Layout: unified · split
Sources/OrgApp/AgendaModel.swift +7
| @@ -118,6 +118,13 @@ public final class AgendaModel { | ||
| 118 | 118 | return await Task.detached { Reminders.upcoming(all, now: now, calendar: calendar, days: days, lead: lead) }.value |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | /// The habits of the agenda files, file by file. | |
| 122 | public func habits(open: [String: String]) async -> [HabitSnapshot.Entry] { | |
| 123 | await sources(open: open).flatMap { source in | |
| 124 | source.habits.map { HabitSnapshot.Entry(title: $0.title, path: source.path, offset: $0.offset, habit: $0.habit) } | |
| 125 | } | |
| 126 | } | |
| 127 | ||
| 121 | 128 | /// Entries for the table and kanban views, and the keywords their files use. |
| 122 | 129 | public func board(open: [String: String], match: String) async -> (entries: [BoardEntry], keywords: [String]) { |
| 123 | 130 | let all = await sources(open: open) |
Sources/OrgApp/AgendaSnapshot.swift +6
| @@ -239,6 +239,8 @@ public enum AppLink: Equatable, Sendable { | ||
| 239 | 239 | case entry(path: String, offset: Int) |
| 240 | 240 | /// The entry of the running clock. |
| 241 | 241 | case clock |
| 242 | /// The capture sheet, with a template's key or the first template. | |
| 243 | case capture(template: String?) | |
| 242 | 244 | |
| 243 | 245 | public static let scheme = "orgstar" |
| 244 | 246 | |
| @@ -250,6 +252,9 @@ public enum AppLink: Equatable, Sendable { | ||
| 250 | 252 | components.host = "agenda" |
| 251 | 253 | case .clock: |
| 252 | 254 | components.host = "clock" |
| 255 | case .capture(let template): | |
| 256 | components.host = "capture" | |
| 257 | if let template { components.queryItems = [URLQueryItem(name: "template", value: template)] } | |
| 253 | 258 | case .entry(let path, let offset): |
| 254 | 259 | components.host = "entry" |
| 255 | 260 | components.queryItems = [URLQueryItem(name: "path", value: path), URLQueryItem(name: "offset", value: String(offset))] |
| @@ -263,6 +268,7 @@ public enum AppLink: Equatable, Sendable { | ||
| 263 | 268 | switch components.host { |
| 264 | 269 | case "agenda": self = .agenda |
| 265 | 270 | case "clock": self = .clock |
| 271 | case "capture": self = .capture(template: query("template")) | |
| 266 | 272 | case "entry": |
| 267 | 273 | guard let path = query("path"), let offset = query("offset").flatMap(Int.init) else { return nil } |
| 268 | 274 | self = .entry(path: path, offset: offset) |
Sources/OrgApp/WidgetData.swift added +107
| @@ -0,0 +1,107 @@ | ||
| 1 | import Foundation | |
| 2 | import OrgCore | |
| 3 | import 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. | |
| 7 | public protocol WidgetData: Codable, Equatable, Sendable { | |
| 8 | static var name: String { get } | |
| 9 | } | |
| 10 | ||
| 11 | extension 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. | |
| 33 | public 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. | |
| 73 | public 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. | |
| 89 | public 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 | } | |
Sources/OrgApp/WidgetIntents.swift added +103
| @@ -0,0 +1,103 @@ | ||
| 1 | #if os(iOS) | |
| 2 | import AppIntents | |
| 3 | import Foundation | |
| 4 | ||
| 5 | /// A capture template, chosen in a widget's or control's settings from the list the app wrote. | |
| 6 | public struct CaptureTemplateEntity: AppEntity { | |
| 7 | public static let typeDisplayRepresentation: TypeDisplayRepresentation = "Capture Template" | |
| 8 | public static let defaultQuery = Query() | |
| 9 | ||
| 10 | public var id: String | |
| 11 | public var name: String | |
| 12 | ||
| 13 | public init(id: String, name: String) { | |
| 14 | self.id = id | |
| 15 | self.name = name | |
| 16 | } | |
| 17 | ||
| 18 | public var displayRepresentation: DisplayRepresentation { DisplayRepresentation(title: "\(name)") } | |
| 19 | ||
| 20 | public struct Query: EntityQuery { | |
| 21 | public init() {} | |
| 22 | ||
| 23 | private var all: [CaptureTemplateEntity] { | |
| 24 | (TemplateList.read()?.templates ?? []).map { CaptureTemplateEntity(id: $0.key, name: $0.name) } | |
| 25 | } | |
| 26 | ||
| 27 | public func entities(for identifiers: [String]) async throws -> [CaptureTemplateEntity] { | |
| 28 | all.filter { identifiers.contains($0.id) } | |
| 29 | } | |
| 30 | ||
| 31 | public func suggestedEntities() async throws -> [CaptureTemplateEntity] { all } | |
| 32 | } | |
| 33 | } | |
| 34 | ||
| 35 | /// Opens the capture sheet, with a template or the first one. | |
| 36 | public struct OpenCaptureIntent: AppIntent { | |
| 37 | public static let title: LocalizedStringResource = "Capture" | |
| 38 | public static let description = IntentDescription("Opens orgstar's capture sheet.") | |
| 39 | public static let openAppWhenRun = true | |
| 40 | ||
| 41 | @Parameter(title: "Template") | |
| 42 | public var template: CaptureTemplateEntity? | |
| 43 | ||
| 44 | /// Shows the capture sheet; set by the app at launch. | |
| 45 | @MainActor public static var handler: (@MainActor @Sendable (String?) -> Void)? | |
| 46 | ||
| 47 | public init() {} | |
| 48 | ||
| 49 | public init(template: CaptureTemplateEntity?) { | |
| 50 | self.template = template | |
| 51 | } | |
| 52 | ||
| 53 | @MainActor | |
| 54 | public func perform() async throws -> some IntentResult { | |
| 55 | Self.handler?(template?.id) | |
| 56 | return .result() | |
| 57 | } | |
| 58 | } | |
| 59 | ||
| 60 | /// The clock control: off clocks out, on clocks in to the entry clocked last | |
| 61 | /// (`org-clock-in-last`). It runs in the app's process. | |
| 62 | public struct ClockToggleIntent: SetValueIntent, LiveActivityIntent { | |
| 63 | public static let title: LocalizedStringResource = "Clock In or Out" | |
| 64 | public static let description = IntentDescription("Clocks out of the running clock, or in to the entry clocked last.") | |
| 65 | public static let isDiscoverable = false | |
| 66 | ||
| 67 | @Parameter(title: "Clocked In") | |
| 68 | public var value: Bool | |
| 69 | ||
| 70 | /// Clocks in (true) or out (false); set by the app at launch. | |
| 71 | @MainActor public static var handler: (@MainActor @Sendable (Bool) async -> Void)? | |
| 72 | ||
| 73 | public init() {} | |
| 74 | ||
| 75 | public func perform() async throws -> some IntentResult { | |
| 76 | let handler = await MainActor.run { Self.handler } | |
| 77 | await handler?(value) | |
| 78 | return .result() | |
| 79 | } | |
| 80 | } | |
| 81 | ||
| 82 | /// The capture control's settings. | |
| 83 | public struct CaptureControlConfiguration: ControlConfigurationIntent { | |
| 84 | public static let title: LocalizedStringResource = "Capture" | |
| 85 | public static let description = IntentDescription("The template to capture with.") | |
| 86 | ||
| 87 | @Parameter(title: "Template") | |
| 88 | public var template: CaptureTemplateEntity? | |
| 89 | ||
| 90 | public init() {} | |
| 91 | } | |
| 92 | ||
| 93 | /// The Lock Screen capture widget's settings. | |
| 94 | public struct CaptureWidgetConfiguration: WidgetConfigurationIntent { | |
| 95 | public static let title: LocalizedStringResource = "Capture" | |
| 96 | public static let description = IntentDescription("The template to capture with.") | |
| 97 | ||
| 98 | @Parameter(title: "Template") | |
| 99 | public var template: CaptureTemplateEntity? | |
| 100 | ||
| 101 | public init() {} | |
| 102 | } | |
| 103 | #endif | |
Sources/OrgCore/Agenda/AgendaSource.swift +9
| @@ -98,6 +98,15 @@ public struct AgendaSource: Sendable { | ||
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | let headings: [Heading] |
| 101 | ||
| 102 | /// The file's habits not done and not in skipped trees: their titles, heading offsets and | |
| 103 | /// what their consistency graphs are drawn from. | |
| 104 | public var habits: [(title: String, offset: Int, habit: Habit)] { | |
| 105 | headings.compactMap { heading in | |
| 106 | guard heading.isHabit, !heading.skipped, !heading.isDone, let habit = heading.habit else { return nil } | |
| 107 | return (heading.title, heading.start, habit) | |
| 108 | } | |
| 109 | } | |
| 101 | 110 | let stamps: [Stamp] |
| 102 | 111 | let sexps: [SexpLine] |
| 103 | 112 | let blocks: [Block] |
Sources/OrgCore/Agenda/Habit.swift +12 −1
| @@ -2,7 +2,7 @@ import Foundation | ||
| 2 | 2 | |
| 3 | 3 | /// An entry with `STYLE: habit` (org-habit.el, Org 9.8.7), as `org-habit-parse-todo` reads |
| 4 | 4 | /// it, with `org-log-states-order-reversed` t. |
| 5 | public struct Habit: Sendable, Equatable { | |
| 5 | public struct Habit: Sendable, Equatable, Codable { | |
| 6 | 6 | /// Days are absolute day numbers (`Days`). |
| 7 | 7 | public let scheduled: Int |
| 8 | 8 | public let scheduledRepeat: Int |
| @@ -13,6 +13,17 @@ public struct Habit: Sendable, Equatable { | ||
| 13 | 13 | /// `+`, `++` or `.+`. |
| 14 | 14 | public let repeatType: String |
| 15 | 15 | |
| 16 | public init( | |
| 17 | scheduled: Int, scheduledRepeat: Int, deadlineDay: Int? = nil, deadlineRepeatDays: Int? = nil, doneDates: [Int], repeatType: String = ".+" | |
| 18 | ) { | |
| 19 | self.scheduled = scheduled | |
| 20 | self.scheduledRepeat = scheduledRepeat | |
| 21 | self.deadlineDay = deadlineDay | |
| 22 | self.deadlineRepeatDays = deadlineRepeatDays | |
| 23 | self.doneDates = doneDates | |
| 24 | self.repeatType = repeatType | |
| 25 | } | |
| 26 | ||
| 16 | 27 | public static let precedingDays = 21 |
| 17 | 28 | public static let followingDays = 7 |
| 18 | 29 | |
Sources/OrgstarMobile/LiveClock.swift +29 −5
| @@ -2,6 +2,7 @@ | ||
| 2 | 2 | import ActivityKit |
| 3 | 3 | import AppIntents |
| 4 | 4 | import OrgApp |
| 5 | import WidgetKit | |
| 5 | 6 | |
| 6 | 7 | /// The running clock's Live Activity: one while a clock runs, started when it starts and |
| 7 | 8 | /// ended when it stops, however that happens. |
| @@ -28,16 +29,39 @@ enum LiveClock { | ||
| 28 | 29 | ) |
| 29 | 30 | } |
| 30 | 31 | |
| 31 | /// The Clock Out button's work, in the app's process. | |
| 32 | /// The clock control's state: the running clock, and the entry Clock In Last would take. | |
| 33 | static func publish(_ clock: ClockModel) { | |
| 34 | let snapshot = ClockSnapshot(running: clock.running?.heading, startedAt: clock.running?.startedAt, last: clock.history.first?.title) | |
| 35 | if (try? snapshot.write()) == true { ControlCenter.shared.reloadControls(ofKind: "sh.krz.orgstar.clock-control") } | |
| 36 | } | |
| 37 | ||
| 38 | /// The Clock Out button's work, in the app's process, and the capture and clock controls'. | |
| 32 | 39 | static func install() { |
| 33 | ClockOutIntent.handler = { | |
| 40 | ClockOutIntent.handler = clockOut | |
| 41 | ClockToggleIntent.handler = { on in | |
| 42 | guard on else { return await clockOut() } | |
| 43 | // Asks in the app if a dangling clock needs resolving. | |
| 34 | 44 | let model = MobileModel.shared |
| 35 | let path = model.clock.running?.path | |
| 36 | _ = model.clock.clockOut(model.session) | |
| 37 | if let path, model.session.dirtyPaths.contains(path) { _ = model.session.save(path) } | |
| 45 | guard let workspace = model.workspace else { return } | |
| 46 | let paths = workspace.files.filter { $0.kind == .org }.map(\.path) | |
| 47 | _ = await model.clock.clockIn(model.session, target: .last, paths: paths, ask: model.clockPrompts.ask) | |
| 48 | for path in model.session.dirtyPaths { _ = model.session.save(path) } | |
| 38 | 49 | await sync(model.clock.running) |
| 50 | publish(model.clock) | |
| 51 | } | |
| 52 | OpenCaptureIntent.handler = { template in | |
| 53 | MobileModel.shared.captureRequest = CaptureRequest(template: template) | |
| 39 | 54 | } |
| 40 | 55 | } |
| 56 | ||
| 57 | private static func clockOut() async { | |
| 58 | let model = MobileModel.shared | |
| 59 | let path = model.clock.running?.path | |
| 60 | _ = model.clock.clockOut(model.session) | |
| 61 | if let path, model.session.dirtyPaths.contains(path) { _ = model.session.save(path) } | |
| 62 | await sync(model.clock.running) | |
| 63 | publish(model.clock) | |
| 64 | } | |
| 41 | 65 | } |
| 42 | 66 | |
| 43 | 67 | /// The App Intents OrgApp declares. |
Sources/OrgstarMobile/MobileModel.swift +8 −4
| @@ -34,7 +34,7 @@ final class MobileModel { | ||
| 34 | 34 | var openRequest: ReaderRoute? |
| 35 | 35 | /// Agenda days to show, from a followed timestamp: the first (a `Days` number) and how many. |
| 36 | 36 | var agendaRequest: AgendaDays? |
| 37 | /// A capture to show, from the agenda. | |
| 37 | /// A capture to show, from the agenda or the capture control. | |
| 38 | 38 | var captureRequest: CaptureRequest? |
| 39 | 39 | /// Calendar's events for the agenda, with `calendar-events`. |
| 40 | 40 | let calendarEvents = CalendarEvents() |
| @@ -251,7 +251,8 @@ final class MobileModel { | ||
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | /// The widgets' agenda: today and the next days in the app group's container, with the |
| 254 | /// widgets reloaded when it changed; and the icon's overdue badge. | |
| 254 | /// widgets reloaded when it changed; their habits and capture templates; and the icon's | |
| 255 | /// overdue badge. | |
| 255 | 256 | func updateWidgets() async { |
| 256 | 257 | let interval = Perf.begin("widgets.update") |
| 257 | 258 | defer { interval.end() } |
| @@ -266,9 +267,12 @@ final class MobileModel { | ||
| 266 | 267 | days.calendarNames = agenda.calendarNames |
| 267 | 268 | } |
| 268 | 269 | await days.refresh(open: session.openTexts) |
| 269 | await updateBadge(OverdueBadge.counts(days.groups(), today: days.today, days: AgendaSnapshot.dayCount)) | |
| 270 | let groups = days.groups() | |
| 271 | await updateBadge(OverdueBadge.counts(groups, today: days.today, days: AgendaSnapshot.dayCount)) | |
| 272 | if (try? HabitSnapshot(habits: await days.habits(open: session.openTexts), theme: theme).write()) == true { WidgetCenter.shared.reloadTimelines(ofKind: "sh.krz.orgstar.habits") } | |
| 273 | _ = try? TemplateList(templates: CaptureTemplates.load().templates.map { .init(key: $0.key, name: $0.name) }).write() | |
| 270 | 274 | guard let file = AgendaSnapshot.file() else { return } |
| 271 | let snapshot = AgendaSnapshot(groups: days.groups(), today: days.today, theme: theme, generated: Date()) | |
| 275 | let snapshot = AgendaSnapshot(groups: groups, today: days.today, theme: theme, generated: Date()) | |
| 272 | 276 | if (try? snapshot.write(to: file)) == true { WidgetCenter.shared.reloadAllTimelines() } |
| 273 | 277 | } |
| 274 | 278 | |
Sources/OrgstarMobile/OrgstarMobileApp.swift +10 −2
| @@ -213,11 +213,13 @@ struct RootView: View { | ||
| 213 | 213 | // An open file takes in what changed on disk. |
| 214 | 214 | if let path = workspace.changedFile?.path { session.fileChanged(path) } |
| 215 | 215 | } |
| 216 | .onChange(of: model.captureRequest?.id) { | |
| 216 | // Initially too: the capture control may have launched the app. | |
| 217 | .onChange(of: model.captureRequest?.id, initial: true) { | |
| 217 | 218 | guard let request = model.captureRequest else { return } |
| 218 | 219 | model.captureRequest = nil |
| 219 | 220 | capture = request |
| 220 | 221 | } |
| 222 | ||
| 221 | 223 | .sheet(item: $capture, onDismiss: showWaiting) { request in |
| 222 | 224 | CaptureScreen(workspace: workspace, session: session, request: request) |
| 223 | 225 | } |
| @@ -260,8 +262,12 @@ struct RootView: View { | ||
| 260 | 262 | } |
| 261 | 263 | } |
| 262 | 264 | // The Live Activity follows the clock, wherever it starts or stops. |
| 263 | .task { await LiveClock.sync(model.clock.running) } | |
| 265 | .task { | |
| 266 | LiveClock.publish(model.clock) | |
| 267 | await LiveClock.sync(model.clock.running) | |
| 268 | } | |
| 264 | 269 | .onChange(of: model.clock.running) { |
| 270 | LiveClock.publish(model.clock) | |
| 265 | 271 | Task { await LiveClock.sync(model.clock.running) } |
| 266 | 272 | } |
| 267 | 273 | } |
| @@ -286,6 +292,8 @@ struct RootView: View { | ||
| 286 | 292 | model.openRequest = .offset(path, offset) |
| 287 | 293 | case .clock: |
| 288 | 294 | clock("app.clock.goto") |
| 295 | case .capture(let template): | |
| 296 | capture = CaptureRequest(template: template) | |
| 289 | 297 | } |
| 290 | 298 | } |
| 291 | 299 | |
Sources/OrgstarWidgets/Controls.swift added +100
| @@ -0,0 +1,100 @@ | ||
| 1 | import AppIntents | |
| 2 | import OrgApp | |
| 3 | import SwiftUI | |
| 4 | import WidgetKit | |
| 5 | ||
| 6 | /// Capture from Control Center, the Lock Screen or the Action button, with a chosen template. | |
| 7 | struct CaptureControl: ControlWidget { | |
| 8 | var body: some ControlWidgetConfiguration { | |
| 9 | AppIntentControlConfiguration(kind: "sh.krz.orgstar.capture-control", intent: CaptureControlConfiguration.self) { configuration in | |
| 10 | ControlWidgetButton(action: OpenCaptureIntent(template: configuration.template)) { | |
| 11 | Label(configuration.template?.name ?? "Capture", systemImage: "square.and.pencil") | |
| 12 | } | |
| 13 | } | |
| 14 | .displayName("Capture") | |
| 15 | .description("Opens orgstar's capture sheet, with the template you choose.") | |
| 16 | } | |
| 17 | } | |
| 18 | ||
| 19 | /// The clock: on while one runs, showing its entry; off shows the entry clocked last, which | |
| 20 | /// turning it on clocks in to. | |
| 21 | struct ClockControl: ControlWidget { | |
| 22 | var body: some ControlWidgetConfiguration { | |
| 23 | StaticControlConfiguration(kind: "sh.krz.orgstar.clock-control", provider: ClockValueProvider()) { clock in | |
| 24 | ControlWidgetToggle("Clock", isOn: clock.running != nil, action: ClockToggleIntent()) { isOn in | |
| 25 | Label(isOn ? clock.running ?? "Clocked in" : clock.last.map { "In: \($0)" } ?? "No recent clock", | |
| 26 | systemImage: isOn ? "stopwatch.fill" : "stopwatch") | |
| 27 | } | |
| 28 | .tint(.orange) | |
| 29 | } | |
| 30 | .displayName("Clock") | |
| 31 | .description("Clocks out of the running clock, or back in to the entry clocked last.") | |
| 32 | } | |
| 33 | } | |
| 34 | ||
| 35 | struct ClockValueProvider: ControlValueProvider { | |
| 36 | var previewValue: ClockSnapshot { ClockSnapshot(running: nil, startedAt: nil, last: "Review the draft") } | |
| 37 | ||
| 38 | func currentValue() async throws -> ClockSnapshot { | |
| 39 | ClockSnapshot.read() ?? ClockSnapshot(running: nil, startedAt: nil, last: nil) | |
| 40 | } | |
| 41 | } | |
| 42 | ||
| 43 | /// Capture on the Lock Screen: a button that opens the capture sheet with a chosen template. | |
| 44 | struct CaptureWidget: Widget { | |
| 45 | var body: some WidgetConfiguration { | |
| 46 | AppIntentConfiguration(kind: "sh.krz.orgstar.capture", intent: CaptureWidgetConfiguration.self, provider: CaptureProvider()) { entry in | |
| 47 | CaptureView(entry: entry) | |
| 48 | } | |
| 49 | .configurationDisplayName("Capture") | |
| 50 | .description("Opens orgstar's capture sheet, with the template you choose.") | |
| 51 | .supportedFamilies([.accessoryCircular, .accessoryRectangular]) | |
| 52 | } | |
| 53 | } | |
| 54 | ||
| 55 | struct CaptureEntry: TimelineEntry { | |
| 56 | let date: Date | |
| 57 | let template: CaptureTemplateEntity? | |
| 58 | } | |
| 59 | ||
| 60 | struct CaptureProvider: AppIntentTimelineProvider { | |
| 61 | func placeholder(in context: Context) -> CaptureEntry { | |
| 62 | CaptureEntry(date: .now, template: nil) | |
| 63 | } | |
| 64 | ||
| 65 | func snapshot(for configuration: CaptureWidgetConfiguration, in context: Context) async -> CaptureEntry { | |
| 66 | CaptureEntry(date: .now, template: configuration.template) | |
| 67 | } | |
| 68 | ||
| 69 | func timeline(for configuration: CaptureWidgetConfiguration, in context: Context) async -> Timeline<CaptureEntry> { | |
| 70 | Timeline(entries: [CaptureEntry(date: .now, template: configuration.template)], policy: .never) | |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | struct CaptureView: View { | |
| 75 | let entry: CaptureEntry | |
| 76 | @Environment(\.widgetFamily) private var family | |
| 77 | ||
| 78 | var body: some View { | |
| 79 | Group { | |
| 80 | if family == .accessoryCircular { | |
| 81 | ZStack { | |
| 82 | AccessoryWidgetBackground() | |
| 83 | Image(systemName: "square.and.pencil").font(.title2) | |
| 84 | } | |
| 85 | } else { | |
| 86 | HStack(spacing: 8) { | |
| 87 | Image(systemName: "square.and.pencil").font(.title3) | |
| 88 | VStack(alignment: .leading, spacing: 0) { | |
| 89 | Text("Capture").font(.headline) | |
| 90 | Text(entry.template?.name ?? "First template").font(.caption).foregroundStyle(.secondary).lineLimit(1) | |
| 91 | } | |
| 92 | Spacer(minLength: 0) | |
| 93 | } | |
| 94 | } | |
| 95 | } | |
| 96 | .widgetURL(AppLink.capture(template: entry.template?.id).url) | |
| 97 | .containerBackground(for: .widget) { Color.clear } | |
| 98 | .accessibilityLabel("Capture" + (entry.template.map { " with \($0.name)" } ?? "")) | |
| 99 | } | |
| 100 | } | |
Sources/OrgstarWidgets/HabitsWidget.swift added +173
| @@ -0,0 +1,173 @@ | ||
| 1 | import OrgApp | |
| 2 | import OrgCore | |
| 3 | import SwiftUI | |
| 4 | import WidgetKit | |
| 5 | ||
| 6 | /// The agenda's habits with org-habit's consistency graphs: the last week (small) or the last | |
| 7 | /// three weeks and the next one (medium, large). | |
| 8 | struct HabitsWidget: Widget { | |
| 9 | var body: some WidgetConfiguration { | |
| 10 | StaticConfiguration(kind: "sh.krz.orgstar.habits", provider: HabitProvider()) { entry in | |
| 11 | HabitsView(entry: entry) | |
| 12 | } | |
| 13 | .configurationDisplayName("Habits") | |
| 14 | .description("Your habits and how consistently you've kept them.") | |
| 15 | .supportedFamilies([.systemSmall, .systemMedium, .systemLarge]) | |
| 16 | } | |
| 17 | } | |
| 18 | ||
| 19 | struct HabitEntry: TimelineEntry { | |
| 20 | let date: Date | |
| 21 | /// Nil until the app has written one. | |
| 22 | let snapshot: HabitSnapshot? | |
| 23 | } | |
| 24 | ||
| 25 | /// The app's habits, drawn again at each midnight of the next week, when the graphs move on. | |
| 26 | struct HabitProvider: TimelineProvider { | |
| 27 | func placeholder(in context: Context) -> HabitEntry { | |
| 28 | HabitEntry(date: .now, snapshot: .sample(.now)) | |
| 29 | } | |
| 30 | ||
| 31 | func getSnapshot(in context: Context, completion: @escaping (HabitEntry) -> Void) { | |
| 32 | let snapshot = HabitSnapshot.read() | |
| 33 | completion(HabitEntry(date: .now, snapshot: snapshot ?? (context.isPreview ? .sample(.now) : nil))) | |
| 34 | } | |
| 35 | ||
| 36 | func getTimeline(in context: Context, completion: @escaping (Timeline<HabitEntry>) -> Void) { | |
| 37 | let snapshot = HabitSnapshot.read() | |
| 38 | let midnight = Calendar.current.startOfDay(for: .now) | |
| 39 | let dates = [Date.now] + (1...7).compactMap { Calendar.current.date(byAdding: .day, value: $0, to: midnight) } | |
| 40 | completion(Timeline(entries: dates.map { HabitEntry(date: $0, snapshot: snapshot) }, policy: .atEnd)) | |
| 41 | } | |
| 42 | } | |
| 43 | ||
| 44 | struct HabitsView: View { | |
| 45 | let entry: HabitEntry | |
| 46 | @Environment(\.widgetFamily) private var family | |
| 47 | @Environment(\.colorScheme) private var scheme | |
| 48 | ||
| 49 | private var today: Int { Days.absolute(entry.date, calendar: .current) } | |
| 50 | ||
| 51 | var body: some View { | |
| 52 | Group { | |
| 53 | if let snapshot = entry.snapshot, !snapshot.habits.isEmpty { | |
| 54 | content(snapshot) | |
| 55 | } else { | |
| 56 | Text(entry.snapshot == nil ? "Open orgstar to show your habits here." : "No habits in the agenda. Give an entry STYLE: habit.") | |
| 57 | .font(.caption) | |
| 58 | .foregroundStyle(.secondary) | |
| 59 | .frame(maxWidth: .infinity, maxHeight: .infinity) | |
| 60 | } | |
| 61 | } | |
| 62 | .widgetURL(AppLink.agenda.url) | |
| 63 | .containerBackground(for: .widget) { Color(uiColor: .systemBackground) } | |
| 64 | } | |
| 65 | ||
| 66 | private func content(_ snapshot: HabitSnapshot) -> some View { | |
| 67 | let limit = family == .systemLarge ? 9 : family == .systemMedium ? 4 : 3 | |
| 68 | let done = snapshot.habits.filter { $0.habit.doneDates.contains(today) }.count | |
| 69 | return VStack(alignment: .leading, spacing: family == .systemSmall ? 6 : 8) { | |
| 70 | HStack(alignment: .firstTextBaseline) { | |
| 71 | Text("Habits").font(.subheadline.weight(.semibold)) | |
| 72 | Spacer(minLength: 0) | |
| 73 | Text("\(done)/\(snapshot.habits.count) today").font(.caption2.weight(.semibold)).foregroundStyle(.secondary) | |
| 74 | } | |
| 75 | ForEach(snapshot.habits.prefix(limit)) { habit in | |
| 76 | Link(destination: habit.link.url) { row(habit, snapshot) } | |
| 77 | } | |
| 78 | Spacer(minLength: 0) | |
| 79 | } | |
| 80 | .frame(maxWidth: .infinity, alignment: .leading) | |
| 81 | } | |
| 82 | ||
| 83 | @ViewBuilder | |
| 84 | private func row(_ habit: HabitSnapshot.Entry, _ snapshot: HabitSnapshot) -> some View { | |
| 85 | if family == .systemSmall { | |
| 86 | // The last week, today last. | |
| 87 | let cells = habit.habit.graph(today: today, preceding: 6, following: 1) | |
| 88 | VStack(alignment: .leading, spacing: 2) { | |
| 89 | Text(habit.title).font(.caption).lineLimit(1) | |
| 90 | HabitCells(cells: cells, colors: snapshot.colors, width: 12, height: 8) | |
| 91 | } | |
| 92 | } else { | |
| 93 | HStack(spacing: 8) { | |
| 94 | Text(habit.title).font(.caption).lineLimit(1) | |
| 95 | Spacer(minLength: 4) | |
| 96 | HabitCells(cells: habit.habit.graph(today: today), colors: snapshot.colors, width: 5, height: 12) | |
| 97 | } | |
| 98 | } | |
| 99 | } | |
| 100 | } | |
| 101 | ||
| 102 | /// org-habit's graph: a bar per day in the due-state color, lighter for days to come, a dot | |
| 103 | /// where it was done, today outlined. | |
| 104 | struct HabitCells: View { | |
| 105 | let cells: [Habit.Cell] | |
| 106 | let colors: [String: AgendaSnapshot.Hex] | |
| 107 | let width: CGFloat | |
| 108 | let height: CGFloat | |
| 109 | @Environment(\.colorScheme) private var scheme | |
| 110 | ||
| 111 | var body: some View { | |
| 112 | HStack(spacing: 1) { | |
| 113 | ForEach(cells, id: \.day) { cell in | |
| 114 | RoundedRectangle(cornerRadius: 1.5) | |
| 115 | .fill(color(cell.face)) | |
| 116 | .frame(width: width, height: height) | |
| 117 | .overlay { | |
| 118 | if cell.glyph == "*" { Circle().fill(.white.opacity(0.9)).frame(width: min(width, height) * 0.6) } | |
| 119 | } | |
| 120 | .overlay { | |
| 121 | if cell.glyph == "!" { RoundedRectangle(cornerRadius: 1.5).strokeBorder(Color.primary, lineWidth: 1) } | |
| 122 | } | |
| 123 | } | |
| 124 | } | |
| 125 | .accessibilityElement(children: .ignore) | |
| 126 | .accessibilityLabel("Done \(cells.filter { $0.glyph == "*" }.count) of \(cells.count) days") | |
| 127 | } | |
| 128 | ||
| 129 | private func color(_ face: Habit.Face) -> Color { | |
| 130 | func themed(_ key: String, _ fallback: Color) -> Color { colors[key]?.color(scheme) ?? fallback } | |
| 131 | switch face { | |
| 132 | case .clear: return themed("habit-clear", .blue).opacity(0.7) | |
| 133 | case .clearFuture: return themed("habit-clear", .blue).opacity(0.3) | |
| 134 | case .ready: return themed("habit-ready", .green).opacity(0.8) | |
| 135 | case .readyFuture: return themed("habit-ready", .green).opacity(0.3) | |
| 136 | case .alert: return themed("habit-alert", .yellow).opacity(0.85) | |
| 137 | case .alertFuture: return themed("habit-alert", .yellow).opacity(0.35) | |
| 138 | case .overdue: return themed("habit-overdue", .red).opacity(0.8) | |
| 139 | case .overdueFuture: return themed("habit-overdue", .red).opacity(0.3) | |
| 140 | } | |
| 141 | } | |
| 142 | } | |
| 143 | ||
| 144 | extension HabitSnapshot { | |
| 145 | /// Habits for the gallery and previews. | |
| 146 | static func sample(_ now: Date) -> HabitSnapshot { | |
| 147 | let today = Days.absolute(now, calendar: .current) | |
| 148 | func habit(_ every: Int, done: [Int]) -> Habit { | |
| 149 | Habit(scheduled: today, scheduledRepeat: every, doneDates: done.map { today - $0 }.sorted()) | |
| 150 | } | |
| 151 | return HabitSnapshot( | |
| 152 | habits: [ | |
| 153 | Entry(title: "Stretch", path: "", offset: 0, habit: habit(1, done: [1, 2, 3, 5, 6, 7, 9, 10, 12, 13, 14])), | |
| 154 | Entry(title: "Read 20 pages", path: "", offset: 0, habit: habit(1, done: [1, 3, 4, 8, 11, 15])), | |
| 155 | Entry(title: "Water plants", path: "", offset: 0, habit: habit(3, done: [2, 5, 8, 11, 14, 17])), | |
| 156 | Entry(title: "Review the week", path: "", offset: 0, habit: habit(7, done: [6, 13, 20])), | |
| 157 | ], | |
| 158 | colors: [:] | |
| 159 | ) | |
| 160 | } | |
| 161 | } | |
| 162 | ||
| 163 | #Preview("Small", as: .systemSmall) { | |
| 164 | HabitsWidget() | |
| 165 | } timeline: { | |
| 166 | HabitEntry(date: .now, snapshot: .sample(.now)) | |
| 167 | } | |
| 168 | ||
| 169 | #Preview("Medium", as: .systemMedium) { | |
| 170 | HabitsWidget() | |
| 171 | } timeline: { | |
| 172 | HabitEntry(date: .now, snapshot: .sample(.now)) | |
| 173 | } | |
Sources/OrgstarWidgets/OrgstarWidgets.swift +6 −1
| @@ -3,13 +3,18 @@ import OrgApp | ||
| 3 | 3 | import SwiftUI |
| 4 | 4 | import WidgetKit |
| 5 | 5 | |
| 6 | /// The agenda on the Home Screen and the Lock Screen, and the running clock's Live Activity. | |
| 6 | /// The agenda and habits on the Home Screen and the Lock Screen, capture and the clock in | |
| 7 | /// Control Center, and the running clock's Live Activity. | |
| 7 | 8 | @main |
| 8 | 9 | struct OrgstarWidgets: WidgetBundle { |
| 9 | 10 | var body: some Widget { |
| 10 | 11 | TodayWidget() |
| 12 | HabitsWidget() | |
| 11 | 13 | UpNextWidget() |
| 14 | CaptureWidget() | |
| 12 | 15 | ClockActivityWidget() |
| 16 | CaptureControl() | |
| 17 | ClockControl() | |
| 13 | 18 | } |
| 14 | 19 | } |
| 15 | 20 | |
Tests/OrgAppTests/AgendaSnapshotTests.swift +1 −1
| @@ -112,7 +112,7 @@ struct AgendaSnapshotTests { | ||
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | @Test func linksRoundTrip() { |
| 115 | for link in [AppLink.agenda, .clock, .entry(path: "/notes/a b&c+d/ünï.org", offset: 42)] { | |
| 115 | for link in [AppLink.agenda, .clock, .capture(template: nil), .capture(template: "t"), .entry(path: "/notes/a b&c+d/ünï.org", offset: 42)] { | |
| 116 | 116 | #expect(AppLink(url: link.url) == link) |
| 117 | 117 | } |
| 118 | 118 | #expect(AppLink.agenda.url.absoluteString == "orgstar://agenda") |
Tests/OrgAppTests/WidgetDataTests.swift added +49
| @@ -0,0 +1,49 @@ | ||
| 1 | import Foundation | |
| 2 | import OrgCore | |
| 3 | import OrgPresentation | |
| 4 | import Testing | |
| 5 | @testable import OrgApp | |
| 6 | ||
| 7 | struct WidgetDataTests { | |
| 8 | static let file = """ | |
| 9 | * TODO Stretch | |
| 10 | SCHEDULED: <2026-10-05 Mon .+1d> | |
| 11 | :PROPERTIES: | |
| 12 | :STYLE: habit | |
| 13 | :END: | |
| 14 | :LOGBOOK: | |
| 15 | - State "DONE" from "TODO" [2026-10-04 Sun 08:00] | |
| 16 | :END: | |
| 17 | * TODO Water plants | |
| 18 | SCHEDULED: <2026-10-05 Mon> | |
| 19 | * DONE Old habit | |
| 20 | SCHEDULED: <2026-10-05 Mon .+1d> | |
| 21 | :PROPERTIES: | |
| 22 | :STYLE: habit | |
| 23 | :END: | |
| 24 | """ | |
| 25 | ||
| 26 | @Test func habitsOfTheFilesWithTheirGraphsAndColors() throws { | |
| 27 | let today = Days.absolute(year: 2026, month: 10, day: 5) | |
| 28 | let source = AgendaSource(path: "/notes/life.org", text: Self.file, defaults: .default) | |
| 29 | let habits = source.habits.map { HabitSnapshot.Entry(title: $0.title, path: source.path, offset: $0.offset, habit: $0.habit) } | |
| 30 | let snapshot = HabitSnapshot(habits: habits, theme: .default) | |
| 31 | #expect(snapshot.habits.map(\.title) == ["Stretch"]) | |
| 32 | let habit = try #require(snapshot.habits.first) | |
| 33 | #expect(habit.link == .entry(path: "/notes/life.org", offset: 0)) | |
| 34 | #expect(habit.habit.graph(today: today).contains { $0.glyph == "*" }) | |
| 35 | #expect(Set(snapshot.colors.keys) == ["habit-clear", "habit-ready", "habit-alert", "habit-overdue"]) | |
| 36 | } | |
| 37 | ||
| 38 | @Test func writesOnlyWhatChanged() throws { | |
| 39 | let folder = try Folder() | |
| 40 | let file = folder.url.appendingPathComponent("Widgets/clock.json") | |
| 41 | var clock = ClockSnapshot(running: "Write", startedAt: Date(timeIntervalSince1970: 1_000_000), last: "Read") | |
| 42 | #expect(try clock.write(to: file)) | |
| 43 | #expect(ClockSnapshot.read(from: file) == clock) | |
| 44 | #expect(try !clock.write(to: file)) | |
| 45 | clock.running = nil | |
| 46 | #expect(try clock.write(to: file)) | |
| 47 | #expect(ClockSnapshot.read(from: file)?.running == nil) | |
| 48 | } | |
| 49 | } | |
docs/manual/guide/14-ios.org +12 −3
| @@ -176,18 +176,27 @@ See [[file:07-agenda.org][The agenda]] for the rows, the views and matches, and | ||
| 176 | 176 | |
| 177 | 177 | ** Widgets |
| 178 | 178 | |
| 179 | orgstar has two widgets. Add them as any widget: touch and hold the Home Screen or the Lock Screen, and choose orgstar in the widget gallery. | |
| 179 | orgstar has four widgets. Add them as any widget: touch and hold the Home Screen or the Lock Screen, and choose orgstar in the widget gallery. | |
| 180 | 180 | |
| 181 | 181 | | Widget | Sizes | Shows | |
| 182 | 182 | |--------+----------------------------------------------+---------------------------------------------------------------------------------------------| |
| 183 | 183 | | Today | Small, medium and large (Home Screen) | The date, the count of open entries, and what's left of today: 3 rows (small), 4 rows (medium), or 6 rows and tomorrow's entries (large). Medium and large also count overdue entries. | |
| 184 | 184 | | Up Next | Rectangular, inline and circular (Lock Screen) | The next entry's time or status and title (rectangular, inline), or the count of open entries today (circular, and inline when nothing is left) | |
| 185 | | Habits | Small, medium and large (Home Screen) | Each habit (=STYLE: habit=) in your agenda files that isn't done, with its consistency graph, as the agenda draws it: the last week (small, 3 habits), or the last three weeks and the next one (medium, 4 habits; large, 9), and how many were done today. | | |
| 186 | | Capture | Circular and rectangular (Lock Screen) | A button that opens the capture sheet with the template you choose when you edit the widget, or the first template. | | |
| 185 | 187 | |
| 186 | 188 | What's left of today is today's entries and calendar events that aren't done, without timed ones that have ended (or started, when they have no end), followed by the overdue entries. The open count is today's entries not done, overdue ones included, without calendar events. The widgets use the theme's colours for the status, the keyword, the priority and the category. |
| 187 | 189 | |
| 188 | Tapping a row in the medium or large Today widget opens that entry; tapping elsewhere opens the agenda. Tapping Up Next opens the next entry, or the agenda when there is none. | |
| 190 | Tapping a row in the medium or large Today widget opens that entry; tapping elsewhere opens the agenda. Tapping Up Next opens the next entry, or the agenda when there is none. Tapping a habit opens it. | |
| 189 | 191 | |
| 190 | Widgets can't read your folders, so the app writes the next seven days of the agenda (today included) for them to read: while it is open, when files, edits or settings change, when you return to the app, and every hour. Between those times the widgets move on by themselves as timed entries end and at midnight, but they show nothing new until you open orgstar. Until the app has written the agenda once, they say =Open orgstar to show your agenda here.= The widgets ignore the agenda's filter and show no days without entries. | |
| 192 | Widgets can't read your folders, so the app writes the next seven days of the agenda (today included) for them to read: while it is open, when files, edits or settings change, when you return to the app, and every hour. Between those times the widgets move on by themselves as timed entries end and at midnight, but they show nothing new until you open orgstar. Until the app has written the agenda once, they say =Open orgstar to show your agenda here.= The widgets ignore the agenda's filter and show no days without entries. The Habits widget's graphs move on at midnight by themselves. | |
| 193 | ||
| 194 | ** Controls | |
| 195 | ||
| 196 | orgstar has two controls for Control Center, the Lock Screen and the Action button. Add them as any control: in Control Center, touch and hold an empty space and tap Add a Control, then choose orgstar. | |
| 197 | ||
| 198 | - Capture :: opens the capture sheet. Touch and hold it while editing Control Center to choose a template; without one it uses the first. | |
| 199 | - Clock :: on while a clock runs, showing its entry. Turning it off clocks out; turning it on clocks in to the entry clocked last (Clock In Last). If a clock was left running elsewhere, orgstar asks in the app how to resolve it. | |
| 191 | 200 | |
| 192 | 201 | ** The clock's Live Activity |
| 193 | 202 | |