| @@ -0,0 +1,256 @@ |
| 1 | #if os(iOS) |
| 2 | import OrgApp |
| 3 | import OrgCore |
| 4 | import OrgPresentation |
| 5 | import 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. |
| 9 | struct 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 | .safeAreaInset(edge: .bottom) { |
| 67 | if let message { |
| 68 | Text(message).font(.footnote).padding(.horizontal, 12).padding(.vertical, 6) |
| 69 | .glassEffect(.regular, in: Capsule()) |
| 70 | .onTapGesture { self.message = nil } |
| 71 | } |
| 72 | } |
| 73 | .navigationTitle(kanban ? "Kanban" : "Entries") |
| 74 | .navigationBarTitleDisplayMode(.inline) |
| 75 | .toolbar { |
| 76 | ToolbarItem(placement: .cancellationAction) { Button("Done", action: done) } |
| 77 | ToolbarItem(placement: .principal) { |
| 78 | Picker("View", selection: $kanban) { |
| 79 | Label("List", systemImage: "list.bullet").tag(false) |
| 80 | Label("Kanban", systemImage: "rectangle.split.3x1").tag(true) |
| 81 | } |
| 82 | .pickerStyle(.segmented) |
| 83 | .frame(width: 180) |
| 84 | } |
| 85 | } |
| 86 | .sheet(item: $prompt) { request in |
| 87 | PromptSheet(prompt: request.prompt) { answer in |
| 88 | prompt = nil |
| 89 | request.reply(answer) |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | .onAppear { matchText = match } |
| 94 | .task(id: RefreshKey(match: match, files: workspace.files.count, changed: workspace.changedFile?.version, edits: session.editVersion, |
| 95 | tick: tick, settings: model.settingsVersion)) { |
| 96 | agenda.includeSubfolders = OrgPreferences.agendaSubfolders() |
| 97 | agenda.agendaFiles = OrgPreferences.agendaFiles() |
| 98 | let result = await agenda.board(open: session.openTexts, match: match) |
| 99 | entries = result.entries |
| 100 | keywords = result.keywords |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // MARK: - List |
| 105 | |
| 106 | private var list: some View { |
| 107 | List { |
| 108 | if entries.isEmpty { Text("No entries").foregroundStyle(.secondary) } |
| 109 | ForEach(sorted) { entry in |
| 110 | Button { open(entry) } label: { |
| 111 | HStack(alignment: .firstTextBaseline, spacing: 8) { |
| 112 | if let todo = entry.todo { |
| 113 | Text(todo).font(.caption.weight(.bold)).foregroundStyle(keywordColor(todo, done: entry.isDone)) |
| 114 | } |
| 115 | VStack(alignment: .leading, spacing: 2) { |
| 116 | Text((entry.priority.map { "[#\($0)] " } ?? "") + entry.title).foregroundStyle(.primary) |
| 117 | details(entry) |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | .tint(.primary) |
| 122 | .contextMenu { menu(entry) } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // MARK: - Kanban |
| 128 | |
| 129 | private var board: some View { |
| 130 | ScrollView(.horizontal) { |
| 131 | HStack(alignment: .top, spacing: 12) { |
| 132 | ForEach(keywords, id: \.self) { keyword in column(keyword) } |
| 133 | } |
| 134 | .padding(12) |
| 135 | .frame(maxHeight: .infinity, alignment: .top) |
| 136 | } |
| 137 | .background(Color(.systemGroupedBackground)) |
| 138 | } |
| 139 | |
| 140 | private func column(_ keyword: String) -> some View { |
| 141 | let cards = entries.filter { $0.todo == keyword } |
| 142 | return VStack(alignment: .leading, spacing: 8) { |
| 143 | HStack { |
| 144 | Text(keyword).font(.headline).foregroundStyle(keywordColor(keyword, done: cards.first?.isDone ?? false)) |
| 145 | Text("\(cards.count)").foregroundStyle(.secondary) |
| 146 | } |
| 147 | ScrollView { |
| 148 | VStack(alignment: .leading, spacing: 6) { |
| 149 | ForEach(cards) { entry in |
| 150 | card(entry) |
| 151 | .onTapGesture { open(entry) } |
| 152 | .draggable(entry.id) |
| 153 | .contextMenu { menu(entry) } |
| 154 | .accessibilityElement(children: .combine) |
| 155 | .accessibilityAddTraits(.isButton) |
| 156 | .accessibilityAction { open(entry) } |
| 157 | .accessibilityActions { |
| 158 | ForEach(keywords.filter { $0 != keyword }, id: \.self) { other in |
| 159 | Button("Move to \(other)") { setTodo(entry, other) } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | .frame(width: 260, alignment: .topLeading) |
| 167 | .frame(maxHeight: .infinity, alignment: .top) |
| 168 | .padding(8) |
| 169 | .background(RoundedRectangle(cornerRadius: 10).fill(Color(.secondarySystemFill))) |
| 170 | .dropDestination(for: String.self) { ids, _ in |
| 171 | for id in ids { |
| 172 | if let entry = entries.first(where: { $0.id == id }), entry.todo != keyword { setTodo(entry, keyword) } |
| 173 | } |
| 174 | return true |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private func card(_ entry: BoardEntry) -> some View { |
| 179 | VStack(alignment: .leading, spacing: 4) { |
| 180 | Text((entry.priority.map { "[#\($0)] " } ?? "") + entry.title).lineLimit(3) |
| 181 | details(entry) |
| 182 | } |
| 183 | .padding(10) |
| 184 | .frame(maxWidth: .infinity, alignment: .leading) |
| 185 | .background(RoundedRectangle(cornerRadius: 8).fill(Color(.systemBackground))) |
| 186 | .contentShape(RoundedRectangle(cornerRadius: 8)) |
| 187 | } |
| 188 | |
| 189 | @ViewBuilder |
| 190 | private func details(_ entry: BoardEntry) -> some View { |
| 191 | HStack(spacing: 6) { |
| 192 | Text(entry.category).font(.caption).foregroundStyle(.secondary) |
| 193 | if !entry.tags.isEmpty { |
| 194 | Text(entry.tags.joined(separator: " ")).font(.caption).foregroundStyle(.secondary).lineLimit(1) |
| 195 | } |
| 196 | } |
| 197 | if let deadline = entry.deadline { |
| 198 | Text("Deadline \(deadline)").font(.caption).foregroundStyle(.red) |
| 199 | } else if let scheduled = entry.scheduled { |
| 200 | Text("Scheduled \(scheduled)").font(.caption).foregroundStyle(.secondary) |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | @ViewBuilder |
| 205 | private func menu(_ entry: BoardEntry) -> some View { |
| 206 | Menu("Move To", systemImage: "arrow.right.circle") { |
| 207 | ForEach(keywords.filter { $0 != entry.todo }, id: \.self) { keyword in Button(keyword) { setTodo(entry, keyword) } } |
| 208 | if entry.todo != nil { |
| 209 | Divider() |
| 210 | Button("None") { setTodo(entry, nil) } |
| 211 | } |
| 212 | } |
| 213 | Button("Set Property…", systemImage: "list.bullet.rectangle") { askProperty(entry) } |
| 214 | Button("Open", systemImage: "doc.text") { open(entry) } |
| 215 | } |
| 216 | |
| 217 | private func keywordColor(_ keyword: String, done: Bool) -> Color { |
| 218 | theme.keywordColor(keyword, done: done).map(Color.init(uiColor:)) ?? (done ? .green : .red) |
| 219 | } |
| 220 | |
| 221 | // MARK: - Editing |
| 222 | |
| 223 | private func perform(_ command: any OrgCommand, on entry: BoardEntry, answers: [String: String] = [:]) { |
| 224 | switch session.perform(command, path: entry.path, at: entry.headingOffset, expect: (entry.headingOffset, entry.headingLine), answers: answers) { |
| 225 | case .failed(let text): message = text |
| 226 | case .prompt(let request): |
| 227 | // A state note, or what else the change asks. |
| 228 | prompt = EditorScreen.PromptRequest(prompt: request) { answer in |
| 229 | guard let answer else { return } |
| 230 | perform(command, on: entry, answers: answers.merging([request.key: answer]) { $1 }) |
| 231 | } |
| 232 | case .external(let request): message = MobileEditor.unavailable(request, title: command.title) |
| 233 | case .commit: message = nil |
| 234 | } |
| 235 | tick += 1 |
| 236 | } |
| 237 | |
| 238 | private func setTodo(_ entry: BoardEntry, _ keyword: String?) { |
| 239 | perform(SetTodo(keyword: keyword), on: entry) |
| 240 | } |
| 241 | |
| 242 | private func askProperty(_ entry: BoardEntry) { |
| 243 | prompt = EditorScreen.PromptRequest(prompt: Prompt(key: "property", message: "Property (NAME value):")) { answer in |
| 244 | guard let answer, let space = answer.firstIndex(of: " ") else { return } |
| 245 | let key = String(answer[..<space]) |
| 246 | let value = String(answer[answer.index(after: space)...]).trimmingCharacters(in: .whitespaces) |
| 247 | perform(SetProperty(key: key, value: value), on: entry) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | private func open(_ entry: BoardEntry) { |
| 252 | done() |
| 253 | model.openRequest = .offset(entry.path, entry.headingOffset) |
| 254 | } |
| 255 | } |
| 256 | #endif |