Skip to content

Commit 4d66c7f

Browse files
Merge pull request #57 from componentskit/UKBadge
UKBadge
2 parents fe0a5bc + d53eef5 commit 4d66c7f

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/BadgePreview.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ struct BadgePreview: View {
99

1010
var body: some View {
1111
VStack {
12+
PreviewWrapper(title: "UIKit") {
13+
UKBadge(model: self.model)
14+
.preview
15+
}
1216
PreviewWrapper(title: "SwiftUI") {
1317
SUBadge(model: self.model)
1418
}
@@ -28,6 +32,14 @@ struct BadgePreview: View {
2832
Text("Filled").tag(BadgeVM.Style.filled)
2933
Text("Light").tag(BadgeVM.Style.light)
3034
}
35+
Picker("Paddings", selection: self.$model.paddings) {
36+
Text("8px; 6px")
37+
.tag(Paddings(top: 6, leading: 8, bottom: 6, trailing: 8))
38+
Text("10px; 8px")
39+
.tag(Paddings(top: 8, leading: 10, bottom: 8, trailing: 10))
40+
Text("12px; 10px")
41+
.tag(Paddings(top: 10, leading: 12, bottom: 10, trailing: 12))
42+
}
3143
}
3244
}
3345
}

Sources/ComponentsKit/Components/Badge/Models/BadgeVM.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,12 @@ extension BadgeVM {
5353
}
5454
}
5555
}
56+
57+
// MARK: UIKit Helpers
58+
59+
extension BadgeVM {
60+
func shouldUpdateLayout(_ oldModel: Self?) -> Bool {
61+
return self.font != oldModel?.font
62+
|| self.paddings != oldModel?.paddings
63+
}
64+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import AutoLayout
2+
import UIKit
3+
4+
/// A UIKit component that displays a badge.
5+
open class UKBadge: UIView, UKComponent {
6+
// MARK: - Properties
7+
8+
/// A model that defines the appearance properties.
9+
public var model: BadgeVM {
10+
didSet {
11+
self.update(oldValue)
12+
}
13+
}
14+
15+
private var titleLabelConstraints: LayoutConstraints = .init()
16+
17+
// MARK: - Subviews
18+
19+
/// A label that displays the title from the model.
20+
public var titleLabel = UILabel()
21+
22+
// MARK: - UIView Properties
23+
24+
open override var intrinsicContentSize: CGSize {
25+
return self.sizeThatFits(UIView.layoutFittingExpandedSize)
26+
}
27+
28+
// MARK: - Initialization
29+
30+
/// Initializes a new instance of `UKBadge`.
31+
/// - Parameter model: A model that defines the appearance properties for the badge.
32+
public init(model: BadgeVM = .init()) {
33+
self.model = model
34+
super.init(frame: .zero)
35+
36+
self.setup()
37+
self.style()
38+
self.layout()
39+
}
40+
41+
public required init?(coder: NSCoder) {
42+
fatalError("init(coder:) has not been implemented")
43+
}
44+
45+
// MARK: - Setup
46+
47+
private func setup() {
48+
self.addSubview(self.titleLabel)
49+
}
50+
51+
// MARK: - Style
52+
53+
private func style() {
54+
Self.Style.mainView(self, model: self.model)
55+
Self.Style.titleLabel(self.titleLabel, model: self.model)
56+
}
57+
58+
// MARK: - Layout
59+
60+
private func layout() {
61+
self.titleLabelConstraints = .merged {
62+
self.titleLabel.top(self.model.paddings.top)
63+
self.titleLabel.leading(self.model.paddings.leading)
64+
self.titleLabel.bottom(self.model.paddings.bottom)
65+
self.titleLabel.trailing(self.model.paddings.trailing)
66+
}
67+
68+
self.titleLabelConstraints.allConstraints.forEach { $0?.priority = .defaultHigh }
69+
}
70+
71+
open override func layoutSubviews() {
72+
super.layoutSubviews()
73+
74+
self.layer.cornerRadius = self.model.cornerRadius.value(for: self.bounds.height)
75+
}
76+
77+
// MARK: - Update
78+
79+
public func update(_ oldModel: BadgeVM) {
80+
guard self.model != oldModel else { return }
81+
82+
self.style()
83+
if self.model.shouldUpdateLayout(oldModel) {
84+
self.titleLabelConstraints.leading?.constant = self.model.paddings.leading
85+
self.titleLabelConstraints.top?.constant = self.model.paddings.top
86+
self.titleLabelConstraints.bottom?.constant = -self.model.paddings.bottom
87+
self.titleLabelConstraints.trailing?.constant = -self.model.paddings.trailing
88+
89+
self.invalidateIntrinsicContentSize()
90+
self.setNeedsLayout()
91+
}
92+
}
93+
94+
// MARK: - UIView Methods
95+
96+
open override func sizeThatFits(_ size: CGSize) -> CGSize {
97+
let contentSize = self.titleLabel.sizeThatFits(size)
98+
99+
let totalWidthPadding = self.model.paddings.leading + self.model.paddings.trailing
100+
let totalHeightPadding = self.model.paddings.top + self.model.paddings.bottom
101+
102+
let width = contentSize.width + totalWidthPadding
103+
let height = contentSize.height + totalHeightPadding
104+
105+
return CGSize(
106+
width: min(width, size.width),
107+
height: min(height, size.height)
108+
)
109+
}
110+
}
111+
112+
// MARK: - Style Helpers
113+
114+
extension UKBadge {
115+
fileprivate enum Style {
116+
static func mainView(_ view: UIView, model: BadgeVM) {
117+
view.backgroundColor = model.backgroundColor.uiColor
118+
view.layer.cornerRadius = model.cornerRadius.value(for: view.bounds.height)
119+
}
120+
static func titleLabel(_ label: UILabel, model: BadgeVM) {
121+
label.textAlignment = .center
122+
label.text = model.title
123+
label.font = model.font.uiFont
124+
label.textColor = model.foregroundColor.uiColor
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)