The controllers provided by default,
like the CurrentFilterListTableController work well when you want to use native UIKit with their default behavior.If you want to use another component (other than a UITableView) such as a UICollectionView,
a third-party input view,
or you want to introduce some custom behavior to the already provided UIKit component,
you can create your own controller conforming to the CurrentFiltersController protocol.
func setItems(_ item: [FilterAndID]):Function called when current filters are refreshed and need to be updated.Note that FilterAndID is a struct that contains the filter, its ID, and the text representation of the filtervar onRemoveItem: ((FilterAndID) -> Void)?:Closure to call when a “remove filter” intention is detected on the corresponding current filter.func reload():Function called when the view needs to reload itself with new data.
InstantSearch provides the CurrentFiltersObservableController data model,
which is an implementation of the CurrentFiltersController protocol adapted for usage with SwiftUI.
CurrentFiltersObservableController must be connected to the CurrentFiltersConnector or CurrentFiltersConnector like any other CurrentFiltersController implementation.The example of the current filters view presenting the grouped filters.
Swift
Report incorrect code
Copy
struct ContentView: View { @ObservedObject var currentFiltersController: CurrentFiltersObservableController var body: some View { VStack { Text("Filters") .font(.title) let filtersPerGroup = Dictionary(grouping: currentFiltersController.filters) { el in el.id } .mapValues { $0.map(\.filter) } .map { $0 } ForEach(filtersPerGroup, id: \.key) { (group, filters) in HStack { Text(group.description) .bold() .padding(.leading, 5) Spacer() } .padding(.vertical, 5) .background(Color(.systemGray5)) ForEach(filters, id: \.self) { filter in HStack { Text(filter.description) .padding(.leading, 5) Spacer() } } } Spacer() } }}
If you prefer to create a custom SwiftUI view that presents each ,
use the CurrentFiltersObservableController as a data model.
It provides the filters property along with toggle and isSelected functions to streamline the design process of your custom SwiftUI view.