Skip to main content
Signature
FilterComparisonConnector<T>(
    filterState: FilterState,
    attribute: Attribute,
    operator: NumericOperator,
    number: T?,
    groupID: FilterGroupID
) where T : Number, T : Comparable<T>

About this widget

Filter Numeric Comparison is a view to filter on a numeric value using a comparison operator.

Examples

Kotlin
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = ApplicationID("YourApplicationID"),
        apiKey = APIKey("YourSearchOnlyAPIKey"),
        indexName = IndexName("YourIndexName")
    )
    val filterState = FilterState()
    val price = Attribute("price")
    val operator = NumericOperator.GreaterOrEquals
    val filterComparison = FilterComparisonConnector<Long>(
        filterState = filterState,
        attribute = price,
        operator = operator
    )
    val connection = ConnectionHandler(filterComparison, searcher.connectFilterState(filterState))

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val priceView: NumberView<Long> = MyFilterPriceView() // your `NumberView<T>` implementation
        connection += filterComparison.connectView(priceView)
        searcher.searchAsync()
    }

    override fun onDestroy() {
        super.onDestroy()
        connection.disconnect()
        searcher.cancel()
    }
}

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.
  • NumberViewModel<T>. The logic applied to the numeric value.
  • NumberView<T>. The view that renders the numeric value.
Kotlin
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = ApplicationID("YourApplicationID"),
        apiKey = APIKey("YourSearchOnlyAPIKey"),
        indexName = IndexName("YourIndexName")
    )
    val filterState = FilterState()
    val price = Attribute("price")
    val operator = NumericOperator.Greater
    val viewModel = NumberViewModel(range = 0..10)
    val connection = ConnectionHandler()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val view: NumberView<Int> = MyFilterPriceView() // your `NumberView<T>` implementation
        searcher.query.addFacet(price)
        connection += searcher.connectFilterState(filterState)
        connection += viewModel.connectView(view)
        connection += viewModel.connectFilterState(filterState, price, operator)

        searcher.searchAsync()
    }

    override fun onDestroy() {
        super.onDestroy()
        connection.disconnect()
        searcher.cancel()
    }
}

Compose UI

InstantSearch provides the NumberState as a state model, which is an implementation of the NumberView interface. You need to connect NumberState to the FilterComparisonConnector or NumberViewModel like any other NumberView implementation.
Kotlin
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = ApplicationID("YourApplicationID"),
        apiKey = APIKey("YourSearchOnlyAPIKey"),
        indexName = IndexName("YourIndexName")
    )
    val filterState = FilterState()
    val price = Attribute("price")
    val priceState = NumberState<Long>()
    val filterComparison = FilterComparisonConnector<Long>(
        filterState = filterState,
        attribute = price,
        operator = NumericOperator.GreaterOrEquals
    )
    val connections = ConnectionHandler(filterComparison)

    init {
        connections += searcher.connectFilterState(filterState)
        connections += filterComparison.connectView(priceState)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyFilterPrice(priceState) // your own UI composable to display `text` and `computation`
        }
        searcher.searchAsync()
    }

    override fun onDestroy() {
        super.onDestroy()
        connections.disconnect()
        searcher.cancel()
    }
}

Parameters

filterState
FilterState
required
The FilterState that holds filters.
attribute
Attribute
required
The attribute to filter on.
operator
NumericOperator
required
The NumericOperator used to perform a numeric comparison.
number
T : Number, T : Comparable<T>
default: null
Initial numeric value to filter on.
groupID
FilterGroupID
default: FilterGroupID(attribute, FilterOperator.And)
Groups all created filters under an ID and composes them with this operator. Defaults to the used attribute, with FilterOperator.And between filters in this group.

View

view
NumberView<T>
required
The view that renders the numeric value.
presenter
NumberPresenter<T>
default:"NumberPresenterImpl"
How to display the numeric value.
Kotlin
val view: NumberView<Int> = MyFilterYearView() // your `NumberView<T>` implementation
filterNumericComparison.connectView(view) { year -> year?.toString() ?: "" }
I