API Reference / Android Widgets / Current Filters
Apr. 24, 2019

Current Filters

About this widget

Current Refinements shows the currently active refinements within a given FilterState and lets users remove filters individually.

To add current refinements to your search experience, use these components:

  • FilterCurrentViewModel: The logic for current refinements in the FilterState.
  • FilterState: The current state of the filters.
  • FilterCurrentView: The view that will render the current filters.
  • FilterCurrentPresenter: Optional. The presenter that defines the way we want to display the Filters.

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
31
class MyActivity : AppCompatActivity() {

    val client = ClientSearch(
        ApplicationID("YourApplicationID"),
        APIKey("YourAPIKey")
    )
    val index = client.initIndex(IndexName("YourIndexName"))
    val searcher = SearcherSingleIndex(index)
    val filterState = FilterState()
    val viewModel = FilterCurrentViewModel()
    val connection = ConnectionHandler()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val chipGroup = ChipGroup(this)
        val view: FilterCurrentView = FilterCurrentViewImpl(chipGroup)

        connection += searcher.connectFilterState(filterState)
        connection += viewModel.connectFilterState(filterState)
        connection += viewModel.connectView(view)

        searcher.searchAsync()
    }

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

Parameters

searcher
type: Searcher
Required

The Searcher that handles your searches.

1
viewModel.connectSearcher(searcher)
filterState
type: FilterState
Required

The FilterState that will hold your filters.

1
viewModel.connectFilterState(filterState)
filterCurrentView
type: FilterCurrentView
Required

The view that will render the current filters.

1
viewModel.connectView(filterCurrentView)
items
type: Map<FilterAndID, Filter>
default: mapOf()
Optional

The default filters to display.

1
FilterCurrentViewModel(items = listOf(Filter.Facet(Attribute("foo"), "bar")))
groupIDs
type: List<FilterGroupID>
default: listOf()
Optional

When specified, only matching current refinements will be displayed.

1
2
val groupIDs = listOf(FilterGroupID(Attribute("color"))
viewModel.connectFilterState(filterState, groupIDs = groupIDs)

Presenter

presenter
type: FilterPresenter
default: FilterCurrentPresenterImpl()
Optional

The presenter that defines the way we want to display a filter.

1
2
3
4
5
6
7
val presenter = object : FilterCurrentPresenter {
    override fun invoke(filterIDs: Map<FilterAndID, Filter>): List<Pair<FilterAndID, String>> {
        return filterIDs.map { (key, value) -> key to FilterPresenterImpl()(value) }
            .sortedWith(Comparator { a, b -> a.second.compareTo(b.second) })
    }
}
viewModel.connectView(view, presenter)

Did you find this page helpful?