← All repositories

reportage

A life-logging app for iPhone

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


Reportage/Views/Survey/QuestionPage.swift (6301B)

1import SwiftUI
2
3/// One page of a survey: the prompt, and the answer control for its kind.
4struct QuestionPage: View {
5    let question: Question
6    let model: SurveyModel
7    /// Whether this is the page on screen. Neighbouring pages are built
8    /// before they're swiped to, so fields take focus from this rather than
9    /// from appearing.
10    let isCurrent: Bool
11    /// Called when a single tap has answered the question.
12    let onAnswer: () -> Void
13
14    var body: some View {
15        @Bindable var model = model
16        VStack(alignment: .leading, spacing: Theme.promptGap) {
17            Text(question.prompt)
18                .font(Theme.serif(.largeTitle, weight: .medium))
19                .frame(maxWidth: .infinity, alignment: .leading)
20                .padding(.horizontal, Theme.margin)
21                .accessibilityAddTraits(.isHeader)
22            switch question.kind {
23            case .yesNo:
24                YesNoAnswer(selection: $model[optionFor: question.id], onAnswer: onAnswer)
25            case .choice:
26                ChoiceAnswer(
27                    options: question.options,
28                    selection: $model[optionFor: question.id],
29                    onAnswer: onAnswer
30                )
31            case .tokens, .people:
32                if let picker = model.pickers[question.id] {
33                    TokenAnswer(picker: picker, placeholder: question.placeholder, isActive: isCurrent)
34                }
35            case .location:
36                if let picker = model.placePickers[question.id] {
37                    PlaceAnswerView(picker: picker, placeholder: question.placeholder, isActive: isCurrent)
38                }
39            case .number:
40                NumberAnswer(
41                    value: $model[numberFor: question.id],
42                    placeholder: question.placeholder,
43                    isActive: isCurrent
44                )
45            case .note:
46                NoteAnswer(text: $model[textFor: question.id], isActive: isCurrent)
47            }
48        }
49        .padding(.top, Theme.pageTop)
50        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
51    }
52}
53
54/// A full-width answer, outlined in ink, and filled with ink when selected.
55struct AnswerOptionButton: View {
56    let title: Text
57    let isSelected: Bool
58    let action: () -> Void
59
60    var body: some View {
61        Button(action: action) {
62            HStack {
63                title
64                Spacer()
65                if isSelected {
66                    Image(systemName: "checkmark")
67                        .font(.body.weight(.semibold))
68                }
69            }
70            .font(Theme.serif(.title3))
71            .foregroundStyle(isSelected ? Theme.paper : Theme.ink)
72            .padding(.horizontal, 20)
73            .frame(maxWidth: .infinity, minHeight: 56)
74            .background(isSelected ? Theme.ink : .clear, in: .capsule)
75            .overlay(Capsule().strokeBorder(Theme.ink, lineWidth: 1.5))
76            .contentShape(.capsule)
77        }
78        .buttonStyle(.plain)
79        .accessibilityAddTraits(isSelected ? .isSelected : [])
80    }
81}
82
83struct YesNoAnswer: View {
84    @Binding var selection: String?
85    let onAnswer: () -> Void
86
87    var body: some View {
88        VStack(spacing: Theme.itemGap) {
89            AnswerOptionButton(
90                title: Text("Yes", comment: "Answer to a yes/no question"),
91                isSelected: selection == ResponseValue.yesOption
92            ) {
93                select(ResponseValue.yesOption)
94            }
95            AnswerOptionButton(
96                title: Text("No", comment: "Answer to a yes/no question"),
97                isSelected: selection == ResponseValue.noOption
98            ) {
99                select(ResponseValue.noOption)
100            }
101        }
102        .padding(.horizontal, Theme.margin)
103    }
104
105    private func select(_ option: String) {
106        selection = option
107        onAnswer()
108    }
109}
110
111struct ChoiceAnswer: View {
112    let options: [String]
113    @Binding var selection: String?
114    let onAnswer: () -> Void
115
116    var body: some View {
117        ScrollView {
118            VStack(spacing: Theme.itemGap) {
119                ForEach(options, id: \.self) { option in
120                    AnswerOptionButton(title: Text(option), isSelected: selection == option) {
121                        selection = option
122                        onAnswer()
123                    }
124                }
125            }
126            .padding(.horizontal, Theme.margin)
127        }
128    }
129}
130
131/// A text field drawn as a line to write on.
132struct UnderlinedField<Content: View>: View {
133    @ViewBuilder let content: Content
134
135    var body: some View {
136        content
137            .padding(.bottom, 10)
138            .overlay(alignment: .bottom) {
139                Rectangle()
140                    .fill(Theme.ink)
141                    .frame(height: 1.5)
142            }
143            .padding(.horizontal, Theme.margin)
144    }
145}
146
147struct NumberAnswer: View {
148    @Binding var value: Decimal?
149    let placeholder: String?
150    /// The field is focused while its page is on screen.
151    let isActive: Bool
152
153    @State private var text = ""
154    @FocusState private var isFocused: Bool
155
156    var body: some View {
157        UnderlinedField {
158            TextField(placeholder ?? String(localized: "Number"), text: $text)
159                .keyboardType(.decimalPad)
160                .font(Theme.serif(.largeTitle).monospacedDigit())
161                .focused($isFocused)
162        }
163        .onAppear { text = value?.formatted() ?? "" }
164        .onChange(of: isActive, initial: true) { isFocused = isActive }
165        .onChange(of: text) {
166            let trimmed = text.trimmingCharacters(in: .whitespaces)
167            value = trimmed.isEmpty ? nil : try? Decimal(trimmed, format: .number)
168        }
169    }
170}
171
172struct NoteAnswer: View {
173    @Binding var text: String
174    /// The editor is focused while its page is on screen.
175    let isActive: Bool
176
177    @FocusState private var isFocused: Bool
178
179    var body: some View {
180        TextEditor(text: $text)
181            .font(Theme.serif(.title3))
182            .lineSpacing(4)
183            .focused($isFocused)
184            .scrollContentBackground(.hidden)
185            .padding(.horizontal, 19)
186            .onChange(of: isActive, initial: true) { isFocused = isActive }
187    }
188}