Sources/OrgstarMobile/CaptureScreen.swift

3f183dc5a69542840ae20a73ec755d1148b354dd
orgstar/Sources/OrgstarMobile/CaptureScreen.swift history · blame · raw

93 lines · 3587 bytes

 1#if os(iOS)
 2import OrgApp
 3import OrgCore
 4import SwiftUI
 5
 6/// Capture with a template: its text to edit, filed where the template says.
 7struct CaptureScreen: View {
 8    let workspace: WorkspaceModel
 9    let session: DocumentSession
10    let request: CaptureRequest
11    @Environment(\.dismiss) private var dismiss
12    @State private var templates: [CaptureTemplate] = []
13    @State private var key: String?
14    @State private var text = ""
15    @State private var selection: TextSelection?
16    @State private var problem: String?
17    @FocusState private var focused: Bool
18
19    private var template: CaptureTemplate? { templates.first { $0.key == key } }
20
21    var body: some View {
22        NavigationStack {
23            Form {
24                Picker("Template", selection: $key) {
25                    ForEach(templates) { Text($0.name).tag(Optional($0.key)) }
26                }
27                Section {
28                    TextEditor(text: $text, selection: $selection)
29                        .font(.body.monospaced())
30                        .frame(minHeight: 200)
31                        .focused($focused)
32                } footer: {
33                    if let problem { Text(problem).foregroundStyle(.red) }
34                }
35            }
36            .navigationTitle("Capture")
37            .navigationBarTitleDisplayMode(.inline)
38            .toolbar {
39                ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() } }
40                ToolbarItem(placement: .confirmationAction) { Button("File", action: file).disabled(template == nil) }
41            }
42        }
43        .onAppear {
44            let loaded = CaptureTemplates.load()
45            templates = loaded.templates
46            problem = loaded.problems.first
47            key = request.template.flatMap { wanted in templates.first { $0.key == wanted }?.key } ?? templates.first?.key
48            focused = true
49        }
50        .onChange(of: key, initial: true) { fill() }
51    }
52
53    /// The template expanded, its prompts taking their defaults.
54    private func fill() {
55        guard let template else { return }
56        var context = CaptureContext(
57            initial: request.properties["initial"] ?? "", annotation: request.properties["annotation"] ?? "",
58            allTags: (try? workspace.index.tags()) ?? []
59        )
60        context.linkProperties = request.properties
61        let answers = Capture.questions(template.template, context: context).map { _ in "" }
62        switch Capture.fill(template.template, context: context, answers: answers) {
63        case .success(let filled):
64            text = filled.text
65            // Where the template's `%?` was.
66            selection = TextSelection(insertionPoint: String.Index(utf16Offset: filled.cursor, in: filled.text))
67        case .failure(.message(let message)): problem = message
68        }
69    }
70
71    private func file() {
72        guard let template else { return }
73        var file: String?
74        switch template.target {
75        case .id(let id):
76            guard let location = try? workspace.index.headings(withID: id).first else {
77                problem = "Cannot find target ID \"\(id)\""
78                return
79            }
80            file = location.path
81        case .clock:
82            problem = "There's no running clock on iOS"
83            return
84        default:
85            break
86        }
87        switch session.capture(text, template: template, root: workspace.rootPaths.first, file: file) {
88        case .failure(let error): problem = error.message
89        case .success: dismiss()
90        }
91    }
92}
93#endif