-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathSuggestionsTableViewController.swift
55 lines (43 loc) · 1.58 KB
/
SuggestionsTableViewController.swift
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// SuggestionsTableViewController.swift
// Guides
//
// Created by Vladislav Fitc on 31.03.2022.
//
import Foundation
import InstantSearch
import UIKit
class SuggestionsTableViewController: UITableViewController, HitsController, SearchBoxController {
var onQueryChanged: ((String?) -> Void)?
var onQuerySubmitted: ((String?) -> Void)?
public var hitsSource: HitsInteractor<QuerySuggestion>?
let cellID = "сellID"
override public init(style: UITableView.Style) {
super.init(style: style)
tableView.register(SearchSuggestionTableViewCell.self, forCellReuseIdentifier: cellID)
}
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setQuery(_: String?) {
// not applicable
}
override public func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
return hitsSource?.numberOfHits() ?? 0
}
override public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as? SearchSuggestionTableViewCell else { return .init() }
if let suggestion = hitsSource?.hit(atIndex: indexPath.row) {
cell.setup(with: suggestion)
cell.didTapTypeAheadButton = {
self.onQueryChanged?(suggestion.query)
}
}
return cell
}
override public func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let suggestion = hitsSource?.hit(atIndex: indexPath.row) else { return }
onQuerySubmitted?(suggestion.query)
}
}