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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
//
// HeaderBarView.swift
// MullvadVPN
//
// Created by pronebird on 19/06/2020.
// Copyright © 2020 Mullvad VPN AB. All rights reserved.
//
import Foundation
import UIKit
class HeaderBarView: UIView {
private let brandNameImage = UIImage(named: "LogoText")?
.withTintColor(UIColor.HeaderBar.brandNameColor, renderingMode: .alwaysOriginal)
private let logoImageView = UIImageView(image: UIImage(named: "LogoIcon"))
private lazy var brandNameImageView: UIImageView = {
let imageView = UIImageView(image: brandNameImage)
imageView.contentMode = .scaleAspectFill
return imageView
}()
private let deviceInfoHolder: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.distribution = .fill
stackView.spacing = 16.0
return stackView
}()
private lazy var deviceName: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 14)
label.textColor = UIColor(white: 1.0, alpha: 0.8)
label.setContentHuggingPriority(.defaultHigh, for: .horizontal)
return label
}()
private lazy var timeLeft: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 14)
label.textColor = UIColor(white: 1.0, alpha: 0.8)
label.setContentHuggingPriority(.defaultLow, for: .horizontal)
return label
}()
private lazy var buttonContainer: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [accountButton, settingsButton])
stackView.spacing = UIMetrics.headerBarButtonSpacing
return stackView
}()
private let borderLayer: CALayer = {
let layer = CALayer()
layer.backgroundColor = UIColor.HeaderBar.dividerColor.cgColor
return layer
}()
let accountButton: IncreasedHitButton = {
let button = makeHeaderBarButton(with: UIImage(named: "IconAccount"))
button.accessibilityIdentifier = "AccountButton"
button.accessibilityLabel = NSLocalizedString(
"HEADER_BAR_ACCOUNT_BUTTON_ACCESSIBILITY_LABEL",
tableName: "HeaderBar",
value: "Account",
comment: ""
)
return button
}()
let settingsButton: IncreasedHitButton = {
let button = makeHeaderBarButton(with: UIImage(named: "IconSettings"))
button.accessibilityIdentifier = "SettingsButton"
button.accessibilityLabel = NSLocalizedString(
"HEADER_BAR_SETTINGS_BUTTON_ACCESSIBILITY_LABEL",
tableName: "HeaderBar",
value: "Settings",
comment: ""
)
return button
}()
class func makeHeaderBarButton(with image: UIImage?) -> IncreasedHitButton {
let buttonImage = image?.withTintColor(UIColor.HeaderBar.buttonColor, renderingMode: .alwaysOriginal)
let disabledButtonImage = image?.withTintColor(
UIColor.HeaderBar.disabledButtonColor,
renderingMode: .alwaysOriginal
)
let barButton = IncreasedHitButton(type: .system)
barButton.setImage(buttonImage, for: .normal)
barButton.setImage(disabledButtonImage, for: .disabled)
barButton.configureForAutoLayout()
return barButton
}
var showsDivider = false {
didSet {
if showsDivider {
layer.addSublayer(borderLayer)
} else {
borderLayer.removeFromSuperlayer()
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
directionalLayoutMargins = NSDirectionalEdgeInsets(
top: 0,
leading: UIMetrics.contentLayoutMargins.leading,
bottom: 0,
trailing: UIMetrics.contentLayoutMargins.trailing
)
accessibilityContainerType = .semanticGroup
let imageSize = brandNameImage?.size ?? .zero
let brandNameAspectRatio = imageSize.width / max(imageSize.height, 1)
[deviceName, timeLeft].forEach { deviceInfoHolder.addArrangedSubview($0) }
addConstrainedSubviews([logoImageView, brandNameImageView, buttonContainer, deviceInfoHolder]) {
logoImageView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor)
logoImageView.centerYAnchor.constraint(equalTo: brandNameImageView.centerYAnchor)
logoImageView.widthAnchor.constraint(equalToConstant: UIMetrics.headerBarLogoSize)
logoImageView.heightAnchor.constraint(equalTo: logoImageView.widthAnchor, multiplier: 1)
brandNameImageView.leadingAnchor.constraint(
equalToSystemSpacingAfter: logoImageView.trailingAnchor,
multiplier: 1
)
brandNameImageView.topAnchor.constraint(
equalTo: layoutMarginsGuide.topAnchor,
constant: UIMetrics.headerBarLogoSize * 0.5
)
brandNameImageView.widthAnchor.constraint(
equalTo: brandNameImageView.heightAnchor,
multiplier: brandNameAspectRatio
)
brandNameImageView.heightAnchor.constraint(equalToConstant: UIMetrics.headerBarBrandNameHeight)
buttonContainer.centerYAnchor.constraint(equalTo: brandNameImageView.centerYAnchor)
buttonContainer.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor)
deviceInfoHolder.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor)
deviceInfoHolder.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor)
deviceInfoHolder.topAnchor.constraint(equalToSystemSpacingBelow: logoImageView.bottomAnchor, multiplier: 1)
layoutMarginsGuide.bottomAnchor.constraint(
equalToSystemSpacingBelow: deviceInfoHolder.bottomAnchor,
multiplier: 1
)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
borderLayer.frame = CGRect(x: 0, y: frame.maxY - 1, width: frame.width, height: 1)
brandNameImageView.isHidden = shouldHideBrandName()
}
/// Returns `true` if container holding buttons intersects brand name.
private func shouldHideBrandName() -> Bool {
let buttonContainerRect = buttonContainer.convert(buttonContainer.bounds, to: nil)
let brandNameRect = brandNameImageView.convert(brandNameImageView.bounds, to: nil)
return brandNameRect.intersects(buttonContainerRect)
}
}
extension HeaderBarView {
func update(configuration: RootConfiguration) {
if let name = configuration.deviceName {
let formattedDeviceName = NSLocalizedString(
"DEVICE_NAME_HEADER_VIEW",
tableName: "Account",
value: "Device name: %@",
comment: ""
)
deviceName.text = .init(format: formattedDeviceName, name)
}
if let expiry = configuration.expiry {
let formattedTimeLeft = NSLocalizedString(
"TIME_LEFT_HEADER_VIEW",
tableName: "Account",
value: "Time left: %@",
comment: ""
)
timeLeft.text = .init(
format: formattedTimeLeft,
CustomDateComponentsFormatting.localizedString(
from: Date(),
to: expiry,
unitsStyle: .full
) ?? ""
)
}
deviceInfoHolder.arrangedSubviews.forEach { $0.isHidden = !configuration.showsDeviceInfo }
accountButton.isHidden = !configuration.showsAccountButton
}
}
|