summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/DeviceRowView.swift
blob: ac638f47abfe62db8787529cada9e5bc32728b8c (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
//
//  DeviceRowView.swift
//  MullvadVPN
//
//  Created by pronebird on 26/07/2022.
//  Copyright © 2022 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 = UIFont.systemFont(ofSize: 17)
        textLabel.textColor = .white
        return textLabel
    }()

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

    let removeButton: UIButton = {
        let image = UIImage(named: "IconClose")?
            .withTintColor(
                .white.withAlphaComponent(0.4),
                renderingMode: .alwaysOriginal
            )

        let button = UIButton(type: .custom)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setImage(image, for: .normal)
        button.accessibilityLabel = NSLocalizedString(
            "REMOVE_DEVICE_ACCESSIBILITY_LABEL",
            tableName: "DeviceManagement",
            value: "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)

        backgroundColor = .primaryColor
        layoutMargins = UIMetrics.rowViewLayoutMargins

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

        textLabel.text = viewModel.displayName

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

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

            removeButton.centerYAnchor.constraint(equalTo: textLabel.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)
    }
}