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
…ng a search
  • Loading branch information
stefanceriu committed Nov 7, 2023
1 parent b13abf9 commit 0e743dd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
28 changes: 13 additions & 15 deletions ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,19 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
.weakAssign(to: \.state.selectedRoomID, on: self)
.store(in: &cancellables)

context.$viewState
.map(\.bindings.searchQuery)
.removeDuplicates()
.sink { [weak self] searchQuery in
guard let self else { return }
updateFilter(isSearchFieldFocused: state.bindings.isSearchFieldFocused, searchQuery: searchQuery)
}
.store(in: &cancellables)
struct SearchBarState: Equatable {
let searchQuery: String
let isSearchFieldFocused: Bool
}

context.$viewState
.map(\.bindings.isSearchFieldFocused)
.map { SearchBarState(searchQuery: $0.bindings.searchQuery, isSearchFieldFocused: $0.bindings.isSearchFieldFocused) }
.removeDuplicates()
.sink { [weak self] isSearchFieldFocused in
.sink { [weak self] _ 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.
updateFilter()
}
.store(in: &cancellables)

Expand Down Expand Up @@ -170,13 +168,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 0e743dd

Please sign in to comment.