-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathQuerySuggestionsCategories.SearchResultsController.swift
128 lines (111 loc) · 4.06 KB
/
QuerySuggestionsCategories.SearchResultsController.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// SearchResultsController.swift
// QuerySuggestionsCategories
//
// Created by Vladislav Fitc on 05/11/2021.
//
import Foundation
import InstantSearch
import UIKit
extension QuerySuggestionsCategories {
class SearchResultsController: UITableViewController {
var didSelectSuggestion: ((String) -> Void)?
enum Section: Int, CaseIterable {
case categories
case suggestions
var title: String {
switch self {
case .categories:
return "Categories"
case .suggestions:
return "Suggestions"
}
}
var cellReuseIdentifier: String {
switch self {
case .categories:
return "categories"
case .suggestions:
return "suggestions"
}
}
}
weak var categoriesInteractor: FacetListInteractor? {
didSet {
oldValue?.onResultsUpdated.cancelSubscription(for: tableView)
guard let interactor = categoriesInteractor else { return }
interactor.onResultsUpdated.subscribe(with: tableView) { tableView, _ in
tableView.reloadData()
}.onQueue(.main)
}
}
weak var suggestionsInteractor: HitsInteractor<QuerySuggestion>? {
didSet {
oldValue?.onResultsUpdated.cancelSubscription(for: tableView)
guard let interactor = suggestionsInteractor else { return }
interactor.onResultsUpdated.subscribe(with: tableView) { tableView, _ in
tableView.reloadData()
}.onQueue(.main)
}
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(SearchSuggestionTableViewCell.self, forCellReuseIdentifier: Section.suggestions.cellReuseIdentifier)
tableView.register(CategoryTableViewCell.self, forCellReuseIdentifier: Section.categories.cellReuseIdentifier)
}
override func numberOfSections(in _: UITableView) -> Int {
return Section.allCases.count
}
override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let section = Section(rawValue: section) else { return 0 }
switch section {
case .categories:
return categoriesInteractor?.items.count ?? 0
case .suggestions:
return suggestionsInteractor?.numberOfHits() ?? 0
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let section = Section(rawValue: indexPath.section) else { return UITableViewCell() }
let cell: UITableViewCell
switch section {
case .categories:
cell = tableView.dequeueReusableCell(withIdentifier: Section.categories.cellReuseIdentifier, for: indexPath)
if
let category = categoriesInteractor?.items[indexPath.row],
let categoryCell = cell as? CategoryTableViewCell {
categoryCell.setup(with: category)
}
case .suggestions:
cell = tableView.dequeueReusableCell(withIdentifier: Section.suggestions.cellReuseIdentifier, for: indexPath)
if
let suggestion = suggestionsInteractor?.hit(atIndex: indexPath.row),
let suggestionCell = cell as? SearchSuggestionTableViewCell {
suggestionCell.setup(with: suggestion)
}
}
return cell
}
override func tableView(_: UITableView, titleForHeaderInSection section: Int) -> String? {
guard let section = Section(rawValue: section) else { return nil }
switch section {
case .categories where categoriesInteractor?.items.count ?? 0 == 0:
return nil
case .suggestions where suggestionsInteractor?.numberOfHits() ?? 0 == 0:
return nil
default:
return section.title
}
}
override func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let section = Section(rawValue: indexPath.section) else { return }
switch section {
case .suggestions:
suggestionsInteractor?.hit(atIndex: indexPath.row).flatMap { didSelectSuggestion?($0.query) }
case .categories:
// Handle category selection
break
}
}
}
}