Sources/OrgstarMobile/ColumnViewScreen.swift
118 lines · 5659 bytes
1#if os(iOS)
2import OrgApp
3import OrgCore
4import 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.
9struct 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 ColumnViewGrid(view: view, verticalSpacing: 6) { r, c in cellView(r, c) }
23 .font(.system(.callout, design: .monospaced))
24 .padding(12)
25 }
26 .defaultScrollAnchor(.topLeading)
27 .navigationTitle("Column View")
28 .navigationBarTitleDisplayMode(.inline)
29 .toolbar {
30 ToolbarItem(placement: .confirmationAction) { Button("Done") { dismiss() } }
31 ToolbarItemGroup(placement: .bottomBar) {
32 Button("Edit", systemImage: "pencil") { edit() }.disabled(selected == nil)
33 Button("Previous Value", systemImage: "chevron.up") { cycle(previous: true) }.disabled(selected == nil)
34 Button("Next Value", systemImage: "chevron.down") { cycle(previous: false) }.disabled(selected == nil)
35 Spacer()
36 Button("Go to Entry", systemImage: "arrow.right.doc.on.clipboard") {
37 guard let selected else { return }
38 jump(view.rows[selected.row].start)
39 dismiss()
40 }
41 .disabled(selected == nil)
42 }
43 }
44 .message($message, edge: .top)
45 .sheet(item: $prompt) { request in
46 PromptSheet(prompt: request.prompt) { answer in
47 prompt = nil
48 request.reply(answer)
49 }
50 }
51 }
52 }
53
54 private func cellView(_ r: Int, _ c: Int) -> some View {
55 let row = view.rows[r]
56 let isSelected = selected.map { $0 == (r, c) } ?? false
57 let text = view.specs[c].property == "ITEM" ? String(repeating: "*", count: row.level) + " " + row.values[c] : row.values[c]
58 return Text(text.isEmpty ? " " : text)
59 .lineLimit(1)
60 .frame(maxWidth: view.specs[c].width.map { CGFloat($0) * 8.5 }, alignment: .leading)
61 .padding(.horizontal, 3)
62 .padding(.vertical, 2)
63 .background(isSelected ? Color.accentColor.opacity(0.25) : .clear, in: RoundedRectangle(cornerRadius: 4))
64 .contentShape(Rectangle())
65 .onTapGesture { selected = (r, c) }
66 .accessibilityAddTraits(isSelected ? [.isButton, .isSelected] : .isButton)
67 .accessibilityAction(named: "Edit") {
68 selected = (r, c)
69 edit()
70 }
71 }
72
73 /// `org-columns-edit-value`: asks for the value, offering the allowed ones.
74 private func edit() {
75 guard let (r, c) = selected else { return }
76 let spec = view.specs[c]
77 let row = view.rows[r]
78 if ["CLOCKSUM", "CLOCKSUM_T", "ALLTAGS", "BLOCKED", "CLOSED", "FILE", "TIMESTAMP", "TIMESTAMP_IA"].contains(spec.property) {
79 message = "This special column cannot be edited"
80 return
81 }
82 let allowed = session.document.flatMap { PropertyValues.allowed(spec.property, at: row.start, text: $0.text, tree: $0.tree)?.values } ?? []
83 let initial = row.raw.indices.contains(c) ? row.raw[c] : row.values[c]
84 // With allowed values the field starts empty, so that they all show.
85 let message = allowed.isEmpty || initial.isEmpty ? spec.title : "\(spec.title) (now \(initial))"
86 prompt = EditorScreen.PromptRequest(prompt: Prompt(key: "value", message: message, initial: allowed.isEmpty ? initial : "", choices: allowed)) { answer in
87 guard let answer else { return }
88 perform(SetColumnValue(property: spec.property, value: answer), at: row.start)
89 }
90 }
91
92 private func cycle(previous: Bool) {
93 guard let (r, c) = selected else { return }
94 let spec = view.specs[c]
95 perform(NextColumnValue(property: spec.property, summary: spec.summary, previous: previous), at: view.rows[r].start)
96 }
97
98 private func perform(_ command: any OrgCommand, at start: Int, answers: [String: String] = [:]) {
99 switch session.perform(command, path: path, at: start, expect: nil, answers: answers) {
100 case .commit:
101 message = nil
102 guard let document = session.document, let new = try? ColumnView.refresh(view, text: document.text, tree: document.tree) else { return }
103 view = new
104 if let selected, selected.row >= new.rows.count { self.selected = nil }
105 case .failed(let text):
106 message = text
107 case .prompt(let request):
108 // A state note, or what else the change asks.
109 prompt = EditorScreen.PromptRequest(prompt: request) { answer in
110 guard let answer else { return }
111 perform(command, at: start, answers: answers.merging([request.key: answer]) { $1 })
112 }
113 case .external(let request):
114 message = MobileEditor.unavailable(request, title: command.title)
115 }
116 }
117}
118#endif