-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathButtonCellContentView.swift
78 lines (61 loc) · 2.54 KB
/
ButtonCellContentView.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
//
// ButtonCellContentView.swift
// MullvadVPN
//
// Created by pronebird on 17/11/2023.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import UIKit
/// Content view presenting a full-width button.
class ButtonCellContentView: UIView, UIContentView {
private let button = AppButton()
/// Default cell corner radius in inset grouped table view
private let tableViewCellCornerRadius: CGFloat = 10
var configuration: UIContentConfiguration {
get {
actualConfiguration
}
set {
guard let newConfiguration = newValue as? ButtonCellContentConfiguration,
actualConfiguration != newConfiguration else { return }
let previousConfiguration = actualConfiguration
actualConfiguration = newConfiguration
configureSubviews(previousConfiguration: previousConfiguration)
}
}
private var actualConfiguration: ButtonCellContentConfiguration
func supports(_ configuration: UIContentConfiguration) -> Bool {
configuration is ButtonCellContentConfiguration
}
init(configuration: ButtonCellContentConfiguration) {
actualConfiguration = configuration
super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 44))
configureSubviews()
addSubviews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configureSubviews(previousConfiguration: ButtonCellContentConfiguration? = nil) {
guard actualConfiguration != previousConfiguration else { return }
configureButton()
configureActions(previousConfiguration: previousConfiguration)
}
private func configureActions(previousConfiguration: ButtonCellContentConfiguration? = nil) {
previousConfiguration?.primaryAction.map { button.removeAction($0, for: .touchUpInside) }
actualConfiguration.primaryAction.map { button.addAction($0, for: .touchUpInside) }
}
private func configureButton() {
button.setTitle(actualConfiguration.text, for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 17)
button.isEnabled = actualConfiguration.isEnabled
button.style = actualConfiguration.style
button.configuration?.contentInsets = actualConfiguration.directionalContentEdgeInsets
button.setAccessibilityIdentifier(actualConfiguration.accessibilityIdentifier)
}
private func addSubviews() {
addConstrainedSubviews([button]) {
button.pinEdgesToSuperview()
}
}
}