← All repositories

reportage

A life-logging app for iPhone

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


commit db9798134739fa2df689d11da4e5e5f2dcea4fac
parent 3f09e1eda93fbc93930527aae9f142ac9624024b
Author: Michael Camilleri <[email protected]>
Date:   Sat, 17 Oct 2026 04:58:53 +0900

Support swiping between survey questions

This commit lays the questions side by side as pages that follow the
user's finger, so a swipe moves between them and a partial drag can be
let go to settle back. Back, Next and the automatic advance after a
yes/no or choice answer turn the same pages, and the header follows
whichever page is showing. Swiping forward on the last question goes no
further, so a report is still only saved with 'Done'.

Neighbouring pages are built before they are swiped to, so fields now
take focus from whether their page is the one showing rather than from
appearing. The number and note fields focus as their page arrives, and
leaving a page with an open search closes the keyboard.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Diffstat

MReportage/Views/Survey/PlaceAnswerView.swift | 3+++
MReportage/Views/Survey/QuestionPage.swift | 28+++++++++++++++++++---------
MReportage/Views/Survey/SurveyModel.swift | 10++++++----
MReportage/Views/Survey/SurveyView.swift | 50+++++++++++++++++++++++++++++++++++++++++++-------
MReportage/Views/Survey/TokenAnswer.swift | 3+++
MTests/Unit/SurveyModelTests.swift | 7+++++--

6 files changed, 79 insertions(+), 22 deletions(-)

diff --git a/Reportage/Views/Survey/PlaceAnswerView.swift b/Reportage/Views/Survey/PlaceAnswerView.swift

@@ -6,6 +6,8 @@ import SwiftUI
 struct PlaceAnswerView: View {
     let picker: PlacePicker
     let placeholder: String?
+    /// Whether the page is on screen. Leaving it closes the keyboard.
+    let isActive: Bool
 
     @FocusState private var isSearchFocused: Bool
 
@@ -41,6 +43,7 @@ struct PlaceAnswerView: View {
         }
         .animation(.snappy, value: isSearchFocused)
         .animation(.snappy, value: showsMap)
+        .onChange(of: isActive) { if !isActive { isSearchFocused = false } }
     }
 
     /// The map is left out only when there's nothing to put on it.

diff --git a/Reportage/Views/Survey/QuestionPage.swift b/Reportage/Views/Survey/QuestionPage.swift

@@ -4,6 +4,10 @@ import SwiftUI
 struct QuestionPage: View {
     let question: Question
     let model: SurveyModel
+    /// Whether this is the page on screen. Neighbouring pages are built
+    /// before they're swiped to, so fields take focus from this rather than
+    /// from appearing.
+    let isCurrent: Bool
     /// Called when a single tap has answered the question.
     let onAnswer: () -> Void
 
@@ -26,16 +30,20 @@ struct QuestionPage: View {
                 )
             case .tokens, .people:
                 if let picker = model.pickers[question.id] {
-                    TokenAnswer(picker: picker, placeholder: question.placeholder)
+                    TokenAnswer(picker: picker, placeholder: question.placeholder, isActive: isCurrent)
                 }
             case .location:
                 if let picker = model.placePickers[question.id] {
-                    PlaceAnswerView(picker: picker, placeholder: question.placeholder)
+                    PlaceAnswerView(picker: picker, placeholder: question.placeholder, isActive: isCurrent)
                 }
             case .number:
-                NumberAnswer(value: $model[numberFor: question.id], placeholder: question.placeholder)
+                NumberAnswer(
+                    value: $model[numberFor: question.id],
+                    placeholder: question.placeholder,
+                    isActive: isCurrent
+                )
             case .note:
-                NoteAnswer(text: $model[textFor: question.id])
+                NoteAnswer(text: $model[textFor: question.id], isActive: isCurrent)
             }
         }
         .padding(.top, 28)
@@ -139,6 +147,8 @@ struct UnderlinedField<Content: View>: View {
 struct NumberAnswer: View {
     @Binding var value: Decimal?
     let placeholder: String?
+    /// The field is focused while its page is on screen.
+    let isActive: Bool
 
     @State private var text = ""
     @FocusState private var isFocused: Bool
@@ -150,10 +160,8 @@ struct NumberAnswer: View {
                 .font(Theme.serif(.largeTitle).monospacedDigit())
                 .focused($isFocused)
         }
-        .onAppear {
-            text = value?.formatted() ?? ""
-            isFocused = true
-        }
+        .onAppear { text = value?.formatted() ?? "" }
+        .onChange(of: isActive, initial: true) { isFocused = isActive }
         .onChange(of: text) {
             let trimmed = text.trimmingCharacters(in: .whitespaces)
             value = trimmed.isEmpty ? nil : try? Decimal(trimmed, format: .number)
@@ -163,6 +171,8 @@ struct NumberAnswer: View {
 
 struct NoteAnswer: View {
     @Binding var text: String
+    /// The editor is focused while its page is on screen.
+    let isActive: Bool
 
     @FocusState private var isFocused: Bool
 
@@ -173,6 +183,6 @@ struct NoteAnswer: View {
             .focused($isFocused)
             .scrollContentBackground(.hidden)
             .padding(.horizontal, 19)
-            .onAppear { isFocused = true }
+            .onChange(of: isActive, initial: true) { isFocused = isActive }
     }
 }

diff --git a/Reportage/Views/Survey/SurveyModel.swift b/Reportage/Views/Survey/SurveyModel.swift

@@ -18,8 +18,6 @@ final class SurveyModel {
     let placePickers: [String: PlacePicker]
 
     private(set) var index = 0
-    /// Whether the last page change went forwards, for the page transition.
-    private(set) var isMovingForward = true
     private var answers: [String: ResponseValue] = [:]
     /// Live context, captured from when the survey opens.
     private var contextTask: Task<Context, Never>?
@@ -125,7 +123,6 @@ final class SurveyModel {
     @discardableResult
     func advance() -> Bool {
         guard !isLastQuestion else { return false }
-        isMovingForward = true
         index += 1
         return true
     }
@@ -139,10 +136,15 @@ final class SurveyModel {
 
     func goBack() {
         guard index > 0 else { return }
-        isMovingForward = false
         index -= 1
     }
 
+    /// Shows the question at `index`, as when the user swipes to it.
+    func show(_ index: Int) {
+        guard questions.indices.contains(index) else { return }
+        self.index = index
+    }
+
     /// Clears the current question's answer, so it's recorded as skipped.
     func clearCurrent() {
         guard let question = currentQuestion else { return }

diff --git a/Reportage/Views/Survey/SurveyView.swift b/Reportage/Views/Survey/SurveyView.swift

@@ -68,17 +68,14 @@ struct SurveyView: View {
     @State private var isShowingSaveError = false
 
     var body: some View {
-        ZStack(alignment: .top) {
-            if let question = model.currentQuestion {
-                QuestionPage(question: question, model: model, onAnswer: answered)
-                    .id(question.id)
-                    .transition(.push(from: model.isMovingForward ? .trailing : .leading))
-            } else {
+        Group {
+            if model.questions.isEmpty {
                 NoQuestions()
+            } else {
+                QuestionPages(model: model, onAnswer: answered)
             }
         }
         .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
-        .clipped()
         .safeAreaInset(edge: .top) {
             SurveyHeader(
                 kind: model.kind,
@@ -159,6 +156,45 @@ struct SurveyView: View {
     }
 }
 
+/// The survey's questions side by side, one per page, swiped between or
+/// turned with Back and Next. Swiping forward on the last question goes no
+/// further: the report is only saved with Done.
+struct QuestionPages: View {
+    let model: SurveyModel
+    let onAnswer: () -> Void
+
+    var body: some View {
+        ScrollView(.horizontal) {
+            LazyHStack(spacing: 0) {
+                ForEach(Array(model.questions.enumerated()), id: \.element.id) { index, question in
+                    QuestionPage(
+                        question: question,
+                        model: model,
+                        isCurrent: index == model.index,
+                        onAnswer: onAnswer
+                    )
+                    .containerRelativeFrame([.horizontal, .vertical])
+                }
+            }
+            .scrollTargetLayout()
+        }
+        .scrollTargetBehavior(.paging)
+        .scrollPosition(id: currentQuestionID)
+        .scrollIndicators(.hidden)
+    }
+
+    /// The current page, which Back and Next change and a swipe sets.
+    private var currentQuestionID: Binding<String?> {
+        Binding(
+            get: { model.currentQuestion?.id },
+            set: { id in
+                guard let index = model.questions.firstIndex(where: { $0.id == id }) else { return }
+                model.show(index)
+            }
+        )
+    }
+}
+
 /// Cancel, the report's kind and time, the question count, and a bar with
 /// one segment per question.
 struct SurveyHeader: View {

diff --git a/Reportage/Views/Survey/TokenAnswer.swift b/Reportage/Views/Survey/TokenAnswer.swift

@@ -5,6 +5,8 @@ import SwiftUI
 struct TokenAnswer: View {
     let picker: TokenPicker
     let placeholder: String?
+    /// Whether the page is on screen. Leaving it closes the keyboard.
+    let isActive: Bool
 
     @FocusState private var isSearchFocused: Bool
 
@@ -22,6 +24,7 @@ struct TokenAnswer: View {
             )
             TokenList(picker: picker)
         }
+        .onChange(of: isActive) { if !isActive { isSearchFocused = false } }
     }
 }
 

diff --git a/Tests/Unit/SurveyModelTests.swift b/Tests/Unit/SurveyModelTests.swift

@@ -99,11 +99,14 @@ struct SurveyModelTests {
         model.advance(from: 0)
         model.advance(from: 0)
         #expect(model.index == 1)
-        #expect(model.isMovingForward)
 
         model.goBack()
         #expect(model.index == 0)
-        #expect(!model.isMovingForward)
+
+        model.show(3)
+        #expect(model.index == 3)
+        model.show(Self.questions.count)
+        #expect(model.index == 3)
 
         for _ in Self.questions { model.advance() }
         #expect(model.index == Self.questions.count - 1)