← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


Reportage/Views/Survey/TokenAnswer.swift (6208B)

1import SwiftUI
2
3/// Answers a `tokens` or `people` question by picking from past answers, most
4/// used first, or adding a new one.
5struct TokenAnswer: View {
6    let picker: TokenPicker
7    let placeholder: String?
8    /// Whether the page is on screen. Leaving it closes the keyboard.
9    let isActive: Bool
10
11    @FocusState private var isSearchFocused: Bool
12
13    var body: some View {
14        @Bindable var picker = picker
15        VStack(spacing: Theme.blockGap) {
16            if !picker.selectedEntries.isEmpty {
17                SelectedTokens(entries: picker.selectedEntries, onRemove: picker.toggle)
18            }
19            TokenSearchField(
20                query: $picker.query,
21                placeholder: placeholder,
22                isFocused: $isSearchFocused,
23                onSubmit: picker.addQuery
24            )
25            TokenList(picker: picker)
26        }
27        .onChange(of: isActive) { if !isActive { isSearchFocused = false } }
28    }
29}
30
31/// The chosen tokens, in the order they were picked. Tapping one removes it.
32struct SelectedTokens: View {
33    let entries: [VocabularyEntry]
34    let onRemove: (String) -> Void
35
36    var body: some View {
37        ScrollView(.horizontal) {
38            HStack(spacing: Theme.chipGap) {
39                ForEach(entries) { entry in
40                    Button {
41                        onRemove(entry.id)
42                    } label: {
43                        HStack(spacing: Theme.chipGap) {
44                            Text(entry.text)
45                            Image(systemName: "xmark")
46                                .font(.caption.weight(.semibold))
47                        }
48                        .font(.subheadline)
49                        .foregroundStyle(Theme.paper)
50                        .padding(.horizontal, 16)
51                        .frame(minHeight: 44)
52                        .background(Theme.ink, in: .capsule)
53                    }
54                    .buttonStyle(.plain)
55                    .accessibilityHint(Text("Removes this answer"))
56                }
57            }
58            .padding(.horizontal, Theme.margin)
59        }
60        .scrollIndicators(.hidden)
61    }
62}
63
64struct TokenSearchField: View {
65    @Binding var query: String
66    let placeholder: String?
67    let isFocused: FocusState<Bool>.Binding
68    let onSubmit: () -> Void
69
70    var body: some View {
71        UnderlinedField {
72            HStack(spacing: Theme.iconGap) {
73                Image(systemName: "magnifyingglass")
74                    .foregroundStyle(Theme.muted)
75                TextField(placeholder ?? String(localized: "Search or add"), text: $query)
76                    .font(Theme.serif(.title3))
77                    .autocorrectionDisabled()
78                    .submitLabel(.done)
79                    .focused(isFocused)
80                    .onSubmit(onSubmit)
81                if !query.isEmpty {
82                    Button("Clear", systemImage: "xmark.circle.fill") { query = "" }
83                        .labelStyle(.iconOnly)
84                        .foregroundStyle(Theme.muted)
85                }
86            }
87        }
88    }
89}
90
91struct TokenList: View {
92    let picker: TokenPicker
93
94    var body: some View {
95        List {
96            if picker.canAddQuery {
97                AddTokenRow(text: picker.query.trimmingCharacters(in: .whitespacesAndNewlines)) {
98                    picker.addQuery()
99                }
100                .listRowStyle()
101            }
102            ForEach(picker.visibleEntries) { entry in
103                TokenRow(
104                    text: entry.text,
105                    useCount: entry.useCount,
106                    isSelected: picker.isSelected(entry.id),
107                    onToggle: { picker.toggle(entry.id) }
108                )
109                .listRowStyle()
110            }
111        }
112        .listStyle(.plain)
113        .scrollContentBackground(.hidden)
114        .scrollDismissesKeyboard(.immediately)
115    }
116}
117
118extension View {
119    func listRowStyle() -> some View {
120        listRowInsets(EdgeInsets(top: 0, leading: Theme.margin, bottom: 0, trailing: Theme.margin))
121            .listRowBackground(Theme.paper)
122            .listRowSeparatorTint(Theme.rule)
123    }
124}
125
126struct AddTokenRow: View {
127    let text: String
128    /// A line beneath, saying what adding it will do.
129    var detail: Text?
130    let onAdd: () -> Void
131
132    var body: some View {
133        Button(action: onAdd) {
134            HStack(spacing: Theme.iconGap) {
135                Image(systemName: "plus")
136                    .font(.body.weight(.semibold))
137                VStack(alignment: .leading, spacing: Theme.lineGap) {
138                    Text("Add “\(text)”", comment: "Adds the typed text as a new answer")
139                        .font(Theme.serif(.title3))
140                    if let detail {
141                        detail
142                            .font(.footnote)
143                            .foregroundStyle(Theme.muted)
144                    }
145                }
146                Spacer()
147            }
148            .foregroundStyle(Theme.accent)
149            .frame(minHeight: 52)
150            .contentShape(.rect)
151        }
152        .buttonStyle(.plain)
153    }
154}
155
156struct TokenRow: View {
157    let text: String
158    let useCount: Int
159    let isSelected: Bool
160    let onToggle: () -> Void
161
162    var body: some View {
163        Button(action: onToggle) {
164            HStack {
165                Text(text)
166                    .font(Theme.serif(.title3))
167                    .foregroundStyle(Theme.ink)
168                Spacer()
169                if isSelected {
170                    Image(systemName: "checkmark")
171                        .font(.body.weight(.semibold))
172                        .foregroundStyle(Theme.accent)
173                } else if useCount > 0 {
174                    Text(useCount, format: .number)
175                        .font(.caption)
176                        .monospacedDigit()
177                        .foregroundStyle(Theme.muted)
178                        .accessibilityLabel(Text("Used \(useCount) times"))
179                }
180            }
181            .frame(minHeight: 52)
182            .contentShape(.rect)
183        }
184        .buttonStyle(.plain)
185        .accessibilityAddTraits(isSelected ? .isSelected : [])
186    }
187}