Clear the search query when leaving the search tab

Switching away from the search tab now resets the query, so returning to
it shows the recent searches instead of the previous results. The search
field no longer focuses itself on appearance.
This commit is contained in:
Stefan Ceriu
2026-09-10 17:27:37 +03:00
parent 82e666bfe1
commit e6cffcba3f
@@ -97,14 +97,19 @@ struct SearchScreen: View {
.searchResultsKeyboardNavigation(moveUp: { moveSelection(backwards: true) },
moveDown: { moveSelection(backwards: false) },
cancel: { context.send(viewAction: .cancel) })
// The TabView calls onAppear each time the search tab is selected, so the field re-focuses
// and the selection resets on every switch.
// The TabView calls onAppear/onDisappear each time the search tab is selected/deselected,
// so the query is cleared and the selection resets on every switch.
.onAppear {
context.send(viewAction: .appeared)
isSearchFieldFocused = true
selectedID = selectableIDs.first
updateHardwareKeyboardConnected()
}
.onDisappear {
// Previews and snapshot tests mount and unmount the view, don't clear their fixed state.
guard !ProcessInfo.isXcodePreview, !ProcessInfo.isRunningTests else { return }
context.searchQuery = ""
}
.onReceive(NotificationCenter.default.publisher(for: .GCKeyboardDidConnect)) { _ in
updateHardwareKeyboardConnected()
}