|
| 1 | +// |
| 2 | +// AddLocationCell.swift |
| 3 | +// MullvadVPN |
| 4 | +// |
| 5 | +// Created by Mojgan on 2024-02-29. |
| 6 | +// Copyright © 2024 Mullvad VPN AB. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +protocol AddLocationCellDelegate: AnyObject { |
| 12 | + func toggleExpanding(cell: AddLocationCell) |
| 13 | + func toggleSelection(cell: AddLocationCell) |
| 14 | +} |
| 15 | + |
| 16 | +class AddLocationCell: UITableViewCell { |
| 17 | + weak var delegate: AddLocationCellDelegate? |
| 18 | + |
| 19 | + private let chevronDown = UIImage(resource: .iconChevronDown) |
| 20 | + private let chevronUp = UIImage(resource: .iconChevronUp) |
| 21 | + |
| 22 | + private let locationLabel: UILabel = { |
| 23 | + let label = UILabel() |
| 24 | + label.font = UIFont.systemFont(ofSize: 17) |
| 25 | + label.textColor = .white |
| 26 | + label.numberOfLines = .zero |
| 27 | + label.lineBreakStrategy = [] |
| 28 | + return label |
| 29 | + }() |
| 30 | + |
| 31 | + private let checkboxButton: UIButton = { |
| 32 | + let button = UIButton() |
| 33 | + button.setImage(UIImage(systemName: "checkmark.square.fill"), for: .selected) |
| 34 | + button.setImage(UIImage(systemName: "square"), for: .normal) |
| 35 | + button.tintColor = .white |
| 36 | + return button |
| 37 | + }() |
| 38 | + |
| 39 | + private let collapseButton: UIButton = { |
| 40 | + let button = UIButton(type: .custom) |
| 41 | + button.accessibilityIdentifier = .collapseButton |
| 42 | + button.isAccessibilityElement = false |
| 43 | + button.tintColor = .white |
| 44 | + return button |
| 45 | + }() |
| 46 | + |
| 47 | + var isExpanded = false { |
| 48 | + didSet { |
| 49 | + updateCollapseImage() |
| 50 | + updateAccessibilityCustomActions() |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + var showsCollapseControl = false { |
| 55 | + didSet { |
| 56 | + collapseButton.isHidden = !showsCollapseControl |
| 57 | + updateAccessibilityCustomActions() |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + override var indentationLevel: Int { |
| 62 | + didSet { |
| 63 | + updateBackgroundColor() |
| 64 | + setLayoutMargins() |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { |
| 69 | + super.init(style: style, reuseIdentifier: reuseIdentifier) |
| 70 | + setupCell() |
| 71 | + } |
| 72 | + |
| 73 | + required init?(coder: NSCoder) { |
| 74 | + fatalError("init(coder:) has not been implemented") |
| 75 | + } |
| 76 | + |
| 77 | + private func setLayoutMargins() { |
| 78 | + let indentation = CGFloat(indentationLevel) * indentationWidth |
| 79 | + |
| 80 | + var contentMargins = UIMetrics.locationCellLayoutMargins |
| 81 | + contentMargins.leading += indentation |
| 82 | + |
| 83 | + contentView.directionalLayoutMargins = contentMargins |
| 84 | + } |
| 85 | + |
| 86 | + private func setupCell() { |
| 87 | + indentationWidth = UIMetrics.TableView.cellIndentationWidth |
| 88 | + |
| 89 | + backgroundColor = .clear |
| 90 | + contentView.backgroundColor = .clear |
| 91 | + |
| 92 | + backgroundView = UIView() |
| 93 | + |
| 94 | + collapseButton.addTarget(self, action: #selector(handleCollapseButton(_:)), for: .touchUpInside) |
| 95 | + checkboxButton.addTarget(self, action: #selector(toggleCheckboxButton(_:)), for: .touchUpInside) |
| 96 | + |
| 97 | + contentView.addConstrainedSubviews([checkboxButton, locationLabel, collapseButton]) { |
| 98 | + checkboxButton.pinEdgeToSuperviewMargin(.leading(.zero)) |
| 99 | + checkboxButton.centerYAnchor.constraint(equalTo: contentView.centerYAnchor) |
| 100 | + checkboxButton.widthAnchor |
| 101 | + .constraint( |
| 102 | + equalToConstant: 44.0 |
| 103 | + ) |
| 104 | + checkboxButton.heightAnchor.constraint(equalTo: checkboxButton.widthAnchor, multiplier: 1, constant: 0) |
| 105 | + |
| 106 | + locationLabel.leadingAnchor.constraint( |
| 107 | + equalTo: checkboxButton.trailingAnchor, |
| 108 | + constant: 12 |
| 109 | + ) |
| 110 | + |
| 111 | + locationLabel.trailingAnchor.constraint(lessThanOrEqualTo: collapseButton.leadingAnchor) |
| 112 | + .withPriority(.defaultHigh) |
| 113 | + locationLabel.pinEdgesToSuperviewMargins(PinnableEdges([.top(.zero), .bottom(.zero)])) |
| 114 | + |
| 115 | + collapseButton.widthAnchor |
| 116 | + .constraint( |
| 117 | + equalToConstant: UIMetrics.contentLayoutMargins.leading + UIMetrics |
| 118 | + .contentLayoutMargins.trailing + 24.0 |
| 119 | + ) |
| 120 | + collapseButton.pinEdgesToSuperviewMargins(.all().excluding(.leading)) |
| 121 | + } |
| 122 | + |
| 123 | + updateCollapseImage() |
| 124 | + updateAccessibilityCustomActions() |
| 125 | + updateBackgroundColor() |
| 126 | + setLayoutMargins() |
| 127 | + } |
| 128 | + |
| 129 | + private func updateBackgroundColor() { |
| 130 | + backgroundView?.backgroundColor = backgroundColorForIdentationLevel() |
| 131 | + } |
| 132 | + |
| 133 | + private func backgroundColorForIdentationLevel() -> UIColor { |
| 134 | + switch indentationLevel { |
| 135 | + case 1: |
| 136 | + return UIColor.Cell.Background.indentationLevelOne |
| 137 | + case 2: |
| 138 | + return UIColor.Cell.Background.indentationLevelTwo |
| 139 | + case 3: |
| 140 | + return UIColor.Cell.Background.indentationLevelThree |
| 141 | + default: |
| 142 | + return UIColor.Cell.Background.normal |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private func updateCollapseImage() { |
| 147 | + let image = isExpanded ? chevronUp : chevronDown |
| 148 | + collapseButton.setImage(image, for: .normal) |
| 149 | + } |
| 150 | + |
| 151 | + private func updateAccessibilityCustomActions() { |
| 152 | + if showsCollapseControl { |
| 153 | + let actionName = isExpanded |
| 154 | + ? NSLocalizedString( |
| 155 | + "ADD_LOCATIONS_COLLAPSE_ACCESSIBILITY_ACTION", |
| 156 | + tableName: "AddLocationsLocation", |
| 157 | + value: "Collapse location", |
| 158 | + comment: "" |
| 159 | + ) |
| 160 | + : NSLocalizedString( |
| 161 | + "ADD_LOCATIONS_EXPAND_ACCESSIBILITY_ACTION", |
| 162 | + tableName: "AddLocationsLocation", |
| 163 | + value: "Expand location", |
| 164 | + comment: "" |
| 165 | + ) |
| 166 | + |
| 167 | + accessibilityCustomActions = [ |
| 168 | + UIAccessibilityCustomAction( |
| 169 | + name: actionName, |
| 170 | + target: self, |
| 171 | + selector: #selector(toggleCollapseAccessibilityAction) |
| 172 | + ), |
| 173 | + ] |
| 174 | + } else { |
| 175 | + accessibilityCustomActions = nil |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + // MARK: - actions |
| 180 | + |
| 181 | + @objc private func handleCollapseButton(_ sender: UIControl) { |
| 182 | + delegate?.toggleExpanding(cell: self) |
| 183 | + } |
| 184 | + |
| 185 | + @objc private func toggleCollapseAccessibilityAction() -> Bool { |
| 186 | + delegate?.toggleExpanding(cell: self) |
| 187 | + return true |
| 188 | + } |
| 189 | + |
| 190 | + @objc private func toggleCheckboxButton(_ sender: UIControl) { |
| 191 | + delegate?.toggleSelection(cell: self) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +extension AddLocationCell { |
| 196 | + func configure(item: AddLocationCellViewModel) { |
| 197 | + accessibilityIdentifier = item.node.name |
| 198 | + locationLabel.text = item.node.name |
| 199 | + showsCollapseControl = !item.node.children.isEmpty |
| 200 | + isExpanded = item.node.showsChildren |
| 201 | + checkboxButton.isSelected = item.isSelected |
| 202 | + checkboxButton.tintColor = item.isSelected ? .successColor : .white |
| 203 | + } |
| 204 | +} |
0 commit comments