-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathLocationSectionHeaderView.swift
97 lines (81 loc) · 3.11 KB
/
LocationSectionHeaderView.swift
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
//
// LocationSectionHeaderView.swift
// MullvadVPN
//
// Created by Mojgan on 2024-01-25.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//
import Foundation
import UIKit
class LocationSectionHeaderView: UIView, UIContentView {
var configuration: UIContentConfiguration {
get {
actualConfiguration
} set {
guard let newConfiguration = newValue as? Configuration,
actualConfiguration != newConfiguration else { return }
actualConfiguration = newConfiguration
apply(configuration: newConfiguration)
}
}
private var actualConfiguration: Configuration
private let nameLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 1
label.textColor = .primaryTextColor
label.font = .systemFont(ofSize: 16, weight: .semibold)
return label
}()
private let actionButton: UIButton = {
let button = UIButton(type: .system)
button.setImage(UIImage(systemName: "ellipsis"), for: .normal)
button.tintColor = UIColor(white: 1, alpha: 0.6)
return button
}()
init(configuration: Configuration) {
self.actualConfiguration = configuration
super.init(frame: .zero)
applyAppearance()
addSubviews()
apply(configuration: configuration)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func addSubviews() {
addConstrainedSubviews([nameLabel, actionButton]) {
nameLabel.pinEdgesToSuperviewMargins(.all().excluding(.trailing))
actionButton.pinEdgesToSuperview(PinnableEdges([.trailing(8)]))
actionButton.heightAnchor.constraint(equalTo: heightAnchor)
actionButton.widthAnchor.constraint(equalTo: actionButton.heightAnchor)
actionButton.leadingAnchor.constraint(equalTo: nameLabel.trailingAnchor, constant: 16)
}
}
private func apply(configuration: Configuration) {
let isActionHidden = configuration.primaryAction == nil
nameLabel.text = configuration.name
actionButton.isHidden = isActionHidden
actionButton.accessibilityIdentifier = nil
actualConfiguration.primaryAction.flatMap { [weak self] action in
self?.actionButton.accessibilityIdentifier = .openCustomListsMenuButton
self?.actionButton.addAction(action, for: .touchUpInside)
}
}
private func applyAppearance() {
backgroundColor = .primaryColor
let leadingInset = UIMetrics.locationCellLayoutMargins.leading + 6
directionalLayoutMargins = NSDirectionalEdgeInsets(top: 8, leading: leadingInset, bottom: 8, trailing: 24)
}
}
extension LocationSectionHeaderView {
struct Configuration: UIContentConfiguration, Equatable {
let name: String
var primaryAction: UIAction?
func makeContentView() -> UIView & UIContentView {
LocationSectionHeaderView(configuration: self)
}
func updated(for state: UIConfigurationState) -> Configuration {
self
}
}
}