Fix room list search bar focus glitches (#2112)

* Fixes #2112 - Room list search bar focus glitches

* Address PR comments
This commit is contained in:
Stefan Ceriu 2023-11-17 18:08:29 +02:00 committed by GitHub
parent d245a4f158
commit f717c7dba4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 6 deletions

View File

@ -100,6 +100,11 @@ struct HomeScreenViewState: BindableState {
HomeScreenRoom.placeholder() HomeScreenRoom.placeholder()
} }
} }
// Used to hide all the rooms when the search field is focused and the query is empty
var shouldHideRoomList: Bool {
bindings.isSearchFieldFocused && bindings.searchQuery.isEmpty
}
} }
struct HomeScreenViewStateBindings { struct HomeScreenViewStateBindings {

View File

@ -109,7 +109,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
// Don't capture the values here as combine behaves incorrectly and `isSearchFieldFocused` is sometimes // Don't capture the values here as combine behaves incorrectly and `isSearchFieldFocused` is sometimes
// turning to true after cancelling the search. Read them directly from the state in the updateFilter // turning to true after cancelling the search. Read them directly from the state in the updateFilter
// method instead on the next run loop to make sure they're up to date. // method instead on the next run loop to make sure they're up to date.
DispatchQueue.main.async { DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
self.updateFilter() self.updateFilter()
} }
} }
@ -170,12 +170,14 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
// MARK: - Private // MARK: - Private
private func updateFilter() { private func updateFilter() {
if !state.bindings.isSearchFieldFocused { if state.shouldHideRoomList {
roomSummaryProvider?.setFilter(.all)
} else if state.bindings.searchQuery.isEmpty {
roomSummaryProvider?.setFilter(.none) roomSummaryProvider?.setFilter(.none)
} else { } else {
roomSummaryProvider?.setFilter(.normalizedMatchRoomName(state.bindings.searchQuery)) if state.bindings.isSearchFieldFocused {
roomSummaryProvider?.setFilter(.normalizedMatchRoomName(state.bindings.searchQuery))
} else {
roomSummaryProvider?.setFilter(.all)
}
} }
} }

View File

@ -22,10 +22,20 @@ struct HomeScreenRoomList: View {
@ObservedObject var context: HomeScreenViewModel.Context @ObservedObject var context: HomeScreenViewModel.Context
var body: some View { var body: some View {
content filteredContent
.onChange(of: isSearchFieldFocused) { context.isSearchFieldFocused = $0 } .onChange(of: isSearchFieldFocused) { context.isSearchFieldFocused = $0 }
} }
@ViewBuilder
private var filteredContent: some View {
// Hide the room list when the search bar is focused but the query is empty
// This works hand in hand with the room list service layer filtering and
// avoids glitches when focusing the search bar
if !context.viewState.shouldHideRoomList {
content
}
}
@ViewBuilder @ViewBuilder
private var content: some View { private var content: some View {
ForEach(context.viewState.visibleRooms) { room in ForEach(context.viewState.visibleRooms) { room in

View File

@ -0,0 +1 @@
Fix room list search bar focus glitches