commit b3838300d5ed4b3b433ce313a0d058f7a7636c5c
parent 1c4ebb7e1b7c2881acee1cacfe34a50b61941a90
Author: Michael Camilleri <[email protected]>
Date: Tue, 17 Nov 2026 03:53:04 +0900
End editing before turning a survey page
If the user typed in the activities search field, tapped a suggestion
and then tapped Next, the survey moved to the second question but showed
the first. The search field still had focus when the page turned. As the
keyboard closed, the system scrolled the paged view back to bring the
field into view, without any scroll phase, so the settling added in the
previous commit never ran. The header then counted the second question
while the first was on screen.
This commit resigns the first responder before a page turn starts — both
when Back, Next or an answer turns the page and when a drag begins — so
no field is left to be scrolled back into view. As a further guard, an
offset that changes while no scroll is running is checked against the
current question, and the survey is sent on to that question's page if
it is elsewhere.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat
| M | Reportage/Views/Survey/SurveyView.swift | | | 16 | ++++++++++++++++ |
1 file changed, 16 insertions(+), 0 deletions(-)
diff --git a/Reportage/Views/Survey/SurveyView.swift b/Reportage/Views/Survey/SurveyView.swift
@@ -1,4 +1,5 @@
import SwiftUI
+import UIKit
/// What to survey: a new report of `kind` describing `date`, or an existing
/// report to fill in or edit.
@@ -239,10 +240,15 @@ struct QuestionPages: View {
return width > 0 ? geometry.contentOffset.x / width : 0
} action: { _, page in
self.page = page
+ // The offset can change without a scroll, as when the keyboard
+ // closes and the system scrolls a text field into view.
+ if phase == .idle { turn(to: model.index) }
}
.onScrollPhaseChange { _, phase in
+ self.phase = phase
if phase == .interacting {
isSwiping = true
+ endEditing()
} else if phase == .idle {
settle()
isSwiping = false
@@ -256,13 +262,23 @@ struct QuestionPages: View {
@State private var page = 0.0
/// Whether the scroll in progress began with a drag.
@State private var isSwiping = false
+ @State private var phase = ScrollPhase.idle
/// 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 }
+ endEditing()
withAnimation { position.scrollTo(id: model.questions[index].id) }
}
+ /// Takes the keyboard away before the page moves. A field that keeps
+ /// focus while the keyboard closes is scrolled back into view by the
+ /// system, which returns the survey to the field's page.
+ private func endEditing() {
+ UIApplication.shared.sendAction(
+ #selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
+ }
+
/// 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