Sources/OrgstarMobile/ColumnViewScreen.swift
124 lines · 5942 bytes
5 symbols in this file
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 .safeAreaInset(edge: .top) {
45 if let message {
46 Text(message).font(.footnote).padding(.horizontal, 12).padding(.vertical, 6)
47 .glassEffect(.regular, in: Capsule())
48 .onTapGesture { self.message = nil }
49 }
50 }
51 .sheet(item: $prompt) { request in
52 PromptSheet(prompt: request.prompt) { answer in
53 prompt = nil
54 request.reply(answer)
55 }
56 }
57 }
58 }
59
60 private func cellView(_ r: Int, _ c: Int) -> some View {
61 let row = view.rows[r]
62 let isSelected = selected.map { $0 == (r, c) } ?? false
63 let text = view.specs[c].property == "ITEM" ? String(repeating: "*", count: row.level) + " " + row.values[c] : row.values[c]
64 return Text(text.isEmpty ? " " : text)
65 .lineLimit(1)
66 .frame(maxWidth: view.specs[c].width.map { CGFloat($0) * 8.5 }, alignment: .leading)
67 .padding(.horizontal, 3)
68 .padding(.vertical, 2)
69 .background(isSelected ? Color.accentColor.opacity(0.25) : .clear, in: RoundedRectangle(cornerRadius: 4))
70 .contentShape(Rectangle())
71 .onTapGesture { selected = (r, c) }
72 .accessibilityAddTraits(isSelected ? [.isButton, .isSelected] : .isButton)
73 .accessibilityAction(named: "Edit") {
74 selected = (r, c)
75 edit()
76 }
77 }
78
79 /// `org-columns-edit-value`: asks for the value, offering the allowed ones.
80 private func edit() {
81 guard let (r, c) = selected else { return }
82 let spec = view.specs[c]
83 let row = view.rows[r]
84 if ["CLOCKSUM", "CLOCKSUM_T", "ALLTAGS", "BLOCKED", "CLOSED", "FILE", "TIMESTAMP", "TIMESTAMP_IA"].contains(spec.property) {
85 message = "This special column cannot be edited"
86 return
87 }
88 let allowed = session.document.flatMap { PropertyValues.allowed(spec.property, at: row.start, text: $0.text, tree: $0.tree)?.values } ?? []
89 let initial = row.raw.indices.contains(c) ? row.raw[c] : row.values[c]
90 // With allowed values the field starts empty, so that they all show.
91 let message = allowed.isEmpty || initial.isEmpty ? spec.title : "\(spec.title) (now \(initial))"
92 prompt = EditorScreen.PromptRequest(prompt: Prompt(key: "value", message: message, initial: allowed.isEmpty ? initial : "", choices: allowed)) { answer in
93 guard let answer else { return }
94 perform(SetColumnValue(property: spec.property, value: answer), at: row.start)
95 }
96 }
97
98 private func cycle(previous: Bool) {
99 guard let (r, c) = selected else { return }
100 let spec = view.specs[c]
101 perform(NextColumnValue(property: spec.property, summary: spec.summary, previous: previous), at: view.rows[r].start)
102 }
103
104 private func perform(_ command: any OrgCommand, at start: Int, answers: [String: String] = [:]) {
105 switch session.perform(command, path: path, at: start, expect: nil, answers: answers) {
106 case .commit:
107 message = nil
108 guard let document = session.document, let new = try? ColumnView.refresh(view, text: document.text, tree: document.tree) else { return }
109 view = new
110 if let selected, selected.row >= new.rows.count { self.selected = nil }
111 case .failed(let text):
112 message = text
113 case .prompt(let request):
114 // A state note, or what else the change asks.
115 prompt = EditorScreen.PromptRequest(prompt: request) { answer in
116 guard let answer else { return }
117 perform(command, at: start, answers: answers.merging([request.key: answer]) { $1 })
118 }
119 case .external(let request):
120 message = MobileEditor.unavailable(request, title: command.title)
121 }
122 }
123}
124#endif