The presenter defining how to display the HierarchicalFacet list.Takes a list of HierarchicalFacet as input and returns a new HierarchicalFacet list.A HierarchicalFacet is a tuple of a Facet, its level, and whether it’s selected or not.
Swift
Report incorrect code
Copy
public static let present: HierarchicalPresenter = { facets in let levels = Set(facets.map { $0.level }).sorted() guard !levels.isEmpty else { return facets } var output: [HierarchicalFacet] = [] output.reserveCapacity(facets.count) levels.forEach { level in let facetsForLevel = facets .filter { $0.level == level } .sorted { $0.facet.value < $1.facet.value } let indexToInsert = output .lastIndex { $0.isSelected } .flatMap { output.index(after: $0) } ?? output.endIndex output.insert(contentsOf: facetsForLevel, at: indexToInsert) } return output }
The default controllers, for example, HierarchicalTableViewController,
work well when you want to use native UIKit with their default behavior.If you want to use another component 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 HierarchicalController protocol.
var onClick: ((String) -> Void)?:Closure to call when a new hierarchical facet is clicked.func setItem(_ item: [HierarchicalFacet])Function called when a new array of hierarchical facets is updated. This is the UI State of the refinement list. Make sure to reload your view here when you get the new items.
InstantSearch provides the HierarchicalList SwiftUI view which you can embed in your views.
It uses HierarchicalObservableController as a data model,
which is an implementation of the HierarchicalController protocol adapted for usage with SwiftUI.
HierarchicalObservableController must be connected to the HierarchicalConnector or HierarchicalInteractor like any other HierarchicalController implementation.
You can define the appearance of the view representing a single hierarchical facet and its selection state or use the HierarchicalFacetRow view provided by InstantSearch.
Swift
Report incorrect code
Copy
struct ContentView: View { @ObservedObject let hierarchicalController: HierarchicalObservableController var body: some View { HierarchicalList(hierarchicalController) { facet, nestingLevel, isSelected in // Use the implementation provided by InstantSearch // HierarchicalFacetRow(facet: facet, nestingLevel: nestingLevel, isSelected: isSelected) // Or declare a custom single hierarchical facet view HStack(spacing: 10) { Image(systemName: isSelected ? "chevron.down" : "chevron.right") .font(.callout) Text("\(facet.value) (\(facet.count))") .fontWeight(isSelected ? .semibold : .regular) .contentShape(Rectangle()) Spacer() } .padding(.leading, CGFloat(nestingLevel * 20)) } }}
If you prefer to create a custom SwiftUI view that presents the list of hierarchical facets,
you can directly use the HierarchicalObservableController as a data model.
It provides the hierarchicalFacets property along with the toggle function to streamline the design process of your custom SwiftUI view.