← All repositories

reportage

A life-logging app for iPhone

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


commit 3f0e9abafa1328b45d529cf6c969986fc3cfb596
parent cd9748d05fb6895770f8508b6f24de25ee6eb351
Author: Michael Camilleri <[email protected]>
Date:   Tue, 17 Nov 2026 03:28:25 +0900

Settle survey pages that Back or Next leaves between questions

If the user tapped Back while the slide from Next was still running, the
survey's paged scroll view could drop the change. The slide then
stopped partway, leaving half of each question on screen, or finished
on the next question while the survey counted the previous one as
current. Paging only snaps after a drag, so nothing moved the scroll
view back onto a page.

This commit makes the current question's index the only source of
truth. The scroll view is driven through a ScrollPosition, so a change
of index scrolls to its page, and when scrolling stops the offset is
checked against that index. A scroll that began with a drag and came
to rest on a whole page chooses the current question, as swiping did
before. Any other scroll that stops away from the current question's
page is sent on to it.

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

Diffstat

MReportage/Views/Survey/SurveyView.swift | 54++++++++++++++++++++++++++++++++++++++++++++----------

1 file changed, 44 insertions(+), 10 deletions(-)

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

@@ -232,19 +232,53 @@ struct QuestionPages: View {
             .scrollTargetLayout()
         }
         .scrollTargetBehavior(.paging)
-        .scrollPosition(id: currentQuestionID)
+        .scrollPosition($position)
         .scrollIndicators(.hidden)
+        .onScrollGeometryChange(for: Double.self) { geometry in
+            let width = geometry.containerSize.width
+            return width > 0 ? geometry.contentOffset.x / width : 0
+        } action: { _, page in
+            self.page = page
+        }
+        .onScrollPhaseChange { _, phase in
+            if phase == .interacting {
+                isSwiping = true
+            } else if phase == .idle {
+                settle()
+                isSwiping = false
+            }
+        }
+        .onChange(of: model.index) { turn(to: model.index) }
     }
 
-    /// 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)
-            }
-        )
+    @State private var position = ScrollPosition(idType: String.self)
+    /// The scroll offset in pages: whole when a page fills the screen.
+    @State private var page = 0.0
+    /// Whether the scroll in progress began with a drag.
+    @State private var isSwiping = false
+
+    /// Scrolls to the page at `index` if the scroll view isn't already on it.
+    private func turn(to index: Int) {
+        guard model.questions.indices.contains(index), !isShowing(index) else { return }
+        withAnimation { position.scrollTo(id: model.questions[index].id) }
+    }
+
+    /// Runs when scrolling stops. A swipe chooses the current question. A
+    /// scroll from Back or Next can stop short of its page, or on the wrong
+    /// one when the index changed again before it finished, so it's sent on
+    /// to the current question's page.
+    private func settle() {
+        let nearest = Int(page.rounded())
+        if isSwiping, isShowing(nearest) {
+            model.show(nearest)
+        } else {
+            turn(to: model.index)
+        }
+    }
+
+    /// Whether the page at `index` fills the screen.
+    private func isShowing(_ index: Int) -> Bool {
+        abs(page - Double(index)) < 0.001
     }
 }