@@ -45,7 +45,20 @@ public class MagicTimer {
4545 }
4646
4747 /// Timer count mode. Default is `.stopWatch`. Checkout ```MagicTimerMode```.
48- public var countMode : MagicTimerMode = . stopWatch
48+ public var countMode : MagicTimerMode = . stopWatch {
49+ didSet {
50+ switch countMode {
51+ case . stopWatch:
52+ break
53+ case . countDown( let fromSeconds) :
54+ // Checking if defaultValue plus fromSeconds not going to invalid format(negative seconds).
55+ guard ( defultValue + fromSeconds) . truncatingRemainder ( dividingBy: effectiveValue) . isEqual ( to: . zero) else {
56+ fatalError ( " The time does not lead to a valid format. Use valid effetiveValue " )
57+ }
58+ counter. totalCountedValue = fromSeconds
59+ }
60+ }
61+ }
4962
5063 /// Timer default value. Default is 0.
5164 public var defultValue : TimeInterval = 0 {
@@ -169,27 +182,19 @@ public class MagicTimer {
169182 private func observeScheduleTimer( ) {
170183 executive. scheduleTimerHandler = { [ weak self] in
171184 guard let self else { return }
172-
173- switch self . countMode {
185+ switch countMode {
174186 case . stopWatch:
175- self . counter. add ( )
176- self . elapsedTime = self . counter. totalCountedValue
177- case . countDown( let fromSeconds) :
178- // Checking if defaultValue plus fromSeconds not going to invalid format(negative seconds).
179- guard ( self . defultValue + fromSeconds) . truncatingRemainder ( dividingBy: self . effectiveValue) . isEqual ( to: . zero) else {
180- fatalError ( " The time does not lead to a valid format. Use valid effetiveValue " )
181- }
182-
183- self . counter. totalCountedValue = fromSeconds
187+ counter. add ( )
188+ case . countDown( _) :
184189 guard counter. totalCountedValue. isBiggerThan ( . zero) else {
185190 executive. suspand {
186191 self . lastState = . stopped
187192 }
188193 return
189194 }
190195 counter. subtract ( )
191- elapsedTime = self . counter. totalCountedValue
192196 }
197+ elapsedTime = counter. totalCountedValue
193198 }
194199 }
195200}
0 commit comments