Sort By
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.
To add SortBy
to your search experience, use these components:
Searcher
: TheSearcher
that handles your searches.IndexSegmentInteractor
: The logic applied to the index sorting/switching.SelectableSegmentController
: The controller that interfaces with a concrete index list.IndexPresenter
: Optional. The presenter that converts an Index to a String output.
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
let alert = UIAlertController(title: "Change Index", message: "Please select a new index", preferredStyle: .actionSheet)
let searcher: SingleIndexSearcher = .init(appID: "YourApplicationID",
apiKey: "YourSearchOnlyAPIKey",
indexName: "indexDefault")
let indexSegmentInteractor: IndexSegmentInteractor = {
let indices [
0 : indexDefault,
1 : indexAscendingOrder,
2 : indexDescendingOrder
]
return .init(items: indices)
}()
lazy var selectIndexController: SelectIndexController = { return .init(alertController: alert) }()
let client: Client = .init(appID: "YourApplicationID", apiKey: "YourSearchOnlyAPIKey")
lazy var indexDefault: Index = { return client.index(withName: "indexDefault") }()
lazy var indexAscendingOrder: Index = { return client.index(withName: "indexAscendingOrder") }()
lazy var indexDescendingOrder: Index = { return client.index(withName: "indexDescendingOrder") }()
override func viewDidLoad() {
super.viewDidLoad()
indexSegmentInteractor.connectSearcher(searcher: searcher)
indexSegmentInteractor.connectController(SelectIndexController(alertController: alert)) { (index) -> String in
switch index {
case self.indexDefault: return "Default"
case self.indexAscendingOrder: return "Year Asc"
case self.indexDescendingOrder: return "Year Desc"
default: return index.name
}
}
}
Parameters
items
|
type: [Int: Index]
Required
The list of indices to search in. |
||
Copy
|
Presenter
Index Presenter
|
type: IndexPresenter
Optional
The presenter that defines the way we want to display an index, taking as input an Index and returning a String. |
||
Copy
|
Customize your view
The controllers provided by default, like the SelectIndexController
work well when you want to use native UIKit with their default behavior like a UIAlertController
.
If you want to use another component, a third-party input view, or you want to introduce some custom behavior to the already provided UIKit component, you can create your own controller conforming to the SelectableSegmentController
protocol.
Protocol
func setSelected(_ selected: Int?)
:
Function called when an index is selected, with the position that is selected.
func setItems(items: [Int: String])
Function called when a new array of indices is defined.
func reload()
Function called when we require a reload of the list view.
var onClick: ((Int) -> Void)?
:
Closure to call when a new index is clicked.
Example
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
public class SelectIndexController: NSObject, SelectableSegmentController {
let alertController: UIAlertController
public var onClick: ((Int) -> Void)?
public init(alertController: UIAlertController) {
self.alertController = alertController
super.init()
}
public func setSelected(_ selected: Int?) {
// Show a check mark next to the item selected here
}
public func setItems(items: [Int: String]) {
guard alertController.actions.isEmpty else { return }
for item in items {
alertController.addAction(UIAlertAction(title: item.value, style: .default, handler: { [weak self] _ in
self?.onClick?(item.key)
}))
}
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: .none))
}
}