Sources/OrgstarMobile/AgendaScreen.swift

fcb63f5b7ba2983a61307124d831d8093a15a186
orgstar/Sources/OrgstarMobile/AgendaScreen.swift

136 lines · 5658 bytes

  1#if os(iOS)
  2import OrgApp
  3import OrgCore
  4import SwiftUI
  5
  6/// The day/week agenda and the TODO list over the workspace's agenda files.
  7struct AgendaScreen: View {
  8    let workspace: WorkspaceModel
  9    let session: DocumentSession
 10    @Bindable var agenda: AgendaModel
 11    @State private var message: String?
 12    @State private var tick = 0
 13
 14    private enum View: String, CaseIterable { case agenda = "Agenda", todo = "TODO" }
 15
 16    private var view: Binding<View> {
 17        Binding {
 18            if case .todo = agenda.mode { .todo } else { .agenda }
 19        } set: { value in
 20            agenda.mode = value == .todo ? .todo(keywords: nil) : .agenda(span: nil, startOffset: nil)
 21        }
 22    }
 23
 24    private struct Key: Equatable {
 25        var files: Int
 26        var changed: Int?
 27        var edits: Int
 28        var tick: Int
 29        var view: String
 30        var start: Int?
 31    }
 32
 33    var body: some SwiftUI.View {
 34        List {
 35            if case .agenda = agenda.mode {
 36                ForEach(agenda.days, id: \.day) { day in
 37                    Section {
 38                        let items = day.items.filter(\.isEntry)
 39                        ForEach(items.indices, id: \.self) { i in row(items[i]) }
 40                    } header: {
 41                        Text(day.header.split(separator: " ", omittingEmptySubsequences: true).prefix(4).joined(separator: " "))
 42                            .foregroundStyle(day.isToday ? Color.accentColor : .secondary)
 43                    }
 44                }
 45            } else {
 46                ForEach(agenda.list.indices, id: \.self) { i in row(agenda.list[i]) }
 47            }
 48        }
 49        .listStyle(.insetGrouped)
 50        .navigationTitle(agenda.isComputing && agenda.days.isEmpty ? "Agenda…" : "Agenda")
 51        .overlay {
 52            if workspace.rootPaths.isEmpty {
 53                ContentUnavailableView("No folders", systemImage: "folder", description: Text("Add a folder of org files under Folders."))
 54            }
 55        }
 56        .safeAreaInset(edge: .bottom) {
 57            if let message {
 58                Text(message).font(.callout).padding(8).frame(maxWidth: .infinity).background(.bar)
 59            }
 60        }
 61        .toolbar {
 62            ToolbarItem(placement: .principal) {
 63                Picker("View", selection: view) {
 64                    ForEach(View.allCases, id: \.self) { Text($0.rawValue) }
 65                }
 66                .pickerStyle(.segmented)
 67                .fixedSize()
 68            }
 69            if case .agenda = agenda.mode {
 70                ToolbarItem(placement: .topBarLeading) {
 71                    Menu("Days", systemImage: "calendar") {
 72                        Button("Today", systemImage: "calendar.circle") { agenda.showToday() }
 73                        Button("Earlier", systemImage: "chevron.left") { agenda.shift(forward: false) }
 74                        Button("Later", systemImage: "chevron.right") { agenda.shift(forward: true) }
 75                    }
 76                }
 77            }
 78        }
 79        .refreshable {
 80            await workspace.refresh()
 81            await agenda.refresh(open: session.openTexts)
 82        }
 83        .task(id: Key(files: workspace.files.count, changed: workspace.changedFile?.version, edits: session.editVersion, tick: tick,
 84                      view: view.wrappedValue.rawValue, start: agenda.start)) {
 85            await agenda.refresh(open: session.openTexts)
 86        }
 87    }
 88
 89    private func row(_ item: AgendaItem) -> some SwiftUI.View {
 90        VStack(alignment: .leading, spacing: 2) {
 91            HStack(alignment: .firstTextBaseline, spacing: 6) {
 92                if let time = item.time.firstMatch(of: /[0-9]{1,2}:[0-9]{2}(-[0-9]{1,2}:[0-9]{2})?/) {
 93                    Text(time.output.0).font(.callout.monospacedDigit()).foregroundStyle(.secondary)
 94                }
 95                Text(item.text).strikethrough(item.isDone)
 96            }
 97            HStack(spacing: 6) {
 98                Text(item.category)
 99                let extra = item.extra.split(whereSeparator: \.isWhitespace).joined(separator: " ")
100                if !extra.isEmpty { Text(extra) }
101                if !item.tags.isEmpty { Text(item.tags.map { ":" + $0 }.joined() + ":") }
102            }
103            .font(.caption)
104            .foregroundStyle(.secondary)
105        }
106        .swipeActions(edge: .trailing) {
107            if item.todo != nil, !item.isDone, doneKeyword(for: item) != nil {
108                Button("Done", systemImage: "checkmark") { markDone(item) }.tint(.green)
109            }
110        }
111    }
112
113    /// The first done keyword of the item's TODO sequence.
114    private func doneKeyword(for item: AgendaItem) -> String? {
115        guard let todo = item.todo, let path = item.path else { return nil }
116        let text = session.openTexts[path] ?? (try? String(contentsOfFile: path, encoding: .utf8)) ?? ""
117        let settings = OrgParser.parse(text).settings
118        return settings.todoSequences.first { ($0.active + $0.done).contains { $0.name == todo } }?.done.first?.name
119    }
120
121    private func markDone(_ item: AgendaItem) {
122        guard let path = item.path, let start = item.headingOffset, let keyword = doneKeyword(for: item) else { return }
123        switch session.perform(SetTodo(keyword: keyword), path: path, at: start, expect: item.headingLine.map { (start, $0) }) {
124        case .failed(let text): message = text
125        case .prompt: message = "This state change asks for a note; make it on the Mac"
126        default: message = nil
127        }
128        tick += 1
129    }
130}
131
132extension AgendaItem {
133    /// An entry, not a time-grid line.
134    var isEntry: Bool { kind != .timeGrid && kind != .currentTime }
135}
136#endif
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form