Skip to content

Commit 7fb95c7

Browse files
refactor: move a method for calculating time width to the model
1 parent a05fe2c commit 7fb95c7

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

Sources/ComponentsKit/Countdown/Models/CountdownVM.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,30 @@ extension CountdownVM {
185185
return result
186186
}
187187
}
188+
}
188189

190+
extension CountdownVM {
189191
func shouldRecalculateWidth(_ oldModel: Self) -> Bool {
190192
return self.unitsPosition != oldModel.unitsPosition
191193
|| self.style != oldModel.style
192194
|| self.preferredFont != oldModel.preferredFont
193195
|| self.size != oldModel.size
196+
|| self.locale != oldModel.locale
197+
}
198+
199+
func timeWidth(manager: CountdownManager) -> CGFloat {
200+
let values: [(Int, CountdownHelpers.Unit)] = [
201+
(manager.days, .days),
202+
(manager.hours, .hours),
203+
(manager.minutes, .minutes),
204+
(manager.seconds, .seconds)
205+
]
206+
207+
let widths = values.map { value, unit -> CGFloat in
208+
let attributedString = self.timeText(value: value, unit: unit)
209+
return CountdownWidthCalculator.preferredWidth(for: attributedString, model: self)
210+
}
211+
212+
return (widths.max() ?? self.defaultMinWidth) + self.horizontalPadding * 2
194213
}
195214
}

Sources/ComponentsKit/Countdown/SUCountdown.swift

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,24 @@ public struct SUCountdown: View {
5555
}
5656
.onAppear {
5757
self.manager.start(until: self.model.until)
58-
self.calculateWidth(model: self.model)
58+
self.timeWidth = self.model.timeWidth(manager: self.manager)
5959
}
6060
.onChange(of: self.model.until) { newDate in
6161
self.manager.stop()
6262
self.manager.start(until: newDate)
6363
}
6464
.onChange(of: self.model) { newValue in
6565
if newValue.shouldRecalculateWidth(self.model) {
66-
self.calculateWidth(model: newValue)
66+
self.timeWidth = newValue.timeWidth(manager: self.manager)
6767
}
6868
}
6969
.onDisappear {
7070
self.manager.stop()
7171
}
7272
}
7373

74+
// MARK: - Subviews
75+
7476
private func styledTime(
7577
value: Int,
7678
unit: CountdownHelpers.Unit
@@ -99,20 +101,4 @@ public struct SUCountdown: View {
99101
.fill(self.model.backgroundColor.color(for: self.colorScheme))
100102
)
101103
}
102-
103-
private func calculateWidth(model: CountdownVM) {
104-
let values: [(Int, CountdownHelpers.Unit)] = [
105-
(self.manager.days, .days),
106-
(self.manager.hours, .hours),
107-
(self.manager.minutes, .minutes),
108-
(self.manager.seconds, .seconds)
109-
]
110-
111-
let widths = values.map { value, unit -> CGFloat in
112-
let attributedString = model.timeText(value: value, unit: unit)
113-
return CountdownWidthCalculator.preferredWidth(for: attributedString, model: model)
114-
}
115-
116-
self.timeWidth = (widths.max() ?? self.model.defaultMinWidth) + self.model.horizontalPadding * 2
117-
}
118104
}

0 commit comments

Comments
 (0)