Bulk agenda actions on iOS !269
2 files changed, +164 −3
Layout: unified · split
Sources/OrgstarMobile/AgendaScreen.swift +161 −2
| @@ -41,6 +41,29 @@ struct AgendaScreen: View, Equatable { | |||
| 41 | @State private var moved = 0 | 41 | @State private var moved = 0 |
| 42 | /// `org-agenda-log-mode`: closed entries and clocked time on each day. | 42 | /// `org-agenda-log-mode`: closed entries and clocked time on each day. |
| 43 | @State private var logMode = false | 43 | @State private var logMode = false |
| 44 | /// Selecting entries for bulk actions, as `m` marks them in Org's agenda, and those marked. | ||
| 45 | @State private var selecting = false | ||
| 46 | @State private var marked: [Mark] = [] | ||
| 47 | @State private var bulkRefiling = false | ||
| 48 | @State private var bulkArchiving = false | ||
| 49 | @State private var bulkPlanning: BulkPlanning? | ||
| 50 | |||
| 51 | /// A marked entry: its heading line, to find it again. | ||
| 52 | private struct Mark: Hashable { | ||
| 53 | let path: String | ||
| 54 | let line: String | ||
| 55 | let offset: Int | ||
| 56 | |||
| 57 | init?(_ item: AgendaItem?) { | ||
| 58 | guard let item, let path = item.path, let line = item.headingLine, let offset = item.headingOffset else { return nil } | ||
| 59 | (self.path, self.line, self.offset) = (path, line, offset) | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | private enum BulkPlanning: String, Identifiable { | ||
| 64 | case schedule, deadline | ||
| 65 | var id: String { rawValue } | ||
| 66 | } | ||
| 44 | 67 | ||
| 45 | /// A match typed for the tag and property view, or a search string. | 68 | /// A match typed for the tag and property view, or a search string. |
| 46 | private enum Asking: Identifiable { | 69 | private enum Asking: Identifiable { |
| @@ -141,7 +164,36 @@ struct AgendaScreen: View, Equatable { | |||
| 141 | } | 164 | } |
| 142 | } | 165 | } |
| 143 | .safeAreaInset(edge: .bottom) { | 166 | .safeAreaInset(edge: .bottom) { |
| 144 | if !workspace.rootPaths.isEmpty { bottomBar } | 167 | if !workspace.rootPaths.isEmpty { |
| 168 | if selecting { bulkBar } else { bottomBar } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | .sheet(isPresented: $bulkRefiling) { | ||
| 172 | RefileSheet(targets: workspace.refileTargets(openPath: session.path, document: session.document)) { target in | ||
| 173 | bulkRefiling = false | ||
| 174 | guard let target else { return } | ||
| 175 | bulk { path, offset in | ||
| 176 | // Targets in the entry's own file from its text now, as earlier moves shift them. | ||
| 177 | let document = session.openDocuments.first { $0.path == path }?.document | ||
| 178 | ?? FileManager.default.contents(atPath: path).map { DocumentState(bytes: [UInt8]($0)) } | ||
| 179 | guard let current = workspace.refileTargets(openPath: path, document: document).first(where: { $0.label == target.label }) else { | ||
| 180 | return "Can't find \(target.label) any more." | ||
| 181 | } | ||
| 182 | return session.refile(to: current, from: (path, offset)) | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | .sheet(item: $bulkPlanning) { kind in | ||
| 187 | DateSheet(title: kind == .deadline ? "Deadline" : "Schedule", action: "Set", initial: date(agenda.today)) { picked in | ||
| 188 | bulkPlanning = nil | ||
| 189 | guard let picked else { return } | ||
| 190 | let d = Days.date(Days.absolute(picked, calendar: .current)) | ||
| 191 | let answer = String(format: "%04d-%02d-%02d", d.year, d.month, d.day) | ||
| 192 | bulk { perform(SetPlanning(deadline: kind == .deadline), $0, $1, answers: ["date": answer]) } | ||
| 193 | } | ||
| 194 | } | ||
| 195 | .confirmationDialog("Archive \(marked.count) \(marked.count == 1 ? "entry" : "entries")?", isPresented: $bulkArchiving, titleVisibility: .visible) { | ||
| 196 | Button("Archive", role: .destructive) { bulk { session.archive(from: ($0, $1)) } } | ||
| 145 | } | 197 | } |
| 146 | .toolbar { | 198 | .toolbar { |
| 147 | ToolbarItem(placement: .topBarLeading) { | 199 | ToolbarItem(placement: .topBarLeading) { |
| @@ -439,6 +491,43 @@ struct AgendaScreen: View, Equatable { | |||
| 439 | if range == nil { show(nil, around: agenda.today) } else { show(range, around: agenda.today) } | 491 | if range == nil { show(nil, around: agenda.today) } else { show(range, around: agenda.today) } |
| 440 | } | 492 | } |
| 441 | 493 | ||
| 494 | /// The bar while selecting: the count, the bulk actions (`B` in Org's agenda) and Done. | ||
| 495 | private var bulkBar: some View { | ||
| 496 | VStack(spacing: 8) { | ||
| 497 | if let message { | ||
| 498 | Text(message).font(.footnote).padding(.horizontal, 12).padding(.vertical, 6) | ||
| 499 | .glassEffect(.regular, in: Capsule()) | ||
| 500 | .onTapGesture { self.message = nil } | ||
| 501 | } | ||
| 502 | HStack(spacing: 12) { | ||
| 503 | Text("\(marked.count) selected").font(.footnote.weight(.semibold).monospacedDigit()).padding(.leading, 12) | ||
| 504 | Menu { | ||
| 505 | Button("TODO State…", systemImage: "checkmark.circle") { askBulkTodo() } | ||
| 506 | Button("Add Tag…", systemImage: "tag") { askBulkTag(on: true) } | ||
| 507 | Button("Remove Tag…", systemImage: "tag.slash") { askBulkTag(on: false) } | ||
| 508 | Button("Schedule…", systemImage: "calendar") { bulkPlanning = .schedule } | ||
| 509 | Button("Deadline…", systemImage: "calendar.badge.exclamationmark") { bulkPlanning = .deadline } | ||
| 510 | Divider() | ||
| 511 | Button("Refile…", systemImage: "arrow.turn.down.right") { bulkRefiling = true } | ||
| 512 | Button("Archive…", systemImage: "archivebox", role: .destructive) { bulkArchiving = true } | ||
| 513 | } label: { | ||
| 514 | Text("Actions").font(.body.weight(.semibold)).frame(minHeight: 44) | ||
| 515 | } | ||
| 516 | .disabled(marked.isEmpty) | ||
| 517 | Button("Done") { | ||
| 518 | selecting = false | ||
| 519 | marked = [] | ||
| 520 | } | ||
| 521 | .font(.body.weight(.semibold)) | ||
| 522 | .frame(minHeight: 44) | ||
| 523 | .padding(.trailing, 12) | ||
| 524 | } | ||
| 525 | .glassEffect(.regular.interactive(), in: Capsule()) | ||
| 526 | } | ||
| 527 | .padding(.bottom, 8) | ||
| 528 | .frame(maxWidth: .infinity) | ||
| 529 | } | ||
| 530 | |||
| 442 | private var bottomBar: some View { | 531 | private var bottomBar: some View { |
| 443 | VStack(spacing: 8) { | 532 | VStack(spacing: 8) { |
| 444 | if let status = message ?? (filter.isEmpty ? nil : "Filter: " + filter.description) { | 533 | if let status = message ?? (filter.isEmpty ? nil : "Filter: " + filter.description) { |
| @@ -452,6 +541,7 @@ struct AgendaScreen: View, Equatable { | |||
| 452 | barButton("Earlier", "chevron.left") { step(forward: false) } | 541 | barButton("Earlier", "chevron.left") { step(forward: false) } |
| 453 | } | 542 | } |
| 454 | barButton("Filter", filter.isEmpty ? "line.3.horizontal.decrease" : "line.3.horizontal.decrease.circle.fill") { showingFilter = true } | 543 | barButton("Filter", filter.isEmpty ? "line.3.horizontal.decrease" : "line.3.horizontal.decrease.circle.fill") { showingFilter = true } |
| 544 | barButton("Select", "checkmark.circle") { selecting = true } | ||
| 455 | if isAgenda { | 545 | if isAgenda { |
| 456 | barButton("Today", "smallcircle.filled.circle") { goToday() } | 546 | barButton("Today", "smallcircle.filled.circle") { goToday() } |
| 457 | barButton("Go to Date", "calendar") { jumping = true } | 547 | barButton("Go to Date", "calendar") { jumping = true } |
| @@ -493,7 +583,24 @@ struct AgendaScreen: View, Equatable { | |||
| 493 | 583 | ||
| 494 | private func row(_ row: AgendaRow, day: Int) -> some View { | 584 | private func row(_ row: AgendaRow, day: Int) -> some View { |
| 495 | Group { | 585 | Group { |
| 496 | if let item = row.item, let path = item.path, let offset = item.headingOffset ?? item.markerOffset { | 586 | if selecting { |
| 587 | if let mark = Mark(row.item) { | ||
| 588 | Button { | ||
| 589 | if let index = marked.firstIndex(of: mark) { marked.remove(at: index) } else { marked.append(mark) } | ||
| 590 | } label: { | ||
| 591 | HStack(spacing: 10) { | ||
| 592 | Image(systemName: marked.contains(mark) ? "checkmark.circle.fill" : "circle") | ||
| 593 | .foregroundStyle(marked.contains(mark) ? Color.accentColor : .secondary) | ||
| 594 | .font(.title3) | ||
| 595 | AgendaRowView(row: row, day: day, theme: theme, dark: isDark) | ||
| 596 | } | ||
| 597 | } | ||
| 598 | .tint(.primary) | ||
| 599 | .accessibilityAddTraits(marked.contains(mark) ? .isSelected : []) | ||
| 600 | } else { | ||
| 601 | AgendaRowView(row: row, day: day, theme: theme, dark: isDark).opacity(0.5) | ||
| 602 | } | ||
| 603 | } else if let item = row.item, let path = item.path, let offset = item.headingOffset ?? item.markerOffset { | ||
| 497 | NavigationLink(value: ReaderRoute.offset(path, offset)) { AgendaRowView(row: row, day: day, theme: theme, dark: isDark) } | 604 | NavigationLink(value: ReaderRoute.offset(path, offset)) { AgendaRowView(row: row, day: day, theme: theme, dark: isDark) } |
| 498 | } else if let event = row.event { | 605 | } else if let event = row.event { |
| 499 | // Calendar can't be asked for one event by URL; it opens on the event's day. | 606 | // Calendar can't be asked for one event by URL; it opens on the event's day. |
| @@ -526,6 +633,12 @@ struct AgendaScreen: View, Equatable { | |||
| 526 | } | 633 | } |
| 527 | .contextMenu { | 634 | .contextMenu { |
| 528 | if let item = row.item, item.path != nil { | 635 | if let item = row.item, item.path != nil { |
| 636 | if let mark = Mark(item) { | ||
| 637 | Button("Select", systemImage: "checkmark.circle") { | ||
| 638 | selecting = true | ||
| 639 | if !marked.contains(mark) { marked.append(mark) } | ||
| 640 | } | ||
| 641 | } | ||
| 529 | Button("TODO State…", systemImage: "checkmark.circle") { run("org.todo.cycle", on: item) } | 642 | Button("TODO State…", systemImage: "checkmark.circle") { run("org.todo.cycle", on: item) } |
| 530 | if canReschedule(row) { | 643 | if canReschedule(row) { |
| 531 | Menu("Reschedule", systemImage: "calendar.badge.clock") { | 644 | Menu("Reschedule", systemImage: "calendar.badge.clock") { |
| @@ -617,6 +730,52 @@ struct AgendaScreen: View, Equatable { | |||
| 617 | tick += 1 | 730 | tick += 1 |
| 618 | } | 731 | } |
| 619 | 732 | ||
| 733 | // MARK: - Bulk actions | ||
| 734 | |||
| 735 | /// Runs `action` on each marked entry, found again first; later entries in a file first, | ||
| 736 | /// so earlier ones keep their place. Selection ends; the last message shows. | ||
| 737 | private func bulk(_ action: (String, Int) -> String?) { | ||
| 738 | var last: String? | ||
| 739 | var failed = 0 | ||
| 740 | for mark in marked.sorted(by: { ($0.path, $1.offset) < ($1.path, $0.offset) }) { | ||
| 741 | guard let offset = session.locateHeading(path: mark.path, line: mark.line, near: mark.offset) else { | ||
| 742 | failed += 1 | ||
| 743 | continue | ||
| 744 | } | ||
| 745 | last = action(mark.path, offset) | ||
| 746 | } | ||
| 747 | message = failed > 0 ? "\(failed) selected \(failed == 1 ? "entry" : "entries") changed and \(failed == 1 ? "was" : "were") skipped" : last | ||
| 748 | marked = [] | ||
| 749 | selecting = false | ||
| 750 | tick += 1 | ||
| 751 | } | ||
| 752 | |||
| 753 | /// `command` at the heading at `offset` in `path`; a message when it doesn't apply. | ||
| 754 | private func perform(_ command: any OrgCommand, _ path: String, _ offset: Int, answers: [String: String] = [:]) -> String? { | ||
| 755 | switch session.perform(command, path: path, at: offset, expect: nil, answers: answers) { | ||
| 756 | case .failed(let text): text | ||
| 757 | case .prompt: "\(command.title) needs an answer; run it on the entry itself" | ||
| 758 | case .external(let request): MobileEditor.unavailable(request, title: command.title) | ||
| 759 | case .commit: nil | ||
| 760 | } | ||
| 761 | } | ||
| 762 | |||
| 763 | private func askBulkTodo() { | ||
| 764 | let keywords = OrgPreferences.orgSettings().todoSequences.flatMap { $0.active.map(\.name) + $0.done.map(\.name) } | ||
| 765 | prompt = EditorScreen.PromptRequest(prompt: Prompt(key: "todo", message: "TODO state (empty for none):", choices: keywords)) { answer in | ||
| 766 | guard let answer else { return } | ||
| 767 | let word = answer.trimmingCharacters(in: .whitespaces) | ||
| 768 | bulk { perform(SetTodo(keyword: word.isEmpty ? nil : word), $0, $1) } | ||
| 769 | } | ||
| 770 | } | ||
| 771 | |||
| 772 | private func askBulkTag(on: Bool) { | ||
| 773 | prompt = EditorScreen.PromptRequest(prompt: Prompt(key: "tag", message: on ? "Tag to add:" : "Tag to remove:")) { answer in | ||
| 774 | guard let tag = answer?.trimmingCharacters(in: .whitespaces), !tag.isEmpty else { return } | ||
| 775 | bulk { perform(ToggleTag(tag: tag, on: on), $0, $1) } | ||
| 776 | } | ||
| 777 | } | ||
| 778 | |||
| 620 | private func markDone(_ item: AgendaItem) { | 779 | private func markDone(_ item: AgendaItem) { |
| 621 | guard let keyword = item.doneKeyword else { return } | 780 | guard let keyword = item.doneKeyword else { return } |
| 622 | run(SetTodo(keyword: keyword), on: item) | 781 | run(SetTodo(keyword: keyword), on: item) |
docs/manual/guide/07-agenda.org +3 −1
| @@ -613,6 +613,8 @@ Each action finds the entry again by its heading line before it changes anything | |||
| 613 | 613 | ||
| 614 | =B= offers =$= archive, =r= refile, =t= set a TODO state (typed; empty for none), =+= add a tag, =-= remove a tag, =s= schedule and =d= set a deadline. Type the letter and press OK. Entries that changed since they were marked are skipped and counted in the status line. An action that needs a further answer per entry, such as a state-change note, is not run in bulk; the status line tells you to run it on the entry. | 614 | =B= offers =$= archive, =r= refile, =t= set a TODO state (typed; empty for none), =+= add a tag, =-= remove a tag, =s= schedule and =d= set a deadline. Type the letter and press OK. Entries that changed since they were marked are skipped and counted in the status line. An action that needs a further answer per entry, such as a state-change note, is not run in bulk; the status line tells you to run it on the entry. |
| 615 | 615 | ||
| 616 | On iOS, tap Select in the bar at the bottom, or Select in an entry's menu, then tap entries to mark them. The bar then shows how many are selected, Actions and Done. Actions offers the same actions: TODO State…, Add Tag…, Remove Tag…, Schedule…, Deadline…, Refile… and Archive…, which asks first. After an action, or Done, the selection ends. | ||
| 617 | |||
| 616 | * Reminders | 618 | * Reminders |
| 617 | 619 | ||
| 618 | orgstar can post a notification before each timed entry, as =org-agenda-to-appt= hands entries to =appt=. Reminders cover the next 7 days and include deadline, scheduled, plain timestamp and range lines that have a time of day and are not done. Overdue items and upcoming-deadline warnings don't get reminders. | 620 | orgstar can post a notification before each timed entry, as =org-agenda-to-appt= hands entries to =appt=. Reminders cover the next 7 days and include deadline, scheduled, plain timestamp and range lines that have a time of day and are not done. Overdue items and upcoming-deadline warnings don't get reminders. |
| @@ -669,7 +671,7 @@ The Agenda tab shows the same views from the same files, using the settings in t | |||
| 669 | - Touch and hold an entry for TODO State…, Reschedule (Tomorrow, Next Week, Pick Date…), Schedule…, Deadline…, Tags…, Priority…, Clock In, Refile…, Archive… and Delete…, which removes the heading and everything under it. | 671 | - Touch and hold an entry for TODO State…, Reschedule (Tomorrow, Next Week, Pick Date…), Schedule…, Deadline…, Tags…, Priority…, Clock In, Refile…, Archive… and Delete…, which removes the heading and everything under it. |
| 670 | - Marking an entry done and rescheduling one give haptic feedback. | 672 | - Marking an entry done and rescheduling one give haptic feedback. |
| 671 | - Log Mode, in the span menu, turns log mode on and off. The widgets never show log mode's entries. | 673 | - Log Mode, in the span menu, turns log mode on and off. The widgets never show log mode's entries. |
| 672 | - There are no keyboard commands or bulk marks. | 674 | - There are no keyboard commands; bulk actions use Select instead of marks (see [[*Bulk actions][Bulk actions]]). |
| 673 | - Reminders are set while the app is open. iOS allows at most 64 pending notifications and the app can't add more while it isn't running, so the agenda's footer says how far ahead they go and asks you to open orgstar to set later ones. | 675 | - Reminders are set while the app is open. iOS allows at most 64 pending notifications and the app can't add more while it isn't running, so the agenda's footer says how far ahead they go and asks you to open orgstar to set later ones. |
| 674 | 676 | ||
| 675 | The Today and Up Next widgets show the agenda on the Home Screen and the Lock Screen; see [[file:14-ios.org][iPhone and iPad]]. | 677 | The Today and Up Next widgets show the agenda on the Home Screen and the Lock Screen; see [[file:14-ios.org][iPhone and iPad]]. |