Skip to content

Commit 77ade46

Browse files
improve code in UKCircularProgress
1 parent bbd4e8c commit 77ade46

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

Sources/ComponentsKit/Components/CircularProgress/Models/CircularProgressVM.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,25 @@ extension CircularProgressVM {
160160
}
161161
}
162162
func stripesBezierPath(in rect: CGRect) -> UIBezierPath {
163-
return UIBezierPath(cgPath: self.stripesCGPath(in: rect))
163+
let center = CGPoint(x: rect.midX, y: rect.midY)
164+
let path = UIBezierPath(cgPath: self.stripesCGPath(in: rect))
165+
var transform = CGAffineTransform.identity
166+
transform = transform
167+
.translatedBy(x: center.x, y: center.y)
168+
.rotated(by: -CGFloat.pi / 2)
169+
.translatedBy(x: -center.x, y: -center.y)
170+
path.apply(transform)
171+
return path
172+
}
173+
func shouldInvalidateIntrinsicContentSize(_ oldModel: Self) -> Bool {
174+
return self.preferredSize != oldModel.preferredSize
175+
}
176+
func shouldUpdateText(_ oldModel: Self) -> Bool {
177+
return self.label != oldModel.label
164178
}
165-
func shouldInvalidateIntrinsicContentSize(_ oldValue: Self) -> Bool {
166-
return self.preferredSize != oldValue.preferredSize
179+
func shouldRecalculateProgress(_ oldModel: Self) -> Bool {
180+
return self.minValue != oldModel.minValue
181+
|| self.maxValue != oldModel.maxValue
167182
}
168183
}
169184

Sources/ComponentsKit/Components/CircularProgress/UKCircularProgress.swift

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,13 @@ open class UKCircularProgress: UIView, UKComponent {
8181
}
8282
}
8383

84-
CATransaction.begin()
85-
CATransaction.setDisableActions(true)
86-
8784
let progress = self.model.progress(for: self.currentValue)
8885
self.progressLayer.strokeEnd = progress
8986
if !self.model.isStripesLayerHidden {
9087
self.stripesMaskLayer.strokeStart = self.model.stripedArcStart(for: progress)
9188
self.stripesMaskLayer.strokeEnd = self.model.stripedArcEnd(for: progress)
9289
}
9390
self.label.text = self.model.label
94-
95-
CATransaction.commit()
9691
}
9792

9893
// MARK: - Style
@@ -102,6 +97,7 @@ open class UKCircularProgress: UIView, UKComponent {
10297
Self.Style.progressLayer(self.progressLayer, model: self.model)
10398
Self.Style.label(self.label, model: self.model)
10499
Self.Style.stripesLayer(self.stripesLayer, model: self.model)
100+
Self.Style.stripesMaskLayer(self.stripesMaskLayer, model: self.model)
105101
}
106102

107103
// MARK: - Update
@@ -110,8 +106,21 @@ open class UKCircularProgress: UIView, UKComponent {
110106
guard self.model != oldModel else { return }
111107
self.style()
112108
self.updateShapePaths()
113-
self.updateProgress()
114109

110+
if self.model.shouldUpdateText(oldModel) {
111+
UIView.transition(
112+
with: self.label,
113+
duration: self.model.animationDuration,
114+
options: .transitionCrossDissolve,
115+
animations: {
116+
self.label.text = self.model.label
117+
},
118+
completion: nil
119+
)
120+
}
121+
if self.model.shouldRecalculateProgress(oldModel) {
122+
self.updateProgress()
123+
}
115124
if self.model.shouldInvalidateIntrinsicContentSize(oldModel) {
116125
self.invalidateIntrinsicContentSize()
117126
}
@@ -130,15 +139,7 @@ open class UKCircularProgress: UIView, UKComponent {
130139
self.backgroundLayer.path = circlePath.cgPath
131140
self.progressLayer.path = circlePath.cgPath
132141
self.stripesMaskLayer.path = circlePath.cgPath
133-
134-
let stripesPath = self.model.stripesBezierPath(in: self.bounds)
135-
var transform = CGAffineTransform.identity
136-
transform = transform
137-
.translatedBy(x: center.x, y: center.y)
138-
.rotated(by: -CGFloat.pi / 2)
139-
.translatedBy(x: -center.x, y: -center.y)
140-
stripesPath.apply(transform)
141-
self.stripesLayer.path = stripesPath.cgPath
142+
self.stripesLayer.path = self.model.stripesBezierPath(in: self.bounds).cgPath
142143
}
143144

144145
private func updateProgress() {
@@ -153,20 +154,6 @@ open class UKCircularProgress: UIView, UKComponent {
153154
self.stripesMaskLayer.strokeEnd = self.model.stripedArcEnd(for: progress)
154155
}
155156
CATransaction.commit()
156-
157-
if let labelText = self.model.label {
158-
UIView.transition(
159-
with: self.label,
160-
duration: self.model.animationDuration,
161-
options: .transitionCrossDissolve,
162-
animations: {
163-
self.label.text = labelText
164-
},
165-
completion: nil
166-
)
167-
} else {
168-
self.label.text = nil
169-
}
170157
}
171158

172159
// MARK: - Layout
@@ -202,9 +189,6 @@ open class UKCircularProgress: UIView, UKComponent {
202189
}
203190

204191
private func handleTraitChanges() {
205-
Self.Style.backgroundLayer(self.backgroundLayer, model: self.model)
206-
Self.Style.progressLayer(self.progressLayer, model: self.model)
207-
Self.Style.label(self.label, model: self.model)
208192
Self.Style.backgroundLayer(self.backgroundLayer, model: self.model)
209193
Self.Style.progressLayer(self.progressLayer, model: self.model)
210194
Self.Style.stripesLayer(self.stripesLayer, model: self.model)
@@ -227,14 +211,20 @@ extension UKCircularProgress {
227211
layer.isHidden = model.isBackgroundLayerHidden
228212
}
229213

230-
static func progressLayer(_ layer: CAShapeLayer, model: CircularProgressVM) {
214+
static func progressLayer(
215+
_ layer: CAShapeLayer,
216+
model: CircularProgressVM
217+
) {
231218
layer.fillColor = UIColor.clear.cgColor
232219
layer.strokeColor = model.color.main.uiColor.cgColor
233220
layer.lineCap = .round
234221
layer.lineWidth = model.circularLineWidth
235222
}
236223

237-
static func label(_ label: UILabel, model: CircularProgressVM) {
224+
static func label(
225+
_ label: UILabel,
226+
model: CircularProgressVM
227+
) {
238228
label.textAlignment = .center
239229
label.adjustsFontSizeToFitWidth = true
240230
label.minimumScaleFactor = 0.5

0 commit comments

Comments
 (0)