-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterController.swift
More file actions
341 lines (266 loc) · 11.5 KB
/
Copy pathFilterController.swift
File metadata and controls
341 lines (266 loc) · 11.5 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//
// FilterController.swift
// Stats
//
// Created by Leonid Kibukevich on 21.09.2021.
//
import UIKit
protocol FilterControllerDelegate: AnyObject {
func didUpdateSelected(filters: [String], parameters: [String])
func localParameters() -> [FilterItem]
func didChangeHeight(height: CGFloat)
}
protocol FilterControllerOutput {
func viewReadyToWork()
func didPressCellFilter(filterId: String, categoryIndex: Int)
func didPressButtonFilter(categoryIndex: Int)
func selectedFilters() -> [String]
func selectedParameters() -> [String]
func didChooseFiltersFromPopUp(values: [CategoryValueFilterViewModel], parentValue: CategoryValueFilterViewModel?, category: CategoryFilterViewModel)
}
protocol FilterControllerInput {
func localParameters() -> [FilterItem]
func updateData(filters: [CategoryFilterViewModel])
func finishLoadFilters(filtersWithDefaultSelected: [CategoryFilterViewModel])
func showPopUpFilters(for filter: CategoryFilterViewModel, parentValue: CategoryValueFilterViewModel?, title: String, selectedValues: [CategoryValueFilterViewModel], images: [ImageMappingFilterElement])
}
class FilterController: UIViewController, UITableViewDelegate, UITableViewDataSource, FilterPopUpControllerDelegate, FiltersRowCellDelegate, FilterRowCompactModeCellDelegate {
enum State {
case expanded
case minimized
case landscape
}
enum Constants {
static let cellHeight: CGFloat = 40
static let topInset: CGFloat = 12
}
private let tableViewInset = UIEdgeInsets(top: 8, left: 12, bottom: 8, right: 12)
weak var delegate: FilterControllerDelegate?
private var presenter: FilterControllerOutput?
private let tableView = UITableView()
private var filters: [CategoryFilterViewModel] = []
private let borderView = UIView()
private let expandButton = UIButton(type: .custom)
private var showBorder: Bool = true
private var isLoading: Bool = true
private var isScrollToFirstSelected: Bool = false
var state: State = .expanded
init(delegate: FilterControllerDelegate, isPopUpNewRowMode: Bool, filterType: FilterType, showBorder: Bool, isScrollToFirstSelected: Bool = false) {
super.init(nibName: nil, bundle: nil)
self.delegate = delegate
self.showBorder = showBorder
self.isScrollToFirstSelected = isScrollToFirstSelected
presenter = FilterPresenter(controller: self, isPopUpNewRowMode: isPopUpNewRowMode, filterType: filterType)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
configureTableView()
configureBorderView(showBorder: showBorder)
configureExpandButton()
presenter?.viewReadyToWork()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
recalculateHeightTableView()
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
self.updateAppearance()
}
/// Height only TableView
private var tableViewHeight: CGFloat {
return isLoading ? SkeletonCell.preferedFilterSkeletonHeight : CGFloat(self.filters.count) * Constants.cellHeight
}
func updateAppearance() {
if UIDevice.current.orientation.isLandscape {
state = .landscape
expandButton.isHidden = true
} else {
state = .expanded
expandButton.isHidden = true
expandButton.isSelected = false
}
tableView.reloadData()
recalculateHeightTableView()
}
// MARK: - Actions
@objc func expandButtonAction() {
expandButton.isSelected = !expandButton.isSelected
if expandButton.isSelected {
state = .minimized
} else {
state = .expanded
}
tableView.reloadData()
recalculateHeightTableView()
}
// MARK: - Private
private func configureBorderView(showBorder: Bool) {
view.insertSubview(borderView, at: 0)
borderView.backgroundColor = .white
let cornerRadius: CGFloat = 17
borderView.layer.cornerRadius = cornerRadius
if showBorder {
borderView.layer.borderWidth = 1
borderView.layer.borderColor = Colors.Palette.f4f4f4.cgColor
} else {
borderView.addShadowView(cornerRadius: cornerRadius)
}
borderView.clipsToBounds = true
borderView.isHidden = false
borderView.snp.makeConstraints { make in
make.left.equalTo(12)
make.right.equalTo(-12)
make.top.equalTo(Constants.topInset)
make.bottom.equalTo(tableView.snp.bottom).offset(tableViewInset.bottom)
}
}
private func configureTableView() {
borderView.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
tableView.isScrollEnabled = false
tableView.separatorStyle = .none
tableView.registerClass(FiltersRowCell.self)
tableView.registerClass(FilterRowCompactModeCell.self)
tableView.clipsToBounds = true
tableView.snp.makeConstraints { make in
make.left.equalTo(tableViewInset.left)
make.top.equalTo(tableViewInset.top)
make.right.equalTo(-tableViewInset.left)
make.bottom.equalTo(0)
make.height.greaterThanOrEqualTo(tableViewHeight)
}
}
private func configureExpandButton() {
view.addSubview(expandButton)
expandButton.isHidden = true
expandButton.setImage(UIImage(named: "plus"), for: .selected)
expandButton.setImage(UIImage(named: "minus"), for: .normal)
expandButton.addTarget(self, action: #selector(expandButtonAction), for: .touchUpInside)
expandButton.snp.makeConstraints { make in
make.width.height.equalTo(40)
make.top.equalTo(borderView.snp.top)
make.right.equalTo(borderView.snp.right)
}
}
private func recalculateHeightTableView() {
let offset: CGFloat = state == .expanded ? 0 : 12
tableView.snp.remakeConstraints { make in
make.left.equalTo(tableViewInset.left)
make.top.equalTo(tableViewInset.top)
make.right.equalTo(-tableViewInset.left)
make.bottom.equalTo(-tableViewInset.bottom)
make.height.greaterThanOrEqualTo(tableViewHeight + offset)
}
delegate?.didChangeHeight(height: tableViewHeight + tableViewInset.bottom + tableViewInset.top + Constants.topInset + offset)
}
// MARK: - FilterPopUpControllerDelegate
func reportFiltersPickerViewController(_ controller: FilterPopUpController, didSelect values: [CategoryValueFilterViewModel], for filter: CategoryFilterViewModel, parentValue: CategoryValueFilterViewModel?) {
presenter?.didChooseFiltersFromPopUp(values: values, parentValue: parentValue, category: filter)
}
// MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
guard !isLoading
else {
return SkeletonCell.preferedFilterSkeletonHeight
}
switch state {
case .expanded:
return Constants.cellHeight
case .minimized, .landscape:
return 64
}
}
// MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard !isLoading
else {
return 1
}
switch state {
case .expanded:
return filters.count
case .minimized:
return 1
case .landscape:
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard !isLoading
else {
let cell: SkeletonCell = .init(skeletonType: .filter)
cell.startSkeleton()
return cell
}
switch state {
case .expanded:
let cell: FiltersRowCell = tableView.dequeueReusableCellForIndexPath(indexPath)
cell.delegate = self
cell.setFilters(filters[indexPath.row].cells, categoryIndex: indexPath.row, isScrollToFirstSelected: isScrollToFirstSelected)
return cell
case .minimized:
let cell: FilterRowCompactModeCell = tableView.dequeueReusableCellForIndexPath(indexPath)
cell.configure(categories: filters, isShowsDeselected: false)
cell.delegate = self
return cell
case .landscape:
let cell: FilterRowCompactModeCell = tableView.dequeueReusableCellForIndexPath(indexPath)
cell.configure(categories: filters, isShowsDeselected: true)
cell.delegate = self
return cell
}
}
// MARK: - FiltersRowCellDelegate, FilterRowCompactModeCellDelegate
func didPressCellFilter(data: CategoryValueFilterViewModel, categoryIndex: Int) {
presenter?.didPressCellFilter(filterId: data.itemId, categoryIndex: categoryIndex)
}
func didPressButtonFilter(categoryIndex: Int) {
presenter?.didPressButtonFilter(categoryIndex: categoryIndex)
}
}
extension FilterController: FilterControllerInput {
func localParameters() -> [FilterItem] {
return delegate?.localParameters() ?? []
}
func showPopUpFilters(for filter: CategoryFilterViewModel, parentValue: CategoryValueFilterViewModel?, title: String, selectedValues: [CategoryValueFilterViewModel], images: [ImageMappingFilterElement]) {
let vc = FilterPopUpController.buildView(filter: filter, parentValue: parentValue, screenTitle: title, values: filter.values, selectedViewValues: selectedValues, delegate: self, images: images)
self.present(vc: vc)
}
func finishLoadFilters(filtersWithDefaultSelected: [CategoryFilterViewModel]) {
isLoading = false
filters = filtersWithDefaultSelected
tableView.reloadData()
recalculateHeightTableView()
delegate?.didUpdateSelected(
filters: presenter?.selectedFilters() ?? [],
parameters: presenter?.selectedParameters() ?? []
)
}
func updateData(filters: [CategoryFilterViewModel]) {
self.filters = filters
tableView.reloadData()
delegate?.didUpdateSelected(
filters: presenter?.selectedFilters() ?? [],
parameters: presenter?.selectedParameters() ?? []
)
recalculateHeightTableView()
}
func recursiveSearchSelectedFilters(filters: [CategoryValueFilterViewModel]) -> [String] {
var result = [String]()
filters.forEach { filter in
if filter.isSelected {
if filter.itemId.isNotEmpty {
result.append(filter.itemId)
result.append(contentsOf: recursiveSearchSelectedFilters(filters: filter.children))
}
}
}
return result
}
}