SortBy
About this widget
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 the user 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
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
class MyActivity : AppCompatActivity() {
val client = ClientSearch(
ApplicationID("YourApplicationID"),
APIKey("YourAPIKey")
)
val index = client.initIndex(IndexName("YourIndexName"))
val indexTitle = client.initIndex(IndexName("mobile_demo_movies"))
val indexYearAsc = client.initIndex(IndexName("mobile_demo_movies_year_asc"))
val indexYearDesc = client.initIndex(IndexName("mobile_demo_movies_year_desc"))
val searcher = SearcherSingleIndex(indexTitle)
private val indexes = mapOf(
0 to indexTitle,
1 to indexYearAsc,
2 to indexYearDesc
)
private val sortBy = SortByConnector(indexes = indexes, searcher = searcher, selected = 0)
private 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) { index ->
when (index) {
indexTitle -> "Default"
indexYearAsc -> "Year Asc"
indexYearDesc -> "Year Desc"
else -> index.indexName.raw
}
}
searcher.searchAsync()
}
override fun onDestroy() {
super.onDestroy()
searcher.cancel()
connection.disconnect()
}
}
Low-level API
If you want to fully control the SortBy components and connect them manually, use the following components:
To add SortBy
to your search experience, use these components:
Searcher
: TheSearcher
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.
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
class MyActivity : AppCompatActivity() {
val client = ClientSearch(
ApplicationID("YourApplicationID"),
APIKey("YourAPIKey")
)
val index = client.initIndex(IndexName("YourIndexName"))
val indexTitle = client.initIndex(IndexName("mobile_demo_movies"))
val indexYearAsc = client.initIndex(IndexName("mobile_demo_movies_year_asc"))
val indexYearDesc = client.initIndex(IndexName("mobile_demo_movies_year_desc"))
val searcher = SearcherSingleIndex(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){ index ->
when (index) {
indexTitle -> "Default"
indexYearAsc -> "Year Asc"
indexYearDesc -> "Year Desc"
else -> index.indexName.raw
}
}
searcher.searchAsync()
}
override fun onDestroy() {
super.onDestroy()
searcher.cancel()
connection.disconnect()
}
}
Parameters
indexes
|
type: Map<Int, Index>
Required
The list of indices to search in. |
searcher
|
type: SearcherSingleIndex
Required
The |
selected
|
type: Int?
default: null
Optional
The index to select. By default, none is selected. |
View
view
|
type: SortByView
Required
The view that renders the list of indices. |
||
presenter
|
type: IndexPresenter
default: IndexPresenterImpl
Optional
The presenter that defines the way we want to display an index, taking as input an Index and returning a String. |
||
Copy
|