Add search modifiers (#1070)

This commit is contained in:
Alfonso Grillo
2023-06-13 14:02:38 +02:00
committed by GitHub
parent 73eeb051c6
commit 9d22dac19e
4 changed files with 65 additions and 0 deletions
@@ -0,0 +1,57 @@
//
// Copyright 2023 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
extension View {
/// Disable the interactive dismiss while the search is on.
/// - Note: the modifier needs to be called before the `searchable` modifier to work properly
func disableInteractiveDismissOnSearch() -> some View {
modifier(InteractiveDismissSearchModifier())
}
/// Dismiss search when the view is disappearing. It helps to restore correct state on pop into a NavigationStack
/// - Note: the modifier needs to be called before the `searchable` modifier to work properly
func dismissSearchOnDisappear() -> some View {
modifier(DismissSearchOnDisappear())
}
}
private struct InteractiveDismissSearchModifier: ViewModifier {
@Environment(\.isSearching) private var isSearching
func body(content: Content) -> some View {
if isSearching {
content.interactiveDismissDisabled()
} else {
content
}
}
}
private struct DismissSearchOnDisappear: ViewModifier {
@Environment(\.isSearching) private var isSearching
@Environment(\.dismissSearch) private var dismissSearch
func body(content: Content) -> some View {
content
.onDisappear {
if isSearching {
dismissSearch()
}
}
}
}
@@ -31,6 +31,8 @@ struct InviteUsersScreen: View {
nextButton
}
}
.disableInteractiveDismissOnSearch()
.dismissSearchOnDisappear()
.searchable(text: $context.searchQuery, placement: .navigationBarDrawer(displayMode: .always), prompt: L10n.commonSearchForSomeone)
.introspectSearchController { searchViewController in
searchViewController.hidesNavigationBarDuringPresentation = false
@@ -37,6 +37,8 @@ struct StartChatScreen: View {
closeButton
}
}
.disableInteractiveDismissOnSearch()
.dismissSearchOnDisappear()
.searchable(text: $context.searchQuery, placement: .navigationBarDrawer(displayMode: .always), prompt: L10n.commonSearchForSomeone)
.compoundSearchField()
.alert(item: $context.alertInfo) { $0.alert }