summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/View controllers/Settings/SettingsHeaderView.swift
blob: d145d5a4041899645cc66b94a02d9d8179fbe9dc (plain)
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
//
//  SettingsHeaderView.swift
//  MullvadVPN
//
//  Created by Jon Petersson on 2023-04-06.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import UIKit

class SettingsHeaderView: UITableViewHeaderFooterView {
    typealias InfoButtonHandler = () -> Void
    typealias CollapseHandler = (SettingsHeaderView) -> Void

    let titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        titleLabel.font = .mullvadSmallSemiBold
        titleLabel.adjustsFontForContentSizeCategory = true
        titleLabel.textColor = UIColor.Cell.titleTextColor
        titleLabel.numberOfLines = 0
        return titleLabel
    }()

    let infoButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setAccessibilityIdentifier(.infoButton)
        button.adjustsImageSizeForAccessibilityContentSizeCategory = true
        button.tintColor = .white
        button.setImage(UIImage.Buttons.info, for: .normal)
        return button
    }()

    let collapseButton: UIButton = {
        let button = UIButton(type: .custom)
        button.adjustsImageSizeForAccessibilityContentSizeCategory = true
        button.setAccessibilityIdentifier(.expandButton)
        button.tintColor = .white
        return button
    }()

    var isExpanded = false {
        didSet {
            updateCollapseImage()
            updateAccessibilityCustomActions()
        }
    }

    var accessibilityCustomActionName = "" {
        didSet {
            updateAccessibilityCustomActions()
        }
    }

    var didCollapseHandler: CollapseHandler? {
        didSet {
            collapseButton.isHidden = didCollapseHandler == nil
        }
    }

    var infoButtonHandler: InfoButtonHandler? {
        didSet {
            infoButton.isHidden = infoButtonHandler == nil
        }
    }

    private let chevronDown = UIImage(named: "IconChevronDown")
    private let chevronUp = UIImage(named: "IconChevronUp")
    private let buttonWidth: CGFloat = 24

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)

        infoButton.isHidden = true
        infoButton.addTarget(
            self,
            action: #selector(handleInfoButton(_:)),
            for: .touchUpInside
        )

        collapseButton.isHidden = true
        collapseButton.addTarget(
            self,
            action: #selector(handleCollapseButton(_:)),
            for: .touchUpInside
        )

        contentView.directionalLayoutMargins = UIMetrics.SettingsCell.defaultLayoutMargins
        contentView.backgroundColor = UIColor.Cell.Background.normal

        let buttonAreaWidth =
            UIMetrics.contentLayoutMargins.leading
            + UIMetrics
            .contentLayoutMargins.trailing + buttonWidth

        contentView.addConstrainedSubviews([titleLabel, infoButton, collapseButton]) {
            titleLabel.pinEdgesToSuperview(.init([.top(contentView.layoutMargins.top), .leading(16)]))
            titleLabel.bottomAnchor.constraint(
                equalTo: contentView.bottomAnchor,
                constant: -contentView.layoutMargins.bottom
            ).withPriority(.defaultHigh)

            infoButton.pinEdgesToSuperview(.init([.top(0), .bottom(0)]))
            titleLabel.trailingAnchor.constraint(
                equalTo: infoButton.leadingAnchor,
                constant: UIMetrics.TableView.infoButtonSpacing
            )
            infoButton.widthAnchor.constraint(equalToConstant: buttonAreaWidth)

            collapseButton.pinEdgesToSuperview(.all().excluding(.leading))
            collapseButton.leadingAnchor.constraint(greaterThanOrEqualTo: infoButton.trailingAnchor)
            collapseButton.widthAnchor.constraint(equalToConstant: buttonAreaWidth)
        }

        updateCollapseImage()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc private func handleInfoButton(_ sender: UIControl) {
        infoButtonHandler?()
    }

    @objc private func handleCollapseButton(_ sender: UIControl) {
        didCollapseHandler?(self)
    }

    @objc private func toggleCollapseAccessibilityAction() -> Bool {
        didCollapseHandler?(self)
        return true
    }

    private func updateCollapseImage() {
        let image = isExpanded ? chevronUp : chevronDown

        collapseButton.setImage(image, for: .normal)
        collapseButton.setAccessibilityIdentifier(isExpanded ? .collapseButton : .expandButton)
    }

    private func updateAccessibilityCustomActions() {
        let actionName =
            isExpanded
            ? String(format: NSLocalizedString("Collapse %@", comment: ""), accessibilityCustomActionName)
            : String(format: NSLocalizedString("Expand %@", comment: ""), accessibilityCustomActionName)

        accessibilityCustomActions = [
            UIAccessibilityCustomAction(
                name: actionName,
                target: self,
                selector: #selector(toggleCollapseAccessibilityAction)
            )
        ]
    }
}