Sources/OrgstarMobile/BoardScreen.swift
cmc Agenda prompts, Week of titles and notification status through the catalog
e633d6bf48 18 hours ago251 lines · 10602 bytes
11 symbols in this file
1#if os(iOS)
2import OrgApp
3import OrgCore
4import OrgPresentation
5import SwiftUI
6
7/// The agenda files' entries as a list, or as a kanban board by TODO keyword, as on the Mac.
8/// Dragging a card to another column, or Move To in its menu, sets its keyword.
9struct BoardScreen: View {
10 let workspace: WorkspaceModel
11 let session: DocumentSession
12 let done: () -> Void
13 @Environment(MobileModel.self) private var model
14 @State private var agenda: AgendaModel
15 @State private var entries: [BoardEntry] = []
16 @State private var keywords: [String] = []
17 @State private var message: String?
18 @State private var tick = 0
19 @State private var prompt: EditorScreen.PromptRequest?
20 @State private var matchText = ""
21 @AppStorage("boardKanban") private var kanban = false
22 @AppStorage("boardMatch") private var match = ""
23
24 init(workspace: WorkspaceModel, session: DocumentSession, done: @escaping () -> Void) {
25 self.workspace = workspace
26 self.session = session
27 self.done = done
28 _agenda = State(initialValue: AgendaModel(workspace: workspace))
29 }
30
31 private var theme: MobileTheme { MobileTheme(model.theme) }
32
33 private struct RefreshKey: Hashable {
34 var match: String
35 var files: Int
36 var changed: Int?
37 var edits: Int
38 var tick: Int
39 var settings: Int
40 }
41
42 /// Entries by keyword order, then title.
43 private var sorted: [BoardEntry] {
44 entries.enumerated().sorted { a, b in
45 let ka = a.element.todo.flatMap { keywords.firstIndex(of: $0) } ?? Int.max
46 let kb = b.element.todo.flatMap { keywords.firstIndex(of: $0) } ?? Int.max
47 if ka != kb { return ka < kb }
48 if a.element.title != b.element.title { return a.element.title.localizedStandardCompare(b.element.title) == .orderedAscending }
49 return a.offset < b.offset
50 }.map(\.element)
51 }
52
53 var body: some View {
54 NavigationStack {
55 VStack(spacing: 0) {
56 TextField("Match: +work/!TODO", text: $matchText)
57 .textInputAutocapitalization(.never)
58 .autocorrectionDisabled()
59 .submitLabel(.search)
60 .onSubmit { match = matchText }
61 .padding(.horizontal, 12)
62 .padding(.vertical, 8)
63 .background(.bar)
64 if kanban { board } else { list }
65 }
66 .message($message)
67 .navigationTitle("Board")
68 .navigationBarTitleDisplayMode(.inline)
69 .toolbar {
70 ToolbarItem(placement: .confirmationAction) { Button("Done", action: done) }
71 ToolbarItem(placement: .principal) {
72 Picker("View", selection: $kanban) {
73 Label("List", systemImage: "list.bullet").tag(false)
74 Label("Kanban", systemImage: "rectangle.split.3x1").tag(true)
75 }
76 .pickerStyle(.segmented)
77 .frame(width: 180)
78 }
79 }
80 .sheet(item: $prompt) { request in
81 PromptSheet(prompt: request.prompt) { answer in
82 prompt = nil
83 request.reply(answer)
84 }
85 }
86 }
87 .onAppear { matchText = match }
88 .task(id: RefreshKey(match: match, files: workspace.files.count, changed: workspace.changedFile?.version, edits: session.editVersion,
89 tick: tick, settings: model.settingsVersion)) {
90 agenda.includeSubfolders = OrgPreferences.agendaSubfolders()
91 agenda.excludeArchives = OrgPreferences.agendaExcludeArchives()
92 agenda.agendaFiles = OrgPreferences.agendaFiles()
93 let result = await agenda.board(open: session.openTexts, match: match)
94 entries = result.entries
95 keywords = result.keywords
96 }
97 }
98
99 // MARK: - List
100
101 private var list: some View {
102 List {
103 if entries.isEmpty { Text("No entries").foregroundStyle(.secondary) }
104 ForEach(sorted) { entry in
105 Button { open(entry) } label: {
106 HStack(alignment: .firstTextBaseline, spacing: 8) {
107 if let todo = entry.todo {
108 Text(todo).font(.caption.weight(.bold)).foregroundStyle(keywordColor(todo, done: entry.isDone))
109 }
110 VStack(alignment: .leading, spacing: 2) {
111 Text((entry.priority.map { "[#\($0)] " } ?? "") + entry.title).foregroundStyle(.primary)
112 details(entry)
113 }
114 }
115 }
116 .tint(.primary)
117 .contextMenu { menu(entry) }
118 }
119 }
120 }
121
122 // MARK: - Kanban
123
124 private var board: some View {
125 ScrollView(.horizontal) {
126 HStack(alignment: .top, spacing: 12) {
127 ForEach(keywords, id: \.self) { keyword in column(keyword) }
128 }
129 .padding(12)
130 .frame(maxHeight: .infinity, alignment: .top)
131 }
132 .background(Color(.systemGroupedBackground))
133 }
134
135 private func column(_ keyword: String) -> some View {
136 let cards = entries.filter { $0.todo == keyword }
137 return VStack(alignment: .leading, spacing: 8) {
138 HStack {
139 Text(keyword).font(.headline).foregroundStyle(keywordColor(keyword, done: cards.first?.isDone ?? false))
140 Text("\(cards.count)").foregroundStyle(.secondary)
141 }
142 ScrollView {
143 VStack(alignment: .leading, spacing: 6) {
144 ForEach(cards) { entry in
145 card(entry)
146 .onTapGesture { open(entry) }
147 .draggable(entry.id)
148 .contextMenu { menu(entry) }
149 .accessibilityElement(children: .combine)
150 .accessibilityAddTraits(.isButton)
151 .accessibilityAction { open(entry) }
152 .accessibilityActions {
153 ForEach(keywords.filter { $0 != keyword }, id: \.self) { other in
154 Button("Move to \(other)") { setTodo(entry, other) }
155 }
156 }
157 }
158 }
159 }
160 }
161 .frame(width: 260, alignment: .topLeading)
162 .frame(maxHeight: .infinity, alignment: .top)
163 .padding(8)
164 .background(RoundedRectangle(cornerRadius: 10).fill(Color(.secondarySystemFill)))
165 .dropDestination(for: String.self) { ids, _ in
166 for id in ids {
167 if let entry = entries.first(where: { $0.id == id }), entry.todo != keyword { setTodo(entry, keyword) }
168 }
169 return true
170 }
171 }
172
173 private func card(_ entry: BoardEntry) -> some View {
174 VStack(alignment: .leading, spacing: 4) {
175 Text((entry.priority.map { "[#\($0)] " } ?? "") + entry.title).lineLimit(3)
176 details(entry)
177 }
178 .padding(10)
179 .frame(maxWidth: .infinity, alignment: .leading)
180 .background(RoundedRectangle(cornerRadius: 8).fill(Color(.systemBackground)))
181 .contentShape(RoundedRectangle(cornerRadius: 8))
182 }
183
184 @ViewBuilder
185 private func details(_ entry: BoardEntry) -> some View {
186 HStack(spacing: 6) {
187 Text(entry.category).font(.caption).foregroundStyle(.secondary)
188 if !entry.tags.isEmpty {
189 Text(entry.tags.joined(separator: " ")).font(.caption).foregroundStyle(.secondary).lineLimit(1)
190 }
191 }
192 if let deadline = entry.deadline {
193 Text("Deadline \(deadline)").font(.caption).foregroundStyle(.red)
194 } else if let scheduled = entry.scheduled {
195 Text("Scheduled \(scheduled)").font(.caption).foregroundStyle(.secondary)
196 }
197 }
198
199 @ViewBuilder
200 private func menu(_ entry: BoardEntry) -> some View {
201 Menu("Move To", systemImage: "arrow.right.circle") {
202 ForEach(keywords.filter { $0 != entry.todo }, id: \.self) { keyword in Button(keyword) { setTodo(entry, keyword) } }
203 if entry.todo != nil {
204 Divider()
205 Button(String(localized: "none.state", defaultValue: "None", comment: "No TODO state")) { setTodo(entry, nil) }
206 }
207 }
208 Button("Set Property…", systemImage: "list.bullet.rectangle") { askProperty(entry) }
209 Button("Open", systemImage: "doc.text") { open(entry) }
210 }
211
212 private func keywordColor(_ keyword: String, done: Bool) -> Color {
213 theme.keywordColor(keyword, done: done).map(Color.init(uiColor:)) ?? (done ? .green : .red)
214 }
215
216 // MARK: - Editing
217
218 private func perform(_ command: any OrgCommand, on entry: BoardEntry, answers: [String: String] = [:]) {
219 switch session.perform(command, path: entry.path, at: entry.headingOffset, expect: (entry.headingOffset, entry.headingLine), answers: answers) {
220 case .failed(let text): message = text
221 case .prompt(let request):
222 // A state note, or what else the change asks.
223 prompt = EditorScreen.PromptRequest(prompt: request) { answer in
224 guard let answer else { return }
225 perform(command, on: entry, answers: answers.merging([request.key: answer]) { $1 })
226 }
227 case .external(let request): message = MobileEditor.unavailable(request, title: command.title)
228 case .commit: message = nil
229 }
230 tick += 1
231 }
232
233 private func setTodo(_ entry: BoardEntry, _ keyword: String?) {
234 perform(SetTodo(keyword: keyword), on: entry)
235 }
236
237 private func askProperty(_ entry: BoardEntry) {
238 prompt = EditorScreen.PromptRequest(prompt: Prompt(key: "property", message: String(localized: "Property (NAME value):"))) { answer in
239 guard let answer, let space = answer.firstIndex(of: " ") else { return }
240 let key = String(answer[..<space])
241 let value = String(answer[answer.index(after: space)...]).trimmingCharacters(in: .whitespaces)
242 perform(SetProperty(key: key, value: value), on: entry)
243 }
244 }
245
246 private func open(_ entry: BoardEntry) {
247 done()
248 model.openRequest = .offset(entry.path, entry.headingOffset)
249 }
250}
251#endif