Fixes #2040 - Prevent the room list from staying empty after cancelli… (#2041)

* Fixes #2040 - Prevent the room list from staying empty after cancelling a search

* Address PR comments, simplify implementation
This commit is contained in:
Stefan Ceriu 2023-11-07 17:54:04 +02:00 committed by GitHub
parent 5dcb24add7
commit 8d20cbfc68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 18 deletions

View File

@ -98,21 +98,20 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
.weakAssign(to: \.state.selectedRoomID, on: self)
.store(in: &cancellables)
context.$viewState
.map(\.bindings.searchQuery)
.removeDuplicates()
.sink { [weak self] searchQuery in
let isSearchFieldFocused = context.$viewState.map(\.bindings.isSearchFieldFocused)
let searchQuery = context.$viewState.map(\.bindings.searchQuery)
isSearchFieldFocused
.combineLatest(searchQuery)
.removeDuplicates { $0 == $1 }
.map { _ in () }
.sink { [weak self] in
guard let self else { return }
updateFilter(isSearchFieldFocused: state.bindings.isSearchFieldFocused, searchQuery: searchQuery)
}
.store(in: &cancellables)
context.$viewState
.map(\.bindings.isSearchFieldFocused)
.removeDuplicates()
.sink { [weak self] isSearchFieldFocused in
guard let self else { return }
updateFilter(isSearchFieldFocused: isSearchFieldFocused, searchQuery: state.bindings.searchQuery)
// 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 {
self.updateFilter()
}
}
.store(in: &cancellables)
@ -170,13 +169,13 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
// MARK: - Private
private func updateFilter(isSearchFieldFocused: Bool, searchQuery: String) {
if !isSearchFieldFocused {
private func updateFilter() {
if !state.bindings.isSearchFieldFocused {
roomSummaryProvider?.setFilter(.all)
} else if searchQuery.isEmpty {
} else if state.bindings.searchQuery.isEmpty {
roomSummaryProvider?.setFilter(.none)
} else {
roomSummaryProvider?.setFilter(.normalizedMatchRoomName(searchQuery))
roomSummaryProvider?.setFilter(.normalizedMatchRoomName(state.bindings.searchQuery))
}
}

1
changelog.d/2040.bugfix Normal file
View File

@ -0,0 +1 @@
Prevent the room list from staying empty after cancelling a search