-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathCustomListCellConfiguration.swift
105 lines (84 loc) · 3.68 KB
/
CustomListCellConfiguration.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
//
// CustomListCellConfiguration.swift
// MullvadVPN
//
// Created by Jon Petersson on 2024-02-14.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//
import Combine
import UIKit
struct CustomListCellConfiguration {
let tableView: UITableView
let subject: CurrentValueSubject<CustomListViewModel, Never>
var onDelete: (() -> Void)?
func dequeueCell(
at indexPath: IndexPath,
for itemIdentifier: CustomListItemIdentifier,
validationErrors: Set<CustomListFieldValidationError>
) -> UITableViewCell {
let cell = tableView.dequeueReusableView(withIdentifier: itemIdentifier.cellIdentifier, for: indexPath)
configureBackground(cell: cell, itemIdentifier: itemIdentifier, validationErrors: validationErrors)
switch itemIdentifier {
case .name:
configureName(cell, itemIdentifier: itemIdentifier)
case .addLocations, .editLocations:
configureLocations(cell, itemIdentifier: itemIdentifier)
case .deleteList:
configureDelete(cell, itemIdentifier: itemIdentifier)
}
return cell
}
private func configureBackground(
cell: UITableViewCell,
itemIdentifier: CustomListItemIdentifier,
validationErrors: Set<CustomListFieldValidationError>
) {
configureErrorState(
cell: cell,
itemIdentifier: itemIdentifier,
contentValidationErrors: validationErrors
)
guard let cell = cell as? any DynamicBackgroundConfiguration else { return }
cell.setAutoAdaptingBackgroundConfiguration(.mullvadListGroupedCell(), selectionType: .dimmed)
}
private func configureErrorState(
cell: UITableViewCell,
itemIdentifier: CustomListItemIdentifier,
contentValidationErrors: Set<CustomListFieldValidationError>
) {
let itemsWithErrors = CustomListItemIdentifier.fromFieldValidationErrors(contentValidationErrors)
if itemsWithErrors.contains(itemIdentifier) {
cell.layer.cornerRadius = 10
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor.Cell.validationErrorBorderColor.cgColor
} else {
cell.layer.borderWidth = 0
}
}
private func configureName(_ cell: UITableViewCell, itemIdentifier: CustomListItemIdentifier) {
var contentConfiguration = TextCellContentConfiguration()
contentConfiguration.text = itemIdentifier.text
contentConfiguration.setPlaceholder(type: .required)
contentConfiguration.textFieldProperties = .withSmartFeaturesDisabled()
contentConfiguration.inputText = subject.value.name
contentConfiguration.editingEvents.onChange = subject.bindTextAction(to: \.name)
cell.contentConfiguration = contentConfiguration
}
private func configureLocations(_ cell: UITableViewCell, itemIdentifier: CustomListItemIdentifier) {
var contentConfiguration = UIListContentConfiguration.mullvadValueCell(tableStyle: tableView.style)
contentConfiguration.text = itemIdentifier.text
cell.contentConfiguration = contentConfiguration
if let cell = cell as? any CustomCellDisclosureHandling {
cell.disclosureType = .chevron
}
}
private func configureDelete(_ cell: UITableViewCell, itemIdentifier: CustomListItemIdentifier) {
var contentConfiguration = ButtonCellContentConfiguration()
contentConfiguration.style = .tableInsetGroupedDanger
contentConfiguration.text = itemIdentifier.text
contentConfiguration.primaryAction = UIAction { _ in
onDelete?()
}
cell.contentConfiguration = contentConfiguration
}
}