SortByConnector(
indexes: Map<Int, IndexName>
searcher: HitsSearcher
selected: Int?
)
SortBy
displays a list of indices,
allowing a user to change the way hits are sorted
(using replica indices).
Another common use case is to let users switch between different indices to show different results.
For this to work, you must define all indices that you pass to SortBy
as replicas of the main index.
Examples
class MyActivity : AppCompatActivity() {
val indexTitle = IndexName("mobile_demo_movies")
val indexYearAsc = IndexName("mobile_demo_movies_year_asc")
val indexYearDesc = IndexName("mobile_demo_movies_year_desc")
val searcher = HitsSearcher(
applicationID = ApplicationID("YourApplicationID"),
apiKey = APIKey("YourSearchOnlyAPIKey"),
indexName = indexTitle
)
val indexes = mapOf(
0 to indexTitle,
1 to indexYearAsc,
2 to indexYearDesc
)
val sortBy = SortByConnector(searcher, indexes, selected = 0)
val connection = ConnectionHandler(sortBy)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val adapter = ArrayAdapter<String>(this, R.layout.menu_item)
val view = SortByViewAutocomplete(autoCompleteTextView, adapter)
connection += sortBy.connectView(view) { indexName ->
when (indexName) {
indexTitle -> "Default"
indexYearAsc -> "Year Asc"
indexYearDesc -> "Year Desc"
else -> indexName.raw
}
}
searcher.searchAsync()
}
override fun onDestroy() {
super.onDestroy()
searcher.cancel()
connection.disconnect()
}
}
Low-level API
To add SortBy
to your search experience, use these components:
Searcher
. The Searcher
that handles your searches.
SortByViewModel
. The logic applied to the index sorting/switching.
SortByViewAutocomplete
. The concrete view implementing SortByView.
IndexPresenter
. Optional. The presenter that converts an Index to a String output.
class MyActivity : AppCompatActivity() {
val indexTitle = IndexName("mobile_demo_movies")
val indexYearAsc = IndexName("mobile_demo_movies_year_asc")
val indexYearDesc = IndexName("mobile_demo_movies_year_desc")
val searcher = HitsSearcher(
applicationID = ApplicationID("YourApplicationID"),
apiKey = APIKey("YourSearchOnlyAPIKey"),
indexName = indexTitle
)
val indexes = mapOf(
0 to indexTitle,
1 to indexYearAsc,
2 to indexYearDesc
)
val viewModel = SortByViewModel(map = indexes, selected = 0)
val connection = ConnectionHandler()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val adapter = ArrayAdapter<String>(this, R.layout.menu_item)
val view = SortByViewAutocomplete(autoCompleteTextView, adapter)
connection += viewModel.connectSearcher(searcher)
connection += viewModel.connectView(view){ indexName ->
when (indexName) {
indexTitle -> "Default"
indexYearAsc -> "Year Asc"
indexYearDesc -> "Year Desc"
else -> indexName.raw
}
}
searcher.searchAsync()
}
override fun onDestroy() {
super.onDestroy()
searcher.cancel()
connection.disconnect()
}
}
Compose UI
class MyActivity : AppCompatActivity() {
private val movies = IndexName("mobile_demo_movies")
private val moviesAsc = IndexName("mobile_demo_movies_year_asc")
private val moviesDesc = IndexName("mobile_demo_movies_year_desc")
private val hitsState = HitsState<Movie>()
private val indexes = mapOf(
0 to movies,
1 to moviesAsc,
2 to moviesDesc
)
private val searcher = HitsSearcher(
applicationID = ApplicationID("YourApplicationID"),
apiKey = APIKey("YourSearchOnlyAPIKey"),
indexName = indexTitle
)
private val sortByState = SortByState()
private val sortBy = SortByConnector(searcher, indexes, selected = 0)
private val connections = ConnectionHandler(sortBy)
init {
connections += searcher.connectHitsView(hitsState) { it.hits.deserialize(Movie.serializer()) }
connections += sortBy.connectView(sortByState) { indexName ->
when (indexName) {
movies -> "Default"
moviesAsc -> "Year Asc"
moviesDesc -> "Year Desc"
else -> indexName.raw
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MySortByScreen() // your own UI composable to display sorting options
}
searcher.searchAsync()
}
override fun onDestroy() {
super.onDestroy()
searcher.cancel()
connections.clear()
}
}
Parameters
indexes
Map<Int, IndexName>
required
The list of indices to search in.
The Searcher
that handles your searches.
The index to select. By default, none is selected.
View
The view that renders the list of indices.InstantSearch includes the SortByViewSpinner
and SortByViewAutocomplete
implementations.
presenter
IndexPresenter
default:"IndexPresenterImpl"
The presenter that defines the way we want to display an index,
taking as input an Index and returning a String.val adapter = ArrayAdapter<String>(this, R.layout.menu_item) // `ArrayAdapter` implementation
val view = SortByViewAutocomplete(autoCompleteTextView, adapter)
sortBy.connectView(view) { index ->
when (index) {
indexTitle -> "Default"
indexYearAsc -> "Year Asc"
indexYearDesc -> "Year Desc"
else -> index.indexName.raw
}
}