| @@ -131,14 +131,56 @@ struct AgendaView: View { |
| 131 | 131 | var tick: Int |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | /// The settings part of the refresh key; `Refresher` adds the files and edits, so that |
| 135 | /// they redraw only it. |
| 134 | 136 | private var refreshKey: RefreshKey { |
| 135 | 137 | RefreshKey( |
| 136 | 138 | mode: agenda.mode, start: agenda.start, span: span, startOffset: startOffset, includeSubfolders: includeSubfolders, prefix: [prefixAgenda, prefixTodo, prefixTags], logMode: logMode, |
| 137 | | calendars: calendarEventsOn ? [calendarNames] : [], files: workspace.files.count, changed: workspace.changedFile?.version, edits: session.editVersion, |
| 138 | | path: session.path, tick: tick |
| 139 | calendars: calendarEventsOn ? [calendarNames] : [], files: 0, changed: nil, edits: 0, path: nil, tick: tick |
| 139 | 140 | ) |
| 140 | 141 | } |
| 141 | 142 | |
| 143 | private func refresh() async { |
| 144 | agenda.span = span |
| 145 | agenda.startOffset = startOffset |
| 146 | agenda.includeSubfolders = includeSubfolders |
| 147 | agenda.options.prefixFormat = ["agenda": prefixAgenda, "todo": prefixTodo, "tags": prefixTags] |
| 148 | agenda.options.logMode = logMode |
| 149 | agenda.options = OrgPreferences.agendaOptions(agenda.options) |
| 150 | agenda.calendarNames = OrgPreferences.calendarEventCalendars() |
| 151 | if calendarEventsOn, isAgenda { |
| 152 | agenda.calendarEvents = await CalendarAccess.events.requestAccess() ? CalendarAccess.events : nil |
| 153 | } else { |
| 154 | agenda.calendarEvents = nil |
| 155 | } |
| 156 | await agenda.refresh(open: session.openTexts) |
| 157 | } |
| 158 | |
| 159 | /// Refreshes the agenda when the settings, files or edits change, once a burst of them ends. |
| 160 | private struct Refresher: View { |
| 161 | let base: RefreshKey |
| 162 | let session: DocumentSession |
| 163 | let workspace: WorkspaceModel |
| 164 | let refresh: () async -> Void |
| 165 | |
| 166 | private var key: RefreshKey { |
| 167 | var key = base |
| 168 | key.files = workspace.files.count |
| 169 | key.changed = workspace.changedFile?.version |
| 170 | key.edits = session.editVersion |
| 171 | key.path = session.path |
| 172 | return key |
| 173 | } |
| 174 | |
| 175 | var body: some View { |
| 176 | Color.clear.task(id: key) { |
| 177 | try? await Task.sleep(for: .milliseconds(150)) |
| 178 | guard !Task.isCancelled else { return } |
| 179 | await refresh() |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 142 | 184 | private var isAgenda: Bool { |
| 143 | 185 | if case .agenda = agenda.mode { return true } |
| 144 | 186 | return false |
| @@ -282,22 +324,7 @@ struct AgendaView: View { |
| 282 | 324 | .onChange(of: viewName) { applyView() } |
| 283 | 325 | .onChange(of: commands.agendaRequest?.start, initial: true) { showRequestedDays() } |
| 284 | 326 | .onChange(of: commands.agendaRequest?.days) { showRequestedDays() } |
| 285 | | .task(id: refreshKey) { |
| 286 | | agenda.span = span |
| 287 | | agenda.startOffset = startOffset |
| 288 | | agenda.includeSubfolders = includeSubfolders |
| 289 | | agenda.options.prefixFormat = ["agenda": prefixAgenda, "todo": prefixTodo, "tags": prefixTags] |
| 290 | | agenda.options.logMode = logMode |
| 291 | | agenda.options = OrgPreferences.agendaOptions(agenda.options) |
| 292 | | agenda.calendarNames = OrgPreferences.calendarEventCalendars() |
| 293 | | if calendarEventsOn, isAgenda { |
| 294 | | agenda.calendarEvents = await CalendarAccess.events.requestAccess() ? CalendarAccess.events : nil |
| 295 | | } else { |
| 296 | | agenda.calendarEvents = nil |
| 297 | | } |
| 298 | | let open = session.openTexts |
| 299 | | await agenda.refresh(open: open) |
| 300 | | } |
| 327 | .background { Refresher(base: refreshKey, session: session, workspace: workspace, refresh: refresh) } |
| 301 | 328 | .task { |
| 302 | 329 | loadViews() |
| 303 | 330 | // The now line moves each minute; a new day refreshes the statuses. |