forked from algolia/instantsearch-ios
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuerySuggestionsRecentSearches.SearchResultController.swift
212 lines (172 loc) · 6.76 KB
/
QuerySuggestionsRecentSearches.SearchResultController.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// SearchResultController.swift
// QuerySuggestionsRecentSearches
//
// Created by Vladislav Fitc on 05/11/2021.
//
import Foundation
import InstantSearch
import UIKit
extension QuerySuggestionsAndRecentSearches {
class SearchResultsController: UITableViewController, HitsController {
var hitsSource: HitsInteractor<QuerySuggestion>?
var recentSearches: [String] = ["last search"]
var onSelection: ((String) -> Void)?
private let headerTitleFontSize: CGFloat = 15
private let headerTitleHeight: CGFloat = 28
enum Section: Int, CaseIterable {
case recentSearches
case querySuggestions
var header: String {
switch self {
case .querySuggestions:
return "Suggestions"
case .recentSearches:
return "Recent searches"
}
}
var cellReuseIdentifier: String {
switch self {
case .querySuggestions:
return "suggestion"
case .recentSearches:
return "recentSearch"
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(SearchSuggestionTableViewCell.self, forCellReuseIdentifier: Section.querySuggestions.cellReuseIdentifier)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: Section.recentSearches.cellReuseIdentifier)
tableView.sectionHeaderTopPadding = 0
}
override func numberOfSections(in tableView: UITableView) -> Int {
return Section.allCases.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let section = Section(rawValue: section) else { return 0 }
switch section {
case .recentSearches:
return recentSearches.count
case .querySuggestions:
return hitsSource?.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 .recentSearches:
cell = tableView.dequeueReusableCell(withIdentifier: Section.recentSearches.cellReuseIdentifier, for: indexPath)
cell.imageView?.image = UIImage(systemName: "clock.arrow.circlepath")
cell.textLabel?.text = recentSearches[indexPath.row]
case .querySuggestions:
cell = tableView.dequeueReusableCell(withIdentifier: Section.querySuggestions.cellReuseIdentifier, for: indexPath)
if
let suggestionCell = cell as? SearchSuggestionTableViewCell,
let suggestion = hitsSource?.hit(atIndex: indexPath.row) {
suggestionCell.setup(with: suggestion)
}
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let section = Section(rawValue: indexPath.section) else { return }
switch section {
case .recentSearches:
onSelection?(recentSearches[indexPath.row])
case .querySuggestions:
hitsSource?.hit(atIndex: indexPath.row).flatMap {
onSelection?($0.query)
}
}
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
guard let section = Section(rawValue: section) else { return nil }
switch section {
case .recentSearches:
return recentSearches.isEmpty ? nil : section.header
case .querySuggestions:
let isEmpty = hitsSource.flatMap { $0.numberOfHits() == 0 } ?? true
return isEmpty ? nil : section.header
}
}
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
guard let section = Section(rawValue: indexPath.section) else { return .none }
switch section {
case .recentSearches:
return .delete
case .querySuggestions:
return .none
}
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
guard let section = Section(rawValue: indexPath.section) else { return }
switch section {
case .recentSearches:
deleteRecentSearch(atIndex: indexPath.row)
case .querySuggestions:
break
}
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
guard let section = Section(rawValue: section) else { return 0 }
switch section {
case .recentSearches:
return recentSearches.isEmpty ? 0 : headerTitleHeight
case .querySuggestions:
return headerTitleHeight
}
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let section = Section(rawValue: section) else { return nil }
if case(.recentSearches) = section, !recentSearches.isEmpty {
let header = SectionHeader(frame: .zero)
header.label.font = .systemFont(ofSize: headerTitleFontSize, weight: .semibold)
header.label.text = Section.recentSearches.header
header.button.addTarget(self, action: #selector(clearRecentSearches), for: .touchUpInside)
return header
} else {
return nil
}
}
private func deleteRecentSearch(atIndex index: Int) {
recentSearches.remove(at: index)
tableView.beginUpdates()
tableView.reloadSections([Section.recentSearches.rawValue], with: .automatic)
tableView.endUpdates()
}
@objc private func clearRecentSearches() {
recentSearches.removeAll()
tableView.beginUpdates()
tableView.reloadSections([Section.recentSearches.rawValue], with: .automatic)
tableView.endUpdates()
}
}
}
private class SectionHeader: UIView {
let label = UILabel()
let button = UIButton()
override init(frame: CGRect) {
super.init(frame: frame)
label.textColor = UIColor.secondaryLabel
label.translatesAutoresizingMaskIntoConstraints = false
button.translatesAutoresizingMaskIntoConstraints = false
button.setImage(UIImage(systemName: "trash"), for: .normal)
let separator = UIView()
separator.translatesAutoresizingMaskIntoConstraints = false
let stackView = UIStackView()
stackView.distribution = .fill
stackView.isLayoutMarginsRelativeArrangement = true
stackView.layoutMargins = .init(top: 0, left: 20, bottom: 0, right: -20)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.addArrangedSubview(label)
stackView.addArrangedSubview(separator)
stackView.addArrangedSubview(button)
addSubview(stackView)
stackView.pin(to: self)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}