Skip to content

Commit 3baf924

Browse files
committed
pr fix
- layout fix and added shouldUpdateLayout - handleTraitChages deleted - added height for corner radius
1 parent b7b8f21 commit 3baf924

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

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+
}

Sources/ComponentsKit/Components/Badge/UKBadge.swift

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ open class UKBadge: UIView, UKComponent {
1414

1515
private var titleLabelConstraints: LayoutConstraints = .init()
1616

17-
// MARK: Subviews
17+
// MARK: - Subviews
1818

1919
/// A label that displays the title from the model.
2020
public var titleLabel = UILabel()
@@ -49,7 +49,6 @@ open class UKBadge: UIView, UKComponent {
4949

5050
if #available(iOS 17.0, *) {
5151
self.registerForTraitChanges([UITraitUserInterfaceStyle.self]) { (view: Self, _: UITraitCollection) in
52-
view.handleTraitChanges()
5352
}
5453
}
5554
}
@@ -64,17 +63,20 @@ open class UKBadge: UIView, UKComponent {
6463
// MARK: - Layout
6564

6665
private func layout() {
67-
self.titleLabelConstraints = self.titleLabel.horizontally(self.model.horizontalPadding)
68-
self.titleLabel.center()
69-
70-
self.titleLabelConstraints.leading?.priority = .defaultHigh
71-
self.titleLabelConstraints.trailing?.priority = .defaultHigh
66+
self.titleLabelConstraints = .merged {
67+
self.titleLabel.top(self.model.paddings.top)
68+
self.titleLabel.leading(self.model.paddings.leading)
69+
self.titleLabel.bottom(self.model.paddings.bottom)
70+
self.titleLabel.trailing(self.model.paddings.trailing)
71+
}
72+
73+
self.titleLabelConstraints.allConstraints.forEach { $0?.priority = .defaultHigh }
7274
}
7375

7476
open override func layoutSubviews() {
7577
super.layoutSubviews()
7678

77-
self.layer.cornerRadius = self.model.cornerRadius.value()
79+
self.layer.cornerRadius = self.model.cornerRadius.value(for: self.bounds.height)
7880
}
7981

8082
// MARK: - Update
@@ -83,20 +85,27 @@ open class UKBadge: UIView, UKComponent {
8385
guard self.model != oldModel else { return }
8486

8587
self.style()
86-
self.titleLabelConstraints.leading?.constant = self.model.horizontalPadding
87-
self.titleLabelConstraints.trailing?.constant = -self.model.horizontalPadding
88-
89-
self.invalidateIntrinsicContentSize()
90-
self.setNeedsLayout()
88+
if self.model.shouldUpdateLayout(oldModel) {
89+
self.titleLabelConstraints.leading?.constant = self.model.paddings.leading
90+
self.titleLabelConstraints.top?.constant = self.model.paddings.top
91+
self.titleLabelConstraints.bottom?.constant = -self.model.paddings.bottom
92+
self.titleLabelConstraints.trailing?.constant = -self.model.paddings.trailing
93+
94+
self.invalidateIntrinsicContentSize()
95+
self.setNeedsLayout()
96+
}
9197
}
9298

9399
// MARK: - UIView Methods
94100

95101
open override func sizeThatFits(_ size: CGSize) -> CGSize {
96102
let contentSize = self.titleLabel.sizeThatFits(size)
97103

98-
let width = contentSize.width + self.model.horizontalPadding * 2
99-
let height = contentSize.height + self.model.verticalPadding * 2
104+
let totalWidthPadding = self.model.paddings.leading + self.model.paddings.trailing
105+
let totalHeightPadding = self.model.paddings.top + self.model.paddings.bottom
106+
107+
let width = contentSize.width + totalWidthPadding
108+
let height = contentSize.height + totalHeightPadding
100109

101110
return CGSize(
102111
width: min(width, size.width),
@@ -106,13 +115,6 @@ open class UKBadge: UIView, UKComponent {
106115

107116
open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
108117
super.traitCollectionDidChange(previousTraitCollection)
109-
self.handleTraitChanges()
110-
}
111-
112-
// MARK: - Helpers
113-
114-
@objc private func handleTraitChanges() {
115-
self.backgroundColor = self.model.backgroundColor?.uiColor
116118
}
117119
}
118120

@@ -121,13 +123,13 @@ open class UKBadge: UIView, UKComponent {
121123
extension UKBadge {
122124
fileprivate enum Style {
123125
static func mainView(_ view: UIView, model: BadgeVM) {
124-
view.backgroundColor = model.backgroundColor?.uiColor
125-
view.layer.cornerRadius = model.cornerRadius.value()
126+
view.backgroundColor = model.backgroundColor.uiColor
127+
view.layer.cornerRadius = model.cornerRadius.value(for: view.bounds.height)
126128
}
127129
static func titleLabel(_ label: UILabel, model: BadgeVM) {
128130
label.textAlignment = .center
129131
label.text = model.title
130-
label.font = model.preferredFont.uiFont
132+
label.font = model.font.uiFont
131133
label.textColor = model.foregroundColor.uiColor
132134
}
133135
}

0 commit comments

Comments
 (0)