FilterComparisonConnector<Number: Comparable & DoubleRepresentable>(
searcher: HitsSearcher,
filterState: FilterState,
attribute: Attribute,
numericOperator: Filter.Numeric.Operator,
number: Number?,
bounds: ClosedRange<Number>?,
operator: RefinementOperator,
groupName: String?
)
Filter Numeric Comparison is a view to filter on a numeric value using a comparison operator.
Examples
Instantiate a FilterComparisonConnector
and launch an initial search on its searcher.
let searcher = HitsSearcher(appID: "YourApplicationID",
apiKey: "YourApiKey",
indexName: "YourIndexName")
let filterState = FilterState()
let stepperController = NumericStepperController()
let filterComparisonConnector = FilterComparisonConnector<Double>(searcher: searcher,
filterState: filterState,
numericOperator: .greaterThanOrEqual,
attribute: "price",
controller: stepperController)
searcher.search()
Low-level API
If you want to fully control the Filter Numeric Comparison components and connect them manually, use the following components:
Searcher
. The Searcher
that handles your searches.
FilterState
. The current state of the filters.
NumberInteractor<T>
. The logic applied to the numeric value.
NumberController
. The view controller that renders the numeric value.
let searcher = HitsSearcher(appID: "YourApplicationID",
apiKey: "YourApiKey",
indexName: "YourIndexName")
let filterState = FilterState()
let numberInteractor = NumberInteractor<Double>()
let stepperController = NumericStepperController()
interactor.connectSearcher(searcher, attribute: "price")
interactor.connectFilterState(filterState,
attribute: attribute,
numericOperator: numericOperator,
operator: .greaterThanOrEqual)
interactor.connectNumberController(stepperController)
searcher.search()
SwiftUI
InstantSearch provides the NumberObservableController
as a state model,
which is an implementation of the NumberController
interface.
You need to connect NumberObservableController
to the FilterComparisonConnector
or NumberInteractor
like any other NumberController
implementation.
struct ContentView: View {
@ObservedObject var priceController: NumberObservableController<Double>
var body: some View {
HStack {
Stepper(value: $priceController.value,
in: priceController.bounds,
step: 0.1) {
HStack {
Text("Price:")
Spacer()
Text(String(format: "%.2f", priceController.value))
}
}
}
}
}
If you prefer to create a custom SwiftUI view that presents the numeric range,
you can directly use the NumberObservableController
as a data model.
It provides value
and bounds
properties to streamline the design process of your custom SwiftUI view.
Parameters
The Searcher that handles your searches
The attribute to filter on.
numericOperator
Filter.Numeric.Operator
required
The comparison operator to apply.
number
Number: Comparable & DoubleRepresentable
default: nil
The initial numeric value to filter on.
bounds
ClosedRange<Number>?
default: nil
The optional bounds limiting the max and the min value of the number.
operator
RefinementOperator
required
default: .and
Whether the filter is added to a conjuncitve(and
) or a disjuncitve (or
) group in the filter state.
Filter group name in the filter state.
If not specified, the attribute value is used as the group name.
Controller
Controller interfacing with a concrete number view.let controller = MyNumberController() // your `NumberController` implementation
filterComparisonConnector.connectNumberController(controller)