| @@ -0,0 +1,136 @@ |
| 1 | #if os(iOS) |
| 2 | import OrgApp |
| 3 | import OrgCore |
| 4 | import SwiftUI |
| 5 | |
| 6 | /// `org-columns` on iOS: the entries and their columns, with summaries, as on the Mac. Tap a |
| 7 | /// cell to select it; the bar edits it (`e`), moves to the previous or next allowed value |
| 8 | /// (`p`, `n`), or goes to the entry. |
| 9 | struct ColumnViewScreen: View { |
| 10 | @State var view: ColumnView.View |
| 11 | let session: DocumentSession |
| 12 | let path: String |
| 13 | let jump: (Int) -> Void |
| 14 | @Environment(\.dismiss) private var dismiss |
| 15 | @State private var selected: (row: Int, column: Int)? |
| 16 | @State private var message: String? |
| 17 | @State private var prompt: EditorScreen.PromptRequest? |
| 18 | |
| 19 | var body: some View { |
| 20 | NavigationStack { |
| 21 | ScrollView([.horizontal, .vertical]) { |
| 22 | Grid(alignment: .leading, horizontalSpacing: 14, verticalSpacing: 6) { |
| 23 | GridRow { |
| 24 | ForEach(view.specs.indices, id: \.self) { i in |
| 25 | Text(view.specs[i].title).bold().foregroundStyle(.secondary) |
| 26 | } |
| 27 | } |
| 28 | Divider() |
| 29 | ForEach(view.rows.indices, id: \.self) { r in |
| 30 | GridRow { |
| 31 | ForEach(view.specs.indices, id: \.self) { c in cellView(r, c) } |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | .font(.system(.callout, design: .monospaced)) |
| 36 | .padding(12) |
| 37 | } |
| 38 | .defaultScrollAnchor(.topLeading) |
| 39 | .navigationTitle("Column View") |
| 40 | .navigationBarTitleDisplayMode(.inline) |
| 41 | .toolbar { |
| 42 | ToolbarItem(placement: .confirmationAction) { Button("Done") { dismiss() } } |
| 43 | ToolbarItemGroup(placement: .bottomBar) { |
| 44 | Button("Edit", systemImage: "pencil") { edit() }.disabled(selected == nil) |
| 45 | Button("Previous Value", systemImage: "chevron.up") { cycle(previous: true) }.disabled(selected == nil) |
| 46 | Button("Next Value", systemImage: "chevron.down") { cycle(previous: false) }.disabled(selected == nil) |
| 47 | Spacer() |
| 48 | Button("Go to Entry", systemImage: "arrow.right.doc.on.clipboard") { |
| 49 | guard let selected else { return } |
| 50 | jump(view.rows[selected.row].start) |
| 51 | dismiss() |
| 52 | } |
| 53 | .disabled(selected == nil) |
| 54 | } |
| 55 | } |
| 56 | .safeAreaInset(edge: .top) { |
| 57 | if let message { |
| 58 | Text(message).font(.footnote).padding(.horizontal, 12).padding(.vertical, 6) |
| 59 | .glassEffect(.regular, in: Capsule()) |
| 60 | .onTapGesture { self.message = nil } |
| 61 | } |
| 62 | } |
| 63 | .sheet(item: $prompt) { request in |
| 64 | PromptSheet(prompt: request.prompt) { answer in |
| 65 | prompt = nil |
| 66 | request.reply(answer) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | private func cellView(_ r: Int, _ c: Int) -> some View { |
| 73 | let row = view.rows[r] |
| 74 | let isSelected = selected.map { $0 == (r, c) } ?? false |
| 75 | let text = view.specs[c].property == "ITEM" ? String(repeating: "*", count: row.level) + " " + row.values[c] : row.values[c] |
| 76 | return Text(text.isEmpty ? " " : text) |
| 77 | .lineLimit(1) |
| 78 | .frame(maxWidth: view.specs[c].width.map { CGFloat($0) * 8.5 }, alignment: .leading) |
| 79 | .padding(.horizontal, 3) |
| 80 | .padding(.vertical, 2) |
| 81 | .background(isSelected ? Color.accentColor.opacity(0.25) : .clear, in: RoundedRectangle(cornerRadius: 4)) |
| 82 | .contentShape(Rectangle()) |
| 83 | .onTapGesture { selected = (r, c) } |
| 84 | .accessibilityAddTraits(isSelected ? [.isButton, .isSelected] : .isButton) |
| 85 | .accessibilityAction(named: "Edit") { |
| 86 | selected = (r, c) |
| 87 | edit() |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// `org-columns-edit-value`: asks for the value, offering the allowed ones. |
| 92 | private func edit() { |
| 93 | guard let (r, c) = selected else { return } |
| 94 | let spec = view.specs[c] |
| 95 | let row = view.rows[r] |
| 96 | if ["CLOCKSUM", "CLOCKSUM_T", "ALLTAGS", "BLOCKED", "CLOSED", "FILE", "TIMESTAMP", "TIMESTAMP_IA"].contains(spec.property) { |
| 97 | message = "This special column cannot be edited" |
| 98 | return |
| 99 | } |
| 100 | let allowed = session.document.flatMap { PropertyValues.allowed(spec.property, at: row.start, text: $0.text, tree: $0.tree)?.values } ?? [] |
| 101 | let initial = row.raw.indices.contains(c) ? row.raw[c] : row.values[c] |
| 102 | // With allowed values the field starts empty, so that they all show. |
| 103 | let message = allowed.isEmpty || initial.isEmpty ? spec.title : "\(spec.title) (now \(initial))" |
| 104 | prompt = EditorScreen.PromptRequest(prompt: Prompt(key: "value", message: message, initial: allowed.isEmpty ? initial : "", choices: allowed)) { answer in |
| 105 | guard let answer else { return } |
| 106 | perform(SetColumnValue(property: spec.property, value: answer), at: row.start) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private func cycle(previous: Bool) { |
| 111 | guard let (r, c) = selected else { return } |
| 112 | let spec = view.specs[c] |
| 113 | perform(NextColumnValue(property: spec.property, summary: spec.summary, previous: previous), at: view.rows[r].start) |
| 114 | } |
| 115 | |
| 116 | private func perform(_ command: any OrgCommand, at start: Int, answers: [String: String] = [:]) { |
| 117 | switch session.perform(command, path: path, at: start, expect: nil, answers: answers) { |
| 118 | case .commit: |
| 119 | message = nil |
| 120 | guard let document = session.document, let new = try? ColumnView.refresh(view, text: document.text, tree: document.tree) else { return } |
| 121 | view = new |
| 122 | if let selected, selected.row >= new.rows.count { self.selected = nil } |
| 123 | case .failed(let text): |
| 124 | message = text |
| 125 | case .prompt(let request): |
| 126 | // A state note, or what else the change asks. |
| 127 | prompt = EditorScreen.PromptRequest(prompt: request) { answer in |
| 128 | guard let answer else { return } |
| 129 | perform(command, at: start, answers: answers.merging([request.key: answer]) { $1 }) |
| 130 | } |
| 131 | case .external(let request): |
| 132 | message = MobileEditor.unavailable(request, title: command.title) |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | #endif |