Commit 6d9f3d99cf

6d9f3d99cf583074bd9ad1152968fe5b22e2a2a6

parent: 1463e5877b

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-22 19:12 UTC

Poll the git branch only while the window is active and cut widget reloads

The Mac mode line reads HEAD while its window appears active and once on
becoming active. iOS reloads widget timelines when orgstar+ locks or
unlocks or the snapshot changes, not on every locked update. The Mac's
widget and reminder refresh waits for a 2 s pause whether or not
reminders are on.

Closes #216

Layout: unified · split

Sources/OrgApp/AgendaSnapshot.swift +7
@@ -242,6 +242,13 @@ public struct AgendaSnapshot: Codable, Equatable, Sendable {
242242 return true
243243 }
244244
245 /// Whether the widgets' timelines need reloading after an update: when orgstar+ locked or
246 /// unlocked since the last update, or on the first update, or when unlocked and the snapshot
247 /// changed.
248 public static func reloadsWidgets(locked: Bool, wasLocked: Bool?, snapshotChanged: Bool) -> Bool {
249 locked != wasLocked || (!locked && snapshotChanged)
250 }
251
245252 static var encoder: JSONEncoder {
246253 let encoder = JSONEncoder()
247254 encoder.dateEncodingStrategy = .iso8601
Sources/Orgstar/ContentView.swift +2 −1
@@ -501,6 +501,8 @@ struct ContentView: View {
501501 enabled: remindersEnabled, lead: reminderLead, subfolders: agendaSubfolders, hidden: HiddenFolders.shared.all, files: workspace.files.count,
502502 changed: workspace.changedFile?.version, edits: session.editVersion, hour: hour, theme: theme
503503 )) {
504 // Once edits pause.
505 try? await Task.sleep(for: .seconds(2))
504506 await updateReminders()
505507 await updateWidgets()
506508 }
@@ -1171,7 +1173,6 @@ struct ContentView: View {
11711173 await scheduler.clear()
11721174 return
11731175 }
1174 try? await Task.sleep(for: .seconds(2))
11751176 guard !Task.isCancelled, !workspace.files.isEmpty else { return }
11761177 let agenda = reminderAgenda ?? AgendaModel(workspace: workspace)
11771178 reminderAgenda = agenda
Sources/Orgstar/ModeLine.swift +10 −3
@@ -14,6 +14,7 @@ struct ModeLine: View {
1414 let perform: (String) -> Void
1515 @Environment(\.orgTheme) private var theme
1616 @Environment(\.colorScheme) private var colorScheme
17 @Environment(\.appearsActive) private var appearsActive
1718 @State private var words: Int?
1819 @State private var characters = 0
1920 @State private var crlf = false
@@ -102,9 +103,10 @@ struct ModeLine: View {
102103 .frame(height: 22)
103104 .background { if let bg = theme.color("modeline-background", in: colorScheme) { bg } else { Rectangle().fill(.bar) } }
104105 .overlay(alignment: .top) { Divider() }
105 .task(id: session.path) {
106 // HEAD changes outside the app, so look again every few seconds.
107 while !Task.isCancelled {
106 .task(id: BranchKey(path: session.path, active: appearsActive)) {
107 // HEAD changes outside the app, so look again every few seconds while the window is
108 // active, and once when it becomes active again.
109 while appearsActive, !Task.isCancelled {
108110 let path = session.path
109111 branch = await Task.detached(priority: .utility) { path.flatMap(GitBranch.current) }.value
110112 try? await Task.sleep(for: .seconds(5))
@@ -120,6 +122,11 @@ struct ModeLine: View {
120122 }
121123 }
122124
125 private struct BranchKey: Hashable {
126 var path: String?
127 var active: Bool
128 }
129
123130 private struct WordsKey: Hashable {
124131 var path: String?
125132 var version: Int
Sources/OrgstarMobile/MobileModel.swift +9 −3
@@ -63,6 +63,8 @@ final class MobileModel {
6363 /// `org-stored-links`, most recent first; kept while the app runs, as in Emacs.
6464 var storedLinks: [StoredLink] = []
6565 @ObservationIgnored private var configShowMarkup: Bool?
66 /// Whether orgstar+ was locked at the last widget update; nil before the first.
67 @ObservationIgnored private var widgetsLocked: Bool?
6668 @ObservationIgnored private var presenter: FolderPresenter?
6769 @ObservationIgnored private var sizeObserver: NSObjectProtocol?
6870
@@ -313,11 +315,14 @@ final class MobileModel {
313315 }
314316 await days.refresh(open: session.openTexts)
315317 let groups = days.groups()
316 guard ProAccess.shared.isUnlocked else {
318 let locked = !ProAccess.shared.isUnlocked
319 let wasLocked = widgetsLocked
320 widgetsLocked = locked
321 guard !locked else {
317322 // Widgets read the flag and show that they need Pro.
318323 WatchLink.shared.send(.locked(generated: Date()))
319324 await updateBadge(OverdueBadge.counts(groups, today: days.today, days: AgendaSnapshot.dayCount))
320 WidgetCenter.shared.reloadAllTimelines()
325 if AgendaSnapshot.reloadsWidgets(locked: true, wasLocked: wasLocked, snapshotChanged: false) { WidgetCenter.shared.reloadAllTimelines() }
321326 return
322327 }
323328 WatchLink.shared.send(WatchAgenda(groups: groups, today: days.today, clock: clock.running, lastClocked: clock.history.first?.title, generated: Date()))
@@ -326,7 +331,8 @@ final class MobileModel {
326331 _ = try? TemplateList(templates: CaptureTemplates.load().templates.map { .init(key: $0.key, name: $0.name) }).write()
327332 guard let file = AgendaSnapshot.file() else { return }
328333 let snapshot = AgendaSnapshot(groups: groups, today: days.today, theme: theme, generated: Date())
329 if (try? snapshot.write(to: file)) == true { WidgetCenter.shared.reloadAllTimelines() }
334 let changed = (try? snapshot.write(to: file)) == true
335 if AgendaSnapshot.reloadsWidgets(locked: false, wasLocked: wasLocked, snapshotChanged: changed) { WidgetCenter.shared.reloadAllTimelines() }
330336 }
331337
332338 /// Today's count on the icon, and each later day's set for its midnight by a notification
Tests/OrgAppTests/AgendaSnapshotTests.swift +10
@@ -104,6 +104,16 @@ struct AgendaSnapshotTests {
104104 #expect(AgendaSnapshot.read(from: file)?.days.count == 1)
105105 }
106106
107 @Test func reloadsWidgetsWhenTheLockOrTheSnapshotChanges() {
108 #expect(AgendaSnapshot.reloadsWidgets(locked: true, wasLocked: nil, snapshotChanged: false))
109 #expect(AgendaSnapshot.reloadsWidgets(locked: false, wasLocked: nil, snapshotChanged: false))
110 #expect(!AgendaSnapshot.reloadsWidgets(locked: true, wasLocked: true, snapshotChanged: false))
111 #expect(AgendaSnapshot.reloadsWidgets(locked: true, wasLocked: false, snapshotChanged: false))
112 #expect(AgendaSnapshot.reloadsWidgets(locked: false, wasLocked: true, snapshotChanged: false))
113 #expect(!AgendaSnapshot.reloadsWidgets(locked: false, wasLocked: false, snapshotChanged: false))
114 #expect(AgendaSnapshot.reloadsWidgets(locked: false, wasLocked: false, snapshotChanged: true))
115 }
116
107117 @Test func whatIsLeftOfToday() {
108118 let snapshot = Self.snapshot()
109119 let morning = Self.date(Self.today, 7)