UI libraries / InstantSearch Android / Widgets
Signature
FilterClearConnector(
  filterState: FilterState,
  groupIDs: List<FilterGroupID>,
  mode: ClearMode,
  viewModel: FilterClearViewModel
)

About this widget

ClearFilters lets users clear all refinements that are currently active within the given FilterState.

To add ClearFilters to your search experience, use these components:

  • FilterClearViewModel: The logic for clearing refinements in the FilterState.
  • FilterState: The current state of the filters.
  • FilterClearView: The view that renders the clear filter UI.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = ApplicationID("YourApplicationID"),
        apiKey = APIKey("YourSearchOnlyAPIKey"),
        indexName = IndexName("YourIndexName")
    )
    val color = Attribute("color")
    val category = Attribute("category")
    val groupColor = groupOr(color)
    val groupCategory = groupOr(category)
    val filters = filters {
        group(groupColor) {
            facet(color, "red")
            facet(color, "green")
        }
        group(groupCategory) {
            facet(category, "shoe")
        }
    }
    val filterState = FilterState(filters)
    val clearAll = FilterClearConnector(filterState = filterState)
    val clearSpecified = FilterClearConnector(
        filterState = filterState,
        groupIDs = listOf(groupColor),
        mode = ClearMode.Specified
    )
    val clearExcept = FilterClearConnector(
        filterState = filterState,
        groupIDs = listOf(groupColor),
        mode = ClearMode.Except
    )
    val connection = ConnectionHandler(
        clearSpecified, clearExcept, searcher.connectFilterState(filterState)
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        connection += clearAll.connectView(FilterClearViewImpl(filtersClearAll))
        connection += clearSpecified.connectView(FilterClearViewImpl(buttonClearSpecified))
        connection += clearExcept.connectView(FilterClearViewImpl(buttonClearExcept))
        searcher.searchAsync()
    }

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

Compose UI

InstantSearch provides the FilterClear as a state model, which is an implementation of the FilterClearView interface. You need to connect FilterClear to the FilterClearConnector or FilterClearViewModel like any other FilterClearView 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
37
38
39
40
41
42
43
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = ApplicationID("YourApplicationID"),
        apiKey = APIKey("YourSearchOnlyAPIKey"),
        indexName = IndexName("YourIndexName")
    )
    val color = Attribute("color")
    val category = Attribute("category")
    val groupColor = groupOr(color)
    val groupCategory = groupOr(category)
    val filters = filters {
        group(groupColor) {
            facet(color, "red")
            facet(color, "green")
        }
        group(groupCategory) {
            facet(category, "shoe")
        }
    }
    val filterState = FilterState()
    val filterClear = FilterClear()
    val clearAll = FilterClearConnector(filterState = filterState)
    val connections = ConnectionHandler(clearAll)

    init {
        connections += searcher.connectFilterState(filterState)
        connections += clearAll.connectView(filterClear)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyClearButton(clearAll) // your own composable to trigger filter clear
        }
        searcher.searchAsync()
    }

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

Parameters

Parameter Description
filterState
type: FilterState
Required

The FilterState that will hold your filters.

groupIDs
type: List<FilterGroupID>
default: listOf()
Optional

The groupIDs of filters to clear. All filters will be cleared if unspecified.

mode
type: ClearMode
default: ClearMode.Specified
Optional

Whether we should clear the Specified filters or all filters Except them.

viewModel
type: FilterClearViewModel
default: FilterClearViewModel()
Optional

The logic for clearing refinements in the FilterState.

View

Parameter Description
view
type: FilterClearView
Required

The view that renders the clear filter UI.

1
2
val view = FilterClearViewImpl(myButton) // using your own Android view
filterClearConnector.connectView(view)
Did you find this page helpful?