summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/View controllers/Account/AccountDeviceRow.swift
blob: c1429c9a0bece1b79c31a55669d9438d663366e6 (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
//
//  AccountDeviceRow.swift
//  MullvadVPN
//
//  Created by Mojgan on 2023-08-28.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import UIKit

class AccountDeviceRow: UIView {
    var deviceName: String? {
        didSet {
            deviceLabel.text = deviceName?.capitalized ?? ""
            accessibilityValue = deviceName
            setAccessibilityIdentifier(.accountPageDeviceNameLabel)
        }
    }

    var deviceManagementButtonAction: (() -> Void)?

    private let titleLabel: UILabel = {
        let label = UILabel()
        label.text = NSLocalizedString("Device name", comment: "")
        label.font = .mullvadTiny
        label.adjustsFontForContentSizeCategory = true
        label.textColor = UIColor(white: 1.0, alpha: 0.6)
        return label
    }()

    private let deviceLabel: UILabel = {
        let label = UILabel()
        label.font = .mullvadSmall
        label.adjustsFontForContentSizeCategory = true
        label.textColor = .white
        label.numberOfLines = 0
        return label
    }()

    private let deviceManagementButton: UILabel = {
        let button = UILabel()
        button.adjustsFontForContentSizeCategory = true
        button.isUserInteractionEnabled = true
        button.numberOfLines = 0
        button.textAlignment = .center

        let attributes: [NSAttributedString.Key: Any] = [
            .font: UIFont.mullvadSmallSemiBold,
            .foregroundColor: UIColor.primaryTextColor,
            .underlineStyle: NSUnderlineStyle.single.rawValue,
        ]
        let title = NSLocalizedString("Manage devices", comment: "")
        button.attributedText = NSMutableAttributedString(
            string: title,
            attributes: attributes
        )
        button.setAccessibilityIdentifier(.deviceManagementButton)
        return button
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        let contentContainerView = UIStackView(arrangedSubviews: [titleLabel, deviceLabel])
        contentContainerView.axis = .vertical
        contentContainerView.alignment = .leading
        contentContainerView.spacing = 8

        contentContainerView.setContentCompressionResistancePriority(.required, for: .horizontal)
        contentContainerView.setContentHuggingPriority(.defaultHigh, for: .horizontal)

        deviceManagementButton.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        deviceManagementButton.setContentHuggingPriority(.defaultHigh, for: .horizontal)

        addConstrainedSubviews(
            [contentContainerView, deviceManagementButton]
        ) {
            contentContainerView.pinEdgesToSuperview(PinnableEdges([.leading(0), .bottom(0), .top(0)]))
            deviceManagementButton.topAnchor.constraint(equalTo: deviceLabel.topAnchor)
            deviceManagementButton.pinEdgesToSuperview(PinnableEdges([.trailing(0), .bottom(0)]))
            deviceManagementButton.leadingAnchor.constraint(equalTo: contentContainerView.trailingAnchor, constant: 16)
        }

        isAccessibilityElement = true
        accessibilityLabel = titleLabel.text

        deviceManagementButton.addGestureRecognizer(
            UITapGestureRecognizer(
                target: self,
                action: #selector(didTapDeviceManagementButton)
            ))
    }

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

    func setButtons(enabled: Bool) {
        deviceManagementButton.isEnabled = enabled
    }

    @objc private func didTapDeviceManagementButton() {
        deviceManagementButtonAction?()
    }
}