diff --git a/ElementX/Sources/Screens/HomeScreen/HomeScreenModels.swift b/ElementX/Sources/Screens/HomeScreen/HomeScreenModels.swift index 2521ec885..8a9f45877 100644 --- a/ElementX/Sources/Screens/HomeScreen/HomeScreenModels.swift +++ b/ElementX/Sources/Screens/HomeScreen/HomeScreenModels.swift @@ -100,6 +100,11 @@ struct HomeScreenViewState: BindableState { 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 { diff --git a/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift b/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift index ce2a933f3..d4be6994b 100644 --- a/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift +++ b/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift @@ -109,7 +109,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol // 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 // 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() } } @@ -170,12 +170,14 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol // MARK: - Private private func updateFilter() { - if !state.bindings.isSearchFieldFocused { - roomSummaryProvider?.setFilter(.all) - } else if state.bindings.searchQuery.isEmpty { + if state.shouldHideRoomList { roomSummaryProvider?.setFilter(.none) } else { - roomSummaryProvider?.setFilter(.normalizedMatchRoomName(state.bindings.searchQuery)) + if state.bindings.isSearchFieldFocused { + roomSummaryProvider?.setFilter(.normalizedMatchRoomName(state.bindings.searchQuery)) + } else { + roomSummaryProvider?.setFilter(.all) + } } } diff --git a/ElementX/Sources/Screens/HomeScreen/View/HomeScreenRoomList.swift b/ElementX/Sources/Screens/HomeScreen/View/HomeScreenRoomList.swift index aad9403e5..fcbfd8be5 100644 --- a/ElementX/Sources/Screens/HomeScreen/View/HomeScreenRoomList.swift +++ b/ElementX/Sources/Screens/HomeScreen/View/HomeScreenRoomList.swift @@ -22,10 +22,20 @@ struct HomeScreenRoomList: View { @ObservedObject var context: HomeScreenViewModel.Context var body: some View { - content + filteredContent .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 private var content: some View { ForEach(context.viewState.visibleRooms) { room in diff --git a/changelog.d/pr-2112.bugfix b/changelog.d/pr-2112.bugfix new file mode 100644 index 000000000..4f73c9146 --- /dev/null +++ b/changelog.d/pr-2112.bugfix @@ -0,0 +1 @@ +Fix room list search bar focus glitches \ No newline at end of file