Energy: poll the branch only while active, cut widget reloads !462
5 files changed, +38 −7
Layout: unified · split
Sources/OrgApp/AgendaSnapshot.swift +7
| @@ -242,6 +242,13 @@ public struct AgendaSnapshot: Codable, Equatable, Sendable { | |||
| 242 | return true | 242 | return true |
| 243 | } | 243 | } |
| 244 | 244 | ||
| 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 | |||
| 245 | static var encoder: JSONEncoder { | 252 | static var encoder: JSONEncoder { |
| 246 | let encoder = JSONEncoder() | 253 | let encoder = JSONEncoder() |
| 247 | encoder.dateEncodingStrategy = .iso8601 | 254 | encoder.dateEncodingStrategy = .iso8601 |
Sources/Orgstar/ContentView.swift +2 −1
| @@ -501,6 +501,8 @@ struct ContentView: View { | |||
| 501 | enabled: remindersEnabled, lead: reminderLead, subfolders: agendaSubfolders, hidden: HiddenFolders.shared.all, files: workspace.files.count, | 501 | enabled: remindersEnabled, lead: reminderLead, subfolders: agendaSubfolders, hidden: HiddenFolders.shared.all, files: workspace.files.count, |
| 502 | changed: workspace.changedFile?.version, edits: session.editVersion, hour: hour, theme: theme | 502 | changed: workspace.changedFile?.version, edits: session.editVersion, hour: hour, theme: theme |
| 503 | )) { | 503 | )) { |
| 504 | // Once edits pause. | ||
| 505 | try? await Task.sleep(for: .seconds(2)) | ||
| 504 | await updateReminders() | 506 | await updateReminders() |
| 505 | await updateWidgets() | 507 | await updateWidgets() |
| 506 | } | 508 | } |
| @@ -1171,7 +1173,6 @@ struct ContentView: View { | |||
| 1171 | await scheduler.clear() | 1173 | await scheduler.clear() |
| 1172 | return | 1174 | return |
| 1173 | } | 1175 | } |
| 1174 | try? await Task.sleep(for: .seconds(2)) | ||
| 1175 | guard !Task.isCancelled, !workspace.files.isEmpty else { return } | 1176 | guard !Task.isCancelled, !workspace.files.isEmpty else { return } |
| 1176 | let agenda = reminderAgenda ?? AgendaModel(workspace: workspace) | 1177 | let agenda = reminderAgenda ?? AgendaModel(workspace: workspace) |
| 1177 | reminderAgenda = agenda | 1178 | reminderAgenda = agenda |
Sources/Orgstar/ModeLine.swift +10 −3
| @@ -14,6 +14,7 @@ struct ModeLine: View { | |||
| 14 | let perform: (String) -> Void | 14 | let perform: (String) -> Void |
| 15 | @Environment(\.orgTheme) private var theme | 15 | @Environment(\.orgTheme) private var theme |
| 16 | @Environment(\.colorScheme) private var colorScheme | 16 | @Environment(\.colorScheme) private var colorScheme |
| 17 | @Environment(\.appearsActive) private var appearsActive | ||
| 17 | @State private var words: Int? | 18 | @State private var words: Int? |
| 18 | @State private var characters = 0 | 19 | @State private var characters = 0 |
| 19 | @State private var crlf = false | 20 | @State private var crlf = false |
| @@ -102,9 +103,10 @@ struct ModeLine: View { | |||
| 102 | .frame(height: 22) | 103 | .frame(height: 22) |
| 103 | .background { if let bg = theme.color("modeline-background", in: colorScheme) { bg } else { Rectangle().fill(.bar) } } | 104 | .background { if let bg = theme.color("modeline-background", in: colorScheme) { bg } else { Rectangle().fill(.bar) } } |
| 104 | .overlay(alignment: .top) { Divider() } | 105 | .overlay(alignment: .top) { Divider() } |
| 105 | .task(id: session.path) { | 106 | .task(id: BranchKey(path: session.path, active: appearsActive)) { |
| 106 | // HEAD changes outside the app, so look again every few seconds. | 107 | // HEAD changes outside the app, so look again every few seconds while the window is |
| 107 | while !Task.isCancelled { | 108 | // active, and once when it becomes active again. |
| 109 | while appearsActive, !Task.isCancelled { | ||
| 108 | let path = session.path | 110 | let path = session.path |
| 109 | branch = await Task.detached(priority: .utility) { path.flatMap(GitBranch.current) }.value | 111 | branch = await Task.detached(priority: .utility) { path.flatMap(GitBranch.current) }.value |
| 110 | try? await Task.sleep(for: .seconds(5)) | 112 | try? await Task.sleep(for: .seconds(5)) |
| @@ -120,6 +122,11 @@ struct ModeLine: View { | |||
| 120 | } | 122 | } |
| 121 | } | 123 | } |
| 122 | 124 | ||
| 125 | private struct BranchKey: Hashable { | ||
| 126 | var path: String? | ||
| 127 | var active: Bool | ||
| 128 | } | ||
| 129 | |||
| 123 | private struct WordsKey: Hashable { | 130 | private struct WordsKey: Hashable { |
| 124 | var path: String? | 131 | var path: String? |
| 125 | var version: Int | 132 | var version: Int |
Sources/OrgstarMobile/MobileModel.swift +9 −3
| @@ -63,6 +63,8 @@ final class MobileModel { | |||
| 63 | /// `org-stored-links`, most recent first; kept while the app runs, as in Emacs. | 63 | /// `org-stored-links`, most recent first; kept while the app runs, as in Emacs. |
| 64 | var storedLinks: [StoredLink] = [] | 64 | var storedLinks: [StoredLink] = [] |
| 65 | @ObservationIgnored private var configShowMarkup: Bool? | 65 | @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? | ||
| 66 | @ObservationIgnored private var presenter: FolderPresenter? | 68 | @ObservationIgnored private var presenter: FolderPresenter? |
| 67 | @ObservationIgnored private var sizeObserver: NSObjectProtocol? | 69 | @ObservationIgnored private var sizeObserver: NSObjectProtocol? |
| 68 | 70 | ||
| @@ -313,11 +315,14 @@ final class MobileModel { | |||
| 313 | } | 315 | } |
| 314 | await days.refresh(open: session.openTexts) | 316 | await days.refresh(open: session.openTexts) |
| 315 | let groups = days.groups() | 317 | 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 { | ||
| 317 | // Widgets read the flag and show that they need Pro. | 322 | // Widgets read the flag and show that they need Pro. |
| 318 | WatchLink.shared.send(.locked(generated: Date())) | 323 | WatchLink.shared.send(.locked(generated: Date())) |
| 319 | await updateBadge(OverdueBadge.counts(groups, today: days.today, days: AgendaSnapshot.dayCount)) | 324 | 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() } |
| 321 | return | 326 | return |
| 322 | } | 327 | } |
| 323 | WatchLink.shared.send(WatchAgenda(groups: groups, today: days.today, clock: clock.running, lastClocked: clock.history.first?.title, generated: Date())) | 328 | 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 { | |||
| 326 | _ = try? TemplateList(templates: CaptureTemplates.load().templates.map { .init(key: $0.key, name: $0.name) }).write() | 331 | _ = try? TemplateList(templates: CaptureTemplates.load().templates.map { .init(key: $0.key, name: $0.name) }).write() |
| 327 | guard let file = AgendaSnapshot.file() else { return } | 332 | guard let file = AgendaSnapshot.file() else { return } |
| 328 | let snapshot = AgendaSnapshot(groups: groups, today: days.today, theme: theme, generated: Date()) | 333 | 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() } | ||
| 330 | } | 336 | } |
| 331 | 337 | ||
| 332 | /// Today's count on the icon, and each later day's set for its midnight by a notification | 338 | /// 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 { | |||
| 104 | #expect(AgendaSnapshot.read(from: file)?.days.count == 1) | 104 | #expect(AgendaSnapshot.read(from: file)?.days.count == 1) |
| 105 | } | 105 | } |
| 106 | 106 | ||
| 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 | |||
| 107 | @Test func whatIsLeftOfToday() { | 117 | @Test func whatIsLeftOfToday() { |
| 108 | let snapshot = Self.snapshot() | 118 | let snapshot = Self.snapshot() |
| 109 | let morning = Self.date(Self.today, 7) | 119 | let morning = Self.date(Self.today, 7) |