From 50e622007600f53e0a7bc42da26523e9fb497b17 Mon Sep 17 00:00:00 2001 From: Mauro <34335419+Velin92@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:56:53 +0100 Subject: [PATCH] Fix for isSearchFieldFocus not always updating properly (#2456) --- .../Screens/HomeScreen/HomeScreenViewModel.swift | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift b/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift index ad2ab21b5..7142cd352 100644 --- a/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift +++ b/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift @@ -110,13 +110,11 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol isSearchFieldFocused .combineLatest(searchQuery, enabledFilters) .removeDuplicates { $0 == $1 } - .map { _ in () } - .sink { [weak self] in + .sink { [weak self] isSearchFieldFocused, _, _ in guard let self else { return } - // 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 { + // isSearchFieldFocused` is sometimes turning to true after cancelling the search. So to be extra sure we are updating the values correctly we read them directly in the next run loop, and we add a small delay if the value has changed + let delay = isSearchFieldFocused == self.context.viewState.bindings.isSearchFieldFocused ? 0.0 : 0.05 + DispatchQueue.main.asyncAfter(deadline: .now() + delay) { self.updateFilter() } }