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) .weakAssign(to: \.state.selectedRoomID, on: self)
.store(in: &cancellables) .store(in: &cancellables)
context.$viewState let isSearchFieldFocused = context.$viewState.map(\.bindings.isSearchFieldFocused)
.map(\.bindings.searchQuery) let searchQuery = context.$viewState.map(\.bindings.searchQuery)
.removeDuplicates() isSearchFieldFocused
.sink { [weak self] searchQuery in .combineLatest(searchQuery)
.removeDuplicates { $0 == $1 }
.map { _ in () }
.sink { [weak self] in
guard let self else { return } guard let self else { return }
updateFilter(isSearchFieldFocused: state.bindings.isSearchFieldFocused, searchQuery: 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
.store(in: &cancellables) // method instead on the next run loop to make sure they're up to date.
DispatchQueue.main.async {
context.$viewState self.updateFilter()
.map(\.bindings.isSearchFieldFocused) }
.removeDuplicates()
.sink { [weak self] isSearchFieldFocused in
guard let self else { return }
updateFilter(isSearchFieldFocused: isSearchFieldFocused, searchQuery: state.bindings.searchQuery)
} }
.store(in: &cancellables) .store(in: &cancellables)
@ -170,13 +169,13 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
// MARK: - Private // MARK: - Private
private func updateFilter(isSearchFieldFocused: Bool, searchQuery: String) { private func updateFilter() {
if !isSearchFieldFocused { if !state.bindings.isSearchFieldFocused {
roomSummaryProvider?.setFilter(.all) roomSummaryProvider?.setFilter(.all)
} else if searchQuery.isEmpty { } else if state.bindings.searchQuery.isEmpty {
roomSummaryProvider?.setFilter(.none) roomSummaryProvider?.setFilter(.none)
} else { } 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