Searching disables the filters (#2530)

This commit is contained in:
Mauro
2024-03-06 11:02:30 +01:00
committed by GitHub
parent 7f3451216d
commit 7754b9b8dc
18 changed files with 84 additions and 74 deletions
@@ -58,7 +58,7 @@ struct EmojiPickerScreen: View {
.navigationTitle(L10n.commonReactions)
.navigationBarTitleDisplayMode(.inline)
.toolbar { toolbar }
.modifier(IsSearching(isSearching: $isSearching))
.isSearching($isSearching)
.searchable(text: $searchString, placement: .navigationBarDrawer(displayMode: .always))
.compoundSearchField()
}
@@ -87,17 +87,6 @@ struct EmojiPickerScreen: View {
}
}
/// A view modifier to extract whether the search field is focussed from a subview.
private struct IsSearching: ViewModifier {
@Environment(\.isSearching) private var isSearchFieldFocused
@Binding var isSearching: Bool
func body(content: Content) -> some View {
content
.onChange(of: isSearchFieldFocused) { isSearching = $0 }
}
}
// MARK: - Previews
struct EmojiPickerScreen_Previews: PreviewProvider, TestablePreview {
@@ -45,7 +45,7 @@ class GlobalSearchScreenViewModel: GlobalSearchScreenViewModelType, GlobalSearch
.map(\.bindings.searchQuery)
.removeDuplicates()
.sink { [weak self] searchQuery in
self?.roomSummaryProvider.setFilter(.include(.init(query: searchQuery)))
self?.roomSummaryProvider.setFilter(.search(query: searchQuery))
}
.store(in: &cancellables)
@@ -60,7 +60,7 @@ class GlobalSearchScreenViewModel: GlobalSearchScreenViewModelType, GlobalSearch
switch viewAction {
case .dismiss:
actionsSubject.send(.dismiss)
roomSummaryProvider.setFilter(.include(.all)) // This is a shared provider
roomSummaryProvider.setFilter(.all(filters: [])) // This is a shared provider
case .select(let roomID):
actionsSubject.send(.select(roomID: roomID))
case .reachedTop:
@@ -89,7 +89,7 @@ struct HomeScreenViewState: BindableState {
var rooms: [HomeScreenRoom] = []
var roomListMode: HomeScreenRoomListMode = .skeletons
var shouldShowFilters = false
var areFiltersEnabled = false
var markAsUnreadEnabled = false
var markAsFavouriteEnabled = false
@@ -122,6 +122,10 @@ struct HomeScreenViewState: BindableState {
var shouldShowEmptyFilterState: Bool {
shouldShowFilters && bindings.filtersState.isFiltering && visibleRooms.isEmpty
}
var shouldShowFilters: Bool {
areFiltersEnabled && !bindings.isSearchFieldFocused
}
}
struct HomeScreenViewStateBindings {
@@ -106,10 +106,10 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
return
}
if !value {
state.shouldShowFilters = false
state.areFiltersEnabled = false
state.bindings.filtersState.clearFilters()
} else {
state.shouldShowFilters = true
state.areFiltersEnabled = true
}
}
.store(in: &cancellables)
@@ -233,10 +233,9 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
roomSummaryProvider?.setFilter(.excludeAll)
} else {
if state.bindings.isSearchFieldFocused {
roomSummaryProvider?.setFilter(.include(.init(query: state.bindings.searchQuery,
filters: state.bindings.filtersState.activeFilters.set)))
roomSummaryProvider?.setFilter(.search(query: state.bindings.searchQuery))
} else {
roomSummaryProvider?.setFilter(.include(.init(filters: state.bindings.filtersState.activeFilters.set)))
roomSummaryProvider?.setFilter(.all(filters: state.areFiltersEnabled ? state.bindings.filtersState.activeFilters.set : []))
}
}
}
@@ -37,21 +37,21 @@ struct RoomListFilterView_Previews: PreviewProvider, TestablePreview {
private struct FilterToggleStyle: ToggleStyle {
private func strokeColor(isOn: Bool) -> Color {
isOn ? .compound.bgSubtleSecondary : .compound.borderInteractiveSecondary
isOn ? .compound.bgActionPrimaryRest : .compound.borderInteractiveSecondary
}
private func backgroundColor(isOn: Bool) -> Color {
isOn ? .compound.bgSubtleSecondary : .compound.bgCanvasDefault
isOn ? .compound.bgActionPrimaryRest : .compound.bgCanvasDefault
}
private func foregroundColor(isOn: Bool) -> Color {
isOn ? .compound.textPrimary : .compound.textSecondary
isOn ? .compound.textOnSolidPrimary : .compound.textPrimary
}
func makeBody(configuration: Configuration) -> some View {
let shape = RoundedRectangle(cornerRadius: 20)
configuration.label
.font(.compound.bodyLGSemibold)
.font(.compound.bodyLG)
.foregroundColor(foregroundColor(isOn: configuration.isOn))
.padding(.horizontal, 16)
.padding(.vertical, 8)
@@ -53,7 +53,7 @@ struct HomeScreenContent: View {
.layoutPriority(1)
}
case .rooms:
if context.viewState.shouldShowFilters {
if context.viewState.areFiltersEnabled {
// Showing empty views in pinned headers makes the room list spasm when reaching the top
LazyVStack(spacing: 0, pinnedViews: [.sectionHeaders]) {
Section {
@@ -68,6 +68,7 @@ struct HomeScreenContent: View {
.readFrame($topSectionFrame)
}
}
.isSearching($context.isSearchFieldFocused)
.searchable(text: $context.searchQuery)
.compoundSearchField()
.disableAutocorrection(true)
@@ -76,6 +77,7 @@ struct HomeScreenContent: View {
LazyVStack(spacing: 0) {
HomeScreenRoomList(context: context)
.isSearching($context.isSearchFieldFocused)
}
.searchable(text: $context.searchQuery)
.compoundSearchField()
@@ -17,17 +17,9 @@
import SwiftUI
struct HomeScreenRoomList: View {
@Environment(\.isSearching) var isSearchFieldFocused
@ObservedObject var context: HomeScreenViewModel.Context
var body: some View {
filteredContent
.onChange(of: isSearchFieldFocused) { context.isSearchFieldFocused = $0 }
}
@ViewBuilder
private var filteredContent: some View {
// Hide the room list when the search bar is focused but the query is empty
// This works hand in hand with the room list service layer filtering and
// avoids glitches when focusing the search bar
@@ -49,7 +49,7 @@ class MessageForwardingScreenViewModel: MessageForwardingScreenViewModelType, Me
.removeDuplicates()
.sink { [weak self] searchQuery in
guard let self else { return }
self.roomSummaryProvider?.setFilter(.include(.init(query: searchQuery)))
self.roomSummaryProvider?.setFilter(.search(query: searchQuery))
}
.store(in: &cancellables)
@@ -60,7 +60,7 @@ class MessageForwardingScreenViewModel: MessageForwardingScreenViewModelType, Me
switch viewAction {
case .cancel:
actionsSubject.send(.dismiss)
roomSummaryProvider?.setFilter(.include(.all))
roomSummaryProvider?.setFilter(.all(filters: []))
case .send:
guard let roomID = state.selectedRoomID else {
fatalError()