Sources/OrgstarMobile/AgendaScreen.swift
374 lines · 16311 bytes
1#if os(iOS)
2import OrgApp
3import OrgCore
4import OrgDocument
5import SwiftUI
6
7/// The agenda over the workspace's agenda files: the day/week view, the TODO list, tag and
8/// property matches and the views in `views.toml`, with Org's filters and the agenda's
9/// actions on each entry.
10struct AgendaScreen: View {
11 let workspace: WorkspaceModel
12 let session: DocumentSession
13 @Bindable var agenda: AgendaModel
14 let scheduler: ReminderScheduler
15 @State private var message: String?
16 @State private var tick = 0
17 @State private var views = AgendaViewLoader.builtIn
18 @State private var viewName = "Agenda"
19 @State private var filter = AgendaFilter()
20 @State private var showingFilter = false
21 @State private var asking: Asking?
22 @State private var prompt: EditorScreen.PromptRequest?
23 @State private var refiling: AgendaItem?
24 @State private var archiving: AgendaItem?
25
26 /// A match typed for the tag and property view.
27 private enum Asking: Identifiable {
28 case match
29 var id: Int { 0 }
30 }
31
32 private struct Key: Equatable {
33 var files: Int
34 var changed: Int?
35 var edits: Int
36 var tick: Int
37 var mode: AgendaMode
38 var start: Int?
39 var span: Int
40 var startOffset: Int
41 var subfolders: Bool
42 }
43
44 var body: some View {
45 List {
46 if case .agenda = agenda.mode {
47 ForEach(agenda.days, id: \.day) { day in
48 Section {
49 let items = day.items.filter(\.isEntry).filter(filter.keeps)
50 ForEach(items.indices, id: \.self) { i in row(items[i]) }
51 } header: {
52 Text(day.header.split(separator: " ", omittingEmptySubsequences: true).prefix(4).joined(separator: " "))
53 .foregroundStyle(day.isToday ? Color.accentColor : .secondary)
54 }
55 }
56 } else {
57 let items = agenda.list.filter(filter.keeps)
58 if items.isEmpty, !agenda.isComputing { Text("No entries").foregroundStyle(.secondary) }
59 ForEach(items.indices, id: \.self) { i in row(items[i]) }
60 }
61 if let reminderStatus {
62 Section { } footer: { Text(reminderStatus) }
63 }
64 }
65 .listStyle(.insetGrouped)
66 .navigationTitle(viewName)
67 .overlay {
68 if workspace.rootPaths.isEmpty {
69 ContentUnavailableView("No folders", systemImage: "folder", description: Text("Add a folder of org files under Folders."))
70 }
71 }
72 .safeAreaInset(edge: .bottom) {
73 if let status = message ?? (filter.isEmpty ? nil : "Filter: " + filter.description) {
74 Text(status).font(.callout).padding(8).frame(maxWidth: .infinity).background(.bar)
75 .onTapGesture { message = nil }
76 }
77 }
78 .toolbar {
79 ToolbarItem(placement: .topBarLeading) {
80 Menu {
81 ForEach(views) { definition in
82 Button(definition.name) { show(definition) }
83 }
84 Button("Tags and Properties…") { asking = .match }
85 if case .agenda = agenda.mode {
86 Divider()
87 Button("Today", systemImage: "calendar.circle") { agenda.showToday() }
88 Button("Earlier", systemImage: "chevron.left") { agenda.shift(forward: false) }
89 Button("Later", systemImage: "chevron.right") { agenda.shift(forward: true) }
90 }
91 } label: {
92 Label("Views", systemImage: "calendar")
93 }
94 }
95 ToolbarItem(placement: .topBarLeading) {
96 Button("Filter", systemImage: filter.isEmpty ? "line.3.horizontal.decrease.circle" : "line.3.horizontal.decrease.circle.fill") {
97 showingFilter = true
98 }
99 }
100 }
101 .sheet(isPresented: $showingFilter) {
102 FilterSheet(filter: $filter, items: shownItems)
103 }
104 .sheet(item: $asking) { _ in
105 PromptSheet(prompt: Prompt(key: "match", message: "Match: tags and properties, as +work-home or TODO=\"WAIT\"")) { answer in
106 asking = nil
107 guard let answer, !answer.isEmpty else { return }
108 agenda.mode = .match(answer, todoOnly: false)
109 viewName = answer
110 }
111 }
112 .sheet(item: $prompt) { request in
113 PromptSheet(prompt: request.prompt) { answer in
114 prompt = nil
115 request.reply(answer)
116 }
117 }
118 .sheet(item: $refiling) { item in
119 RefileSheet(targets: workspace.refileTargets(openPath: session.path, document: session.document)) { target in
120 refiling = nil
121 if let target { refile(item, to: target) }
122 }
123 }
124 .confirmationDialog("Archive this entry?", isPresented: Binding { archiving != nil } set: { if !$0 { archiving = nil } }, titleVisibility: .visible) {
125 Button("Archive", role: .destructive) {
126 if let item = archiving { archive(item) }
127 archiving = nil
128 }
129 } message: {
130 Text(archiving?.text ?? "")
131 }
132 .refreshable {
133 await workspace.refresh()
134 await agenda.refresh(open: session.openTexts)
135 }
136 .task {
137 let loaded = AgendaViewLoader.load()
138 views = loaded.views
139 if let problem = loaded.problems.first { message = problem }
140 }
141 .task(id: Key(files: workspace.files.count, changed: workspace.changedFile?.version, edits: session.editVersion, tick: tick,
142 mode: agenda.mode, start: agenda.start, span: agenda.span, startOffset: agenda.startOffset,
143 subfolders: agenda.includeSubfolders)) {
144 await agenda.refresh(open: session.openTexts)
145 }
146 }
147
148 private var shownItems: [AgendaItem] {
149 if case .agenda = agenda.mode { return agenda.days.flatMap(\.items).filter(\.isEntry) }
150 return agenda.list
151 }
152
153 private func show(_ definition: AgendaViewDefinition) {
154 viewName = definition.name
155 agenda.mode = definition.mode
156 }
157
158 /// How far ahead notifications are set, as the design asks the agenda to show: iOS keeps
159 /// at most 64, and sets no more while the app isn't opened.
160 private var reminderStatus: String? {
161 guard OrgPreferences.reminders() else { return nil }
162 if !scheduler.isAuthorized { return "Notifications are off for Orgstar in Settings." }
163 guard let through = scheduler.scheduledThrough else { return nil }
164 return "Reminders are set through " + through.formatted(.dateTime.weekday(.abbreviated).day().month(.abbreviated).hour().minute())
165 + ". Open Orgstar to set later ones."
166 }
167
168 // MARK: - Rows
169
170 private func row(_ item: AgendaItem) -> some View {
171 Group {
172 if let path = item.path, let offset = item.headingOffset {
173 NavigationLink(value: ReaderRoute.offset(path, offset)) { entry(item) }
174 } else {
175 entry(item)
176 }
177 }
178 .swipeActions(edge: .trailing) {
179 if item.todo != nil, !item.isDone, doneKeyword(for: item) != nil {
180 Button("Done", systemImage: "checkmark") { markDone(item) }.tint(.green)
181 }
182 }
183 .contextMenu {
184 if item.path != nil {
185 Button("TODO State…", systemImage: "checkmark.circle") { run("org.todo.cycle", on: item) }
186 Button("Schedule…", systemImage: "calendar") { run("org.schedule", on: item) }
187 Button("Deadline…", systemImage: "calendar.badge.exclamationmark") { run("org.deadline", on: item) }
188 Button("Tags…", systemImage: "tag") { run("org.tags.set", on: item) }
189 Button("Priority…", systemImage: "exclamationmark.circle") { run("org.priority.set", on: item) }
190 Divider()
191 Button("Refile…", systemImage: "arrow.turn.down.right") { refiling = item }
192 Button("Archive…", systemImage: "archivebox") { archiving = item }
193 }
194 }
195 }
196
197 private func entry(_ item: AgendaItem) -> some View {
198 VStack(alignment: .leading, spacing: 2) {
199 HStack(alignment: .firstTextBaseline, spacing: 6) {
200 if let time = item.time.firstMatch(of: /[0-9]{1,2}:[0-9]{2}(-[0-9]{1,2}:[0-9]{2})?/) {
201 Text(time.output.0).font(.callout.monospacedDigit()).foregroundStyle(.secondary)
202 }
203 Text(item.text).strikethrough(item.isDone)
204 }
205 HStack(spacing: 6) {
206 Text(item.category)
207 let extra = item.extra.split(whereSeparator: \.isWhitespace).joined(separator: " ")
208 if !extra.isEmpty { Text(extra) }
209 if !item.tags.isEmpty { Text(item.tags.map { ":" + $0 }.joined() + ":") }
210 }
211 .font(.caption)
212 .foregroundStyle(.secondary)
213 }
214 }
215
216 // MARK: - Actions
217
218 /// The heading of `item` now, found again by its line in case the file changed.
219 private func locate(_ item: AgendaItem) -> (path: String, offset: Int)? {
220 guard let path = item.path, let line = item.headingLine, let near = item.headingOffset,
221 let offset = session.locateHeading(path: path, line: line, near: near) else {
222 message = "The entry changed; the agenda has been refreshed."
223 tick += 1
224 return nil
225 }
226 return (path, offset)
227 }
228
229 /// Runs a command at the entry's heading, asking what it asks.
230 private func run(_ id: String, on item: AgendaItem, answers: [String: String] = [:]) {
231 guard let command = Commands.command(id) else { return }
232 run(command, on: item, answers: answers)
233 }
234
235 private func run(_ command: any OrgCommand, on item: AgendaItem, answers: [String: String] = [:]) {
236 guard let (path, offset) = locate(item), let line = item.headingLine else { return }
237 switch session.perform(command, path: path, at: offset, expect: (offset, line), answers: answers) {
238 case .prompt(let request):
239 prompt = EditorScreen.PromptRequest(prompt: request) { answer in
240 guard let answer else { return }
241 run(command, on: item, answers: answers.merging([request.key: answer]) { $1 })
242 }
243 case .failed(let text):
244 message = text
245 case .external:
246 message = "\(command.title) needs Emacs, which runs on the Mac."
247 case .commit:
248 message = nil
249 }
250 tick += 1
251 }
252
253 /// The first done keyword of the item's TODO sequence.
254 private func doneKeyword(for item: AgendaItem) -> String? {
255 guard let todo = item.todo, let path = item.path else { return nil }
256 let text = session.openTexts[path] ?? (try? String(contentsOfFile: path, encoding: .utf8)) ?? ""
257 let settings = OrgParser.parse(text).settings
258 return settings.todoSequences.first { ($0.active + $0.done).contains { $0.name == todo } }?.done.first?.name
259 }
260
261 private func markDone(_ item: AgendaItem) {
262 guard let keyword = doneKeyword(for: item) else { return }
263 run(SetTodo(keyword: keyword), on: item)
264 }
265
266 private func refile(_ item: AgendaItem, to target: RefileTarget) {
267 guard let (path, offset) = locate(item) else { return }
268 // Targets in the entry's own file come from its text now.
269 let document = session.openDocuments.first { $0.path == path }?.document
270 ?? FileManager.default.contents(atPath: path).map { DocumentState(bytes: [UInt8]($0)) }
271 guard let current = workspace.refileTargets(openPath: path, document: document).first(where: { $0.label == target.label }) else {
272 message = "Can't find \(target.label) any more."
273 return
274 }
275 message = session.refile(to: current, from: (path, offset))
276 tick += 1
277 }
278
279 private func archive(_ item: AgendaItem) {
280 guard let (path, offset) = locate(item) else { return }
281 message = session.archive(from: (path, offset))
282 tick += 1
283 }
284}
285
286extension AgendaItem: Identifiable {
287 public var id: String { "\(path ?? "")|\(headingOffset ?? -1)|\(kind.rawValue)|\(text)" }
288
289 /// An entry, not a time-grid line.
290 var isEntry: Bool { kind != .timeGrid && kind != .currentTime }
291}
292
293/// Org's agenda filter: tags and categories of the entries shown, kept or left out, or the
294/// filter typed as `/` takes it.
295struct FilterSheet: View {
296 @Binding var filter: AgendaFilter
297 let items: [AgendaItem]
298 @State private var typed = ""
299 @Environment(\.dismiss) private var dismiss
300
301 var body: some View {
302 let tags = Set(items.flatMap(\.tags)).sorted()
303 let categories = Set(items.filter { $0.path != nil }.map(\.category)).sorted()
304 NavigationStack {
305 Form {
306 Section {
307 TextField("+work-home", text: $typed)
308 .autocorrectionDisabled()
309 .textInputAutocapitalization(.never)
310 .onSubmit(apply)
311 } footer: {
312 Text("As Org's / filter: +keep and -drop tags or categories, <0:30 for effort, /regexp/.")
313 }
314 if !tags.isEmpty {
315 Section("Tags") { ForEach(tags, id: \.self) { term($0, in: \.tags) } }
316 }
317 if !categories.isEmpty {
318 Section("Categories") { ForEach(categories, id: \.self) { term($0, in: \.categories) } }
319 }
320 }
321 .navigationTitle("Filter")
322 .navigationBarTitleDisplayMode(.inline)
323 .toolbar {
324 ToolbarItem(placement: .cancellationAction) { Button("Clear") { filter = AgendaFilter(); dismiss() } }
325 ToolbarItem(placement: .confirmationAction) { Button("Done") { apply(); dismiss() } }
326 }
327 }
328 .onAppear { typed = filter.description }
329 }
330
331 /// Keep, leave out, or no filter on `name`.
332 private func term(_ name: String, in list: WritableKeyPath<AgendaFilter, [String]>) -> some View {
333 let state = filter[keyPath: list].contains("+" + name) ? 1 : filter[keyPath: list].contains("-" + name) ? -1 : 0
334 return Button {
335 var terms = filter[keyPath: list].filter { $0.dropFirst() != Substring(name) }
336 if state == 0 { terms.append("+" + name) } else if state == 1 { terms.append("-" + name) }
337 filter[keyPath: list] = terms
338 typed = filter.description
339 } label: {
340 HStack {
341 Text(name).foregroundStyle(.primary)
342 Spacer()
343 Text(state == 1 ? "Keep" : state == -1 ? "Leave out" : "").foregroundStyle(.secondary)
344 }
345 }
346 }
347
348 private func apply() {
349 guard typed != filter.description else { return }
350 let tags = Set(items.flatMap(\.tags))
351 let categories = Set(items.map(\.category))
352 filter = AgendaFilter().parse(typed, tags: tags, categories: categories).filter
353 }
354}
355
356/// Where to refile: the targets `org-refile` offers, searchable.
357struct RefileSheet: View {
358 let targets: [RefileTarget]
359 let choose: (RefileTarget?) -> Void
360 @State private var query = ""
361
362 var body: some View {
363 NavigationStack {
364 List(targets.filter { query.isEmpty || $0.label.localizedCaseInsensitiveContains(query) }, id: \.self) { target in
365 Button(target.label) { choose(target) }.foregroundStyle(.primary)
366 }
367 .searchable(text: $query, placement: .navigationBarDrawer(displayMode: .always))
368 .navigationTitle("Refile to")
369 .navigationBarTitleDisplayMode(.inline)
370 .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Cancel") { choose(nil) } } }
371 }
372 }
373}
374#endif