Skip to content

Commit

Permalink
Fixes #2040 - Prevent the room list from staying empty after cancelli… (
Browse files Browse the repository at this point in the history
#2041)

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

* Address PR comments, simplify implementation
  • Loading branch information
stefanceriu authored Nov 7, 2023
1 parent 5dcb24a commit 8d20cbf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
35 changes: 17 additions & 18 deletions ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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))
}
}

Expand Down
1 change: 1 addition & 0 deletions changelog.d/2040.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prevent the room list from staying empty after cancelling a search

0 comments on commit 8d20cbf

Please sign in to comment.