summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/View controllers/DeviceList/DeviceRowView.swift
blob: 70ee018d4b6a16c4f5f55baeace9c2454dde1443 (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
//
//  DeviceRowView.swift
//  MullvadVPN
//
//  Created by pronebird on 26/07/2022.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import UIKit

class DeviceRowView: UIView {
    let viewModel: DeviceViewModel
    var deleteHandler: ((DeviceRowView) -> Void)?

    let textLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        textLabel.font = .mullvadSmallSemiBold
        textLabel.adjustsFontForContentSizeCategory = true
        textLabel.textColor = .white
        return textLabel
    }()

    let activityIndicator: SpinnerActivityIndicatorView = {
        let activityIndicator = SpinnerActivityIndicatorView(style: .custom)
        activityIndicator.translatesAutoresizingMaskIntoConstraints = false
        return activityIndicator
    }()

    let creationDateLabel: UILabel = {
        let creationDateLabel = UILabel()
        creationDateLabel.translatesAutoresizingMaskIntoConstraints = false
        creationDateLabel.font = .mullvadMiniSemiBold
        creationDateLabel.adjustsFontForContentSizeCategory = true
        creationDateLabel.textColor = .white.withAlphaComponent(0.6)
        return creationDateLabel
    }()

    let removeButton: UIButton = {
        let image = UIImage.Buttons.close
            .withTintColor(
                .white.withAlphaComponent(0.4),
                renderingMode: .alwaysOriginal
            )

        let button = IncreasedHitButton(type: .custom)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setImage(image, for: .normal)
        button.accessibilityLabel = NSLocalizedString("Remove device", comment: "")
        return button
    }()

    var showsActivityIndicator = false {
        didSet {
            removeButton.isHidden = showsActivityIndicator

            if showsActivityIndicator {
                activityIndicator.startAnimating()
            } else {
                activityIndicator.stopAnimating()
            }
        }
    }

    init(viewModel: DeviceViewModel) {
        self.viewModel = viewModel

        super.init(frame: .zero)

        setAccessibilityIdentifier(.deviceCell)
        backgroundColor = .primaryColor
        directionalLayoutMargins = UIMetrics.TableView.rowViewLayoutMargins

        for subview in [textLabel, removeButton, activityIndicator, creationDateLabel] {
            addSubview(subview)
        }

        textLabel.text = viewModel.name
        creationDateLabel.text = .init(
            format:
                NSLocalizedString("Created: %@", comment: ""),
            viewModel.creationDate
        )

        removeButton.addTarget(self, action: #selector(handleButtonTap(_:)), for: .touchUpInside)
        removeButton.setAccessibilityIdentifier(.deviceCellRemoveButton)

        NSLayoutConstraint.activate([
            textLabel.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
            textLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),

            creationDateLabel.leadingAnchor.constraint(equalTo: textLabel.leadingAnchor),
            creationDateLabel.topAnchor.constraint(equalTo: textLabel.bottomAnchor, constant: 4.0),
            creationDateLabel.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
                .withPriority(.defaultLow),
            creationDateLabel.trailingAnchor.constraint(equalTo: textLabel.trailingAnchor),

            removeButton.centerYAnchor.constraint(equalTo: layoutMarginsGuide.centerYAnchor),
            removeButton.leadingAnchor.constraint(
                greaterThanOrEqualTo: textLabel.trailingAnchor,
                constant: 8
            ),
            removeButton.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),

            activityIndicator.centerXAnchor.constraint(equalTo: removeButton.centerXAnchor),
            activityIndicator.centerYAnchor.constraint(equalTo: removeButton.centerYAnchor),

            // Bump dimensions by 6pt to account for transparent pixels around spinner image.
            activityIndicator.widthAnchor.constraint(
                equalTo: removeButton.widthAnchor,
                constant: 6
            ),
            activityIndicator.heightAnchor.constraint(
                equalTo: removeButton.heightAnchor,
                constant: 6
            ),
        ])
    }

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

    @objc private func handleButtonTap(_ sender: Any?) {
        deleteHandler?(self)
    }
}