API Reference / Android Widgets / Filter Numeric Comparison

Filter Numeric Comparison

Widget 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class MyActivity : AppCompatActivity() {
    val client = ClientSearch(
        ApplicationID("YourApplicationID"),
        APIKey("YourAPIKey")
    )
    val index = client.initIndex(IndexName("YourIndexName"))
    val searcher = SearcherSingleIndex(index)
    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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class MyActivity : AppCompatActivity() {
    val client = ClientSearch(
        ApplicationID("YourApplicationID"),
        APIKey("YourAPIKey")
    )
    val index = client.initIndex(IndexName("YourIndexName"))
    val searcher = SearcherSingleIndex(index)
    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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class MyActivity : AppCompatActivity() {
    val client = ClientSearch(
        ApplicationID("YourApplicationID"),
        APIKey("YourAPIKey")
    )
    val index = client.initIndex(IndexName("YourIndexName"))
    val searcher = SearcherSingleIndex(index)
    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
type: FilterState
Required

The FilterState that holds filters.

attribute
type: Attribute
Required

The attribute to filter on.

operator
type: NumericOperator
Required

The NumericOperator used to perform a numeric comparison.

number
type: T : Number, T : Comparable<T>
default: null
Optional

Initial numeric value to filter on.

groupID
type: FilterGroupID
default: FilterGroupID(attribute, FilterOperator.And)
Optional

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
type: NumberView<T>
Required

The view that renders the numeric value.

presenter
type: NumberPresenter<T>
default: NumberPresenterImpl
Optional

How to display the numeric value.

1
2
val view: NumberView<Int> = MyFilterYearView() // your `NumberView<T>` implementation
filterNumericComparison.connectView(view) { year -> year?.toString() ?: "" }

Did you find this page helpful?

InstantSearch Android v2