Skip to main content
Signature
FilterMapConnector(
    filters: Map<Int, Filter>,
    filterState: FilterState,
    selected: Int?,
    groupID: FilterGroupID
)

About this widget

Components holding a map of filters, and that can apply a single filter at a time.

Examples

Kotlin
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = ApplicationID("YourApplicationID"),
        apiKey = APIKey("YourSearchOnlyAPIKey"),
        indexName = IndexName("YourIndexName")
    )
    val filterState = FilterState()
    val gender = Attribute("gender")
    val groupGender = groupAnd(gender)
    val filters = mapOf(
        R.id.male to Filter.Facet(gender, "male"),
        R.id.female to Filter.Facet(gender, "female")
    )
    val filterMap = FilterMapConnector(
        filters = filters,
        filterState = filterState,
        groupID = groupGender
    )
    val connection = ConnectionHandler(filterMap, searcher.connectFilterState(filterState))

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val viewGender = FilterMapViewRadioGroup(radioGroupGender)
        connection += filterMap.connectView(viewGender)
        searcher.searchAsync()
    }

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

Low-level API

If you want to fully control the Filter Map components and connect them manually, use the following components:
  • Searcher. The Searcher that handles searches.
  • FilterState. The current state of the filters.
  • FilterMapViewModel. The logic applied to the filter map.
  • FilterMapView. The view that renders the filter map.
Kotlin
class MyActivity : AppCompatActivity() {
    val gender = Attribute("gender")
    val filterState = FilterState()
    val searcher = HitsSearcher(
        applicationID = ApplicationID("YourApplicationID"),
        apiKey = APIKey("YourSearchOnlyAPIKey"),
        indexName = IndexName("YourIndexName")
    )
    val filters = mapOf(
        0 to Filter.Facet(gender, "male"),
        1 to Filter.Facet(gender, "female")
    )
    val viewModel = FilterMapViewModel(filters)
    val connection = ConnectionHandler()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val view =  FilterMapViewRadioGroup(radioGroupGender)
        connection += viewModel.connectFilterState(filterState)
        connection += viewModel.connectView(view)
        connection += viewModel.connectFilterState(filterState)

        searcher.searchAsync()
    }

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

Compose UI

InstantSearch provides the FilterMapState as a state model, which is an implementation of the FilterMapView interface. FilterMapState must be connected to the FilterMapConnector or FilterMapViewModel like any other FilterMapView implementation.
Kotlin
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = ApplicationID("YourApplicationID"),
        apiKey = APIKey("YourSearchOnlyAPIKey"),
        indexName = IndexName("YourIndexName")
    )
    val filterState = FilterState()
    val gender = Attribute("gender")
    val groupGender = groupAnd(gender)
    val filters = mapOf(
        0 to Filter.Facet(gender, "male"),
        1 to Filter.Facet(gender, "female")
    )
    val filterMapState = FilterMapState()
    val filterMap = FilterMapConnector(
        filters = filters,
        filterState = filterState,
        groupID = groupGender
    )
    val connections = ConnectionHandler(filterMap)

    init {
        connections += searcher.connectFilterState(filterState)
        connections += filterMap.connectView(filterMapState)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyFilterMap(filterMapState) // your own UI composable to display filter map options
        }
        searcher.searchAsync()
    }

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

Parameters

filters
Map<Int, Filter>
required
The map of filters to be held. The key is an unique identifier for the filter value.
Kotlin
val gender = Attribute("gender")
val filters = mapOf(
    0 to Filter.Facet(gender, "male"),
    1 to Filter.Facet(gender, "female")
)
filterState
FilterState
required
The FilterState that holds filters.
selected
Int?
default:"null"
The key of the filter selected by default.
groupID
FilterGroupID
default:"FilterGroupID(FilterOperator.And)"
Groups all created filters under an ID.

View

view
FilterMapView
required
The view that renders the filter map.
presenter
default:"FilterPresenterImpl()"
How to display filters.
Kotlin
val view = FilterMapViewRadioGroup(radioGroupGender)
val presenter = FilterPresenterImpl()
filterMap.connectView(view, presenter)
I