Sunday, December 15, 2024
HomeiOS Developmentios - How can I greatest use dismissSearch and isSearching in my...

ios – How can I greatest use dismissSearch and isSearching in my search view?


I am taking part in round with a search view the place I take advantage of search scopes and search options. For the search options I need to test if the search is energetic and dismiss the search when a suggestion is tapped to cease displaying the keyboard. To take action I need to use isSearching() and dismissSearch() however these can solely be used from a toddler view. Making a toddler view would imply passing the search textual content, search scopes, chosen search scope and search options all as enter to the kid view (as a @Binding). That may be simplified by making a SearchModel which holds these values and passing that to the kid view.

It feels cumbersome to need to make all that knowledge obtainable within the search view and its baby view. Is that this the most effective/solely method or is there a greater method to do that?

struct SearchView: View {
    @State personal var searchText = ""
    @State personal var searchScopes = [SearchScope]()
    @State personal var searchScope: SearchScope?
    @State personal var searchSuggestions = [SearchSuggestion]()

    var physique: some View {
        NavigationStack {
            Textual content("Looking for (searchText)")
        }
        .searchable(textual content: $searchText)
        .searchScopes($searchScope, activation: .onSearchPresentation, {
            ForEach(searchScopes) { scope in
                Textual content(scope.label).tag(scope)
            }
        })
        .searchSuggestions({
            ForEach(searchSuggestions, id: .self) { suggestion in
                Button("(suggestion.label)") {
                    searchScope = searchScopes.first(the place: { $0.label == suggestion.label })
                    searchText = suggestion.worth

                    // I do not name the runSearch() methodology as a result of the onSubmit triggers for some purpose which calls the runSearch()...
                }
            }
        })
        .onSubmit(of: .search, runSearch)
        .onChange(of: searchScope) {
            Process {
                await runSearchSuggestions()
            }
        }
        .onChange(of: searchText) {
            Process {
                await runSearchSuggestions()
            }
        }
        .process {
            self.searchScopes = await httpClient.fetchSearchScopes()
            if !searchScopes.isEmpty {
                self.searchScope = searchScopes[0]
            }
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments