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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
//
// LocationCell.swift
// MullvadVPN
//
// Created by pronebird on 02/05/2019.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import UIKit
protocol LocationCellDelegate: AnyObject {
func toggleExpanding(cell: LocationCell)
func toggleSelecting(cell: LocationCell)
}
class LocationCell: UITableViewCell {
weak var delegate: LocationCellDelegate?
private let locationLabel: UILabel = {
let label = UILabel()
label.font = .mullvadSmall
label.adjustsFontForContentSizeCategory = true
label.textColor = .white
label.lineBreakMode = .byTruncatingTail
label.numberOfLines = 1
return label
}()
private let statusIndicator: UIView = {
let view = UIView()
view.layer.cornerRadius = 8
view.layer.cornerCurve = .circular
return view
}()
private let tickImageView: UIImageView = {
let imageView = UIImageView(image: UIImage.tick)
imageView.adjustsImageSizeForAccessibilityContentSizeCategory = true
imageView.tintColor = .white
return imageView
}()
private let checkboxButton: UIButton = {
let button = UIButton()
let checkboxView = CheckboxView()
checkboxView.isUserInteractionEnabled = false
button.addConstrainedSubviews([checkboxView]) {
checkboxView.centerYAnchor.constraint(equalTo: button.centerYAnchor)
checkboxView.pinEdgesToSuperviewMargins(PinnableEdges([.leading(16), .trailing(16)]))
}
return button
}()
private let collapseButton: UIButton = {
let button = UIButton(type: .custom)
button.isAccessibilityElement = false
button.adjustsImageSizeForAccessibilityContentSizeCategory = true
button.tintColor = .white
return button
}()
private var locationLabelLeadingMargin: CGFloat {
switch behavior {
case .add:
0
case .select:
12
}
}
private var behavior: LocationCellBehavior = .select
private let chevronDown = UIImage.CellDecoration.chevronDown
private let chevronUp = UIImage.CellDecoration.chevronUp
var isDisabled = false {
didSet {
updateDisabled(isDisabled)
updateBackgroundColor()
updateStatusIndicatorColor()
}
}
var isExpanded = false {
didSet {
updateCollapseImage()
}
}
override var indentationLevel: Int {
didSet {
updateBackgroundColor()
setLayoutMargins()
}
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupCell()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setLayoutMargins() {
let indentation = CGFloat(indentationLevel) * indentationWidth
var contentMargins = UIMetrics.locationCellLayoutMargins
contentMargins.leading += indentation
contentView.directionalLayoutMargins = contentMargins
}
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
super.setHighlighted(highlighted, animated: animated)
updateStatusIndicatorColor()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
updateLeadingImage()
updateStatusIndicatorColor()
// Set the accessibility value to indicate selection status
accessibilityValue =
selected
? NSLocalizedString("Selected", comment: "")
: nil
}
private func setupCell() {
indentationWidth = UIMetrics.TableView.cellIndentationWidth
backgroundColor = .clear
contentView.backgroundColor = .clear
backgroundView = UIView()
selectedBackgroundView = UIView()
checkboxButton.addTarget(self, action: #selector(toggleCheckboxButton(_:)), for: .touchUpInside)
collapseButton.addTarget(self, action: #selector(handleCollapseButton(_:)), for: .touchUpInside)
[locationLabel, tickImageView, statusIndicator, collapseButton].forEach { subview in
subview.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(subview)
}
updateCollapseImage()
updateDisabled(isDisabled)
updateBackgroundColor()
setLayoutMargins()
contentView.addConstrainedSubviews([
tickImageView,
statusIndicator,
locationLabel,
collapseButton,
checkboxButton,
]) {
tickImageView.pinEdgesToSuperviewMargins(PinnableEdges([.leading(0)]))
tickImageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor)
statusIndicator.widthAnchor.constraint(equalToConstant: 16)
statusIndicator.heightAnchor.constraint(equalTo: statusIndicator.widthAnchor)
statusIndicator.centerXAnchor.constraint(equalTo: tickImageView.centerXAnchor)
statusIndicator.centerYAnchor.constraint(equalTo: tickImageView.centerYAnchor)
checkboxButton.centerYAnchor.constraint(equalTo: contentView.centerYAnchor)
checkboxButton.trailingAnchor.constraint(equalTo: locationLabel.leadingAnchor, constant: 14)
locationLabel.pinEdgesToSuperviewMargins(PinnableEdges([.top(0), .bottom(0)]))
locationLabel.leadingAnchor.constraint(
equalTo: statusIndicator.trailingAnchor,
constant: locationLabelLeadingMargin
)
locationLabel.trailingAnchor.constraint(lessThanOrEqualTo: collapseButton.leadingAnchor)
.withPriority(.defaultHigh)
collapseButton.widthAnchor.constraint(
equalToConstant: UIMetrics.contentLayoutMargins.leading + UIMetrics.contentLayoutMargins.trailing + 24
)
collapseButton.pinEdgesToSuperview(.all().excluding(.leading))
}
}
private func setupAccessibility(_ locationCellViewModel: LocationCellViewModel) {
isAccessibilityElement = true
accessibilityTraits = .button
// Set the accessibility label to the location name
accessibilityLabel = locationCellViewModel.node.code
// Provide a hint about the action
if !locationCellViewModel.node.children.isEmpty {
accessibilityHint =
locationCellViewModel.node.showsChildren
? NSLocalizedString("Collapses this location.", comment: "")
: NSLocalizedString("Expands this location.", comment: "")
} else {
accessibilityHint = nil
}
let selectAction = UIAccessibilityCustomAction(
name: "SelectLocation",
target: self,
selector: #selector(handleSelectAction)
)
accessibilityCustomActions = [selectAction]
}
private func updateLeadingImage() {
switch behavior {
case .add:
checkboxButton.isHidden = false
statusIndicator.isHidden = true
tickImageView.isHidden = true
case .select:
checkboxButton.isHidden = true
statusIndicator.isHidden = isSelected
tickImageView.isHidden = !isSelected
}
}
private func updateStatusIndicatorColor() {
statusIndicator.backgroundColor = statusIndicatorColor()
}
private func updateDisabled(_ isDisabled: Bool) {
locationLabel.alpha = isDisabled ? 0.2 : 1
if isDisabled {
accessibilityTraits.insert(.notEnabled)
} else {
accessibilityTraits.remove(.notEnabled)
}
}
private func updateBackgroundColor() {
backgroundView?.backgroundColor = backgroundColorForIdentationLevel()
selectedBackgroundView?.backgroundColor = selectedBackgroundColorForIndentationLevel()
}
private func backgroundColorForIdentationLevel() -> UIColor {
switch indentationLevel {
case 1:
return UIColor.Cell.Background.indentationLevelOne
case 2:
return UIColor.Cell.Background.indentationLevelTwo
case 3:
return UIColor.Cell.Background.indentationLevelThree
default:
return UIColor.Cell.Background.indentationLevelZero
}
}
private func selectedBackgroundColorForIndentationLevel() -> UIColor {
if isDisabled {
return UIColor.Cell.Background.disabledSelected
} else {
return UIColor.Cell.Background.selected
}
}
private func statusIndicatorColor() -> UIColor {
if isDisabled {
return UIColor.RelayStatusIndicator.inactiveColor
} else if isHighlighted {
return UIColor.RelayStatusIndicator.highlightColor
} else {
return UIColor.RelayStatusIndicator.activeColor
}
}
@objc private func handleCollapseButton(_ sender: UIControl) {
delegate?.toggleExpanding(cell: self)
}
@objc private func toggleCollapseAccessibilityAction() -> Bool {
delegate?.toggleExpanding(cell: self)
return true
}
@objc private func handleSelectAction() -> Bool {
delegate?.toggleSelecting(cell: self)
return true
}
private func updateCollapseImage() {
let image = isExpanded ? chevronUp : chevronDown
collapseButton.setAccessibilityIdentifier(isExpanded ? .collapseButton : .expandButton)
collapseButton.setImage(image, for: .normal)
}
@objc private func toggleCheckboxButton(_ sender: UIControl) {
delegate?.toggleSelecting(cell: self)
}
override func accessibilityActivate() -> Bool {
toggleCollapseAccessibilityAction()
}
}
extension LocationCell {
enum LocationCellBehavior {
case add
case select
}
func configure(item: LocationCellViewModel, behavior: LocationCellBehavior) {
isDisabled = !item.node.isActive
locationLabel.text = item.node.name
isExpanded = item.node.showsChildren
collapseButton.isHidden = item.node.children.isEmpty
checkboxButton.setAccessibilityIdentifier(.customListLocationCheckmarkButton)
setupAccessibility(item)
for view in checkboxButton.subviews where view is CheckboxView {
let checkboxView = view as? CheckboxView
checkboxView?.isChecked = item.isSelected
}
if item.node is CustomListLocationNode {
setAccessibilityIdentifier(.customListLocationCell)
} else {
// Only custom list nodes have more than one location. Therefore checking first
// location here is fine.
if let location = item.node.locations.first {
let accessibilityId: AccessibilityIdentifier =
switch location {
case .country: .countryLocationCell
case .city: .cityLocationCell
case .hostname: .relayLocationCell
}
setAccessibilityIdentifier(accessibilityId)
}
}
setBehavior(behavior)
}
func setExcluded(relayTitle: String? = nil) {
updateDisabled(true)
if let relayTitle {
locationLabel.text! += " (\(relayTitle))"
}
}
private func setBehavior(_ newBehavior: LocationCellBehavior) {
self.behavior = newBehavior
updateLeadingImage()
}
}
|