UI libraries / InstantSearch Android / Widgets
Signature
StatsConnector(
  searcher: HitsSearcher,
  responseSearch: ResponseSearch
)

About this widget

Each search Response contains various metadata that you might display in your search experience.

The following information is available as a part of the Response:

  • hitsPerPage: number of hits per page.
  • totalHitsCount: total number of hits.
  • pagesCount: total number of pages.
  • page: current page.
  • processingTimeMS: processing time of the search request (in ms).
  • serverTimeMS: processing time of the complete request (in ms).
  • query: query text that produced these results.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = ApplicationID("YourApplicationID"),
        apiKey = APIKey("YourSearchOnlyAPIKey"),
        indexName = IndexName("YourIndexName")
    )
    val stats = StatsConnector(searcher)
    val connection = ConnectionHandler(stats)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val statsView = StatsTextView(statsA)
        connection += stats.connectView(statsView, StatsPresenterImpl())
        searcher.searchAsync()
    }

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

Low-level API

If you want to fully control the Stats components and connect them manually, use the following components:

  • Searcher: The Searcher that handles your searches.
  • StatsViewModel: The logic applied to the stats.
  • StatsView: The view that renders the stats.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = ApplicationID("YourApplicationID"),
        apiKey = APIKey("YourSearchOnlyAPIKey"),
        indexName = IndexName("YourIndexName")
    )
    val statsViewModel = StatsViewModel()
    val connection = ConnectionHandler()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val statsView = StatsTextView(statsA)
        connection += statsViewModel.connectSearcher(searcher)
        connection += statsViewModel.connectView(statsView)
        searcher.searchAsync()
    }

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

Compose UI

InstantSearch provides the StatsState as a state model, which is an implementation of the StatsView interface. You need to connect StatsState to the StatsConnector or StatsViewModel like any other StatsView 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
class MyActivity : AppCompatActivity() {
    val searcher = HitsSearcher(
        applicationID = ApplicationID("YourApplicationID"),
        apiKey = APIKey("YourSearchOnlyAPIKey"),
        indexName = IndexName("YourIndexName")
    )
    val statsState = StatsTextState()
    val stats = StatsConnector(searcher)
    val connections = ConnectionHandler(stats)

    init {
        connections += stats.connectView(statsState, StatsPresenterImpl())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyStats(statsState) // your own UI composable to display stats
        }
        searcher.searchAsync()
    }

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

Parameters

searcher
type: HitsSearcher
Required

The Searcher that handles your searches.

responseSearch
type: ResponseSearch
Optional

The initial search response to render.

View

view
Required

The view that renders the stats.

presenter
type: StatsPresenter<T>
Required

The presenter that defines the way we want to display stats, taking as input a Response and returning a T.

1
2
3
val view = StatsTextViewSpanned(statsB)
val presenter: StatsPresenter<String> = StatsPresenterImpl() // or your own `StatsPresenter` implementation
statsConnector.connectView(view, presenter)
Did you find this page helpful?