Skip to content

Commit 83a90cd

Browse files
committed
#29 issue fixed.
1 parent 23db92f commit 83a90cd

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

Sources/MagicTimer/MagicTimer.swift

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Sources/MagicTimer/MagicTimerBackgroundCalculator.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ public final class MagicTimerBackgroundCalculator: MagicTimerBackgroundCalculato
1616
public var isActiveBackgroundMode: Bool = true
1717
public var backgroundTimeCalculateHandler: ((TimeInterval) -> Void)?
1818

19+
private var shouldCalculate: Bool = false
20+
1921
public init() {
2022
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForegroundNotification), name: UIApplication.willEnterForegroundNotification, object: nil)
23+
NotificationCenter.default.addObserver(self, selector: #selector(willEnterBackgroundNotification), name: UIApplication.didEnterBackgroundNotification, object: nil)
2124
}
2225

2326
deinit {
@@ -30,17 +33,22 @@ public final class MagicTimerBackgroundCalculator: MagicTimerBackgroundCalculato
3033

3134
private func calculateDateDiffrence() -> TimeInterval? {
3235
guard let timerFiredDate else { return nil }
33-
let validTimeSubtraction = abs(timerFiredDate - Date())
36+
let validTimeSubtraction = floor(abs(timerFiredDate - Date()))
3437
return validTimeSubtraction.convertToTimeInterval()
3538
}
3639

3740
@objc
3841
private func willEnterForegroundNotification() {
39-
guard isActiveBackgroundMode else { return }
42+
guard isActiveBackgroundMode, shouldCalculate else { return }
4043

4144
if let timeInterval = calculateDateDiffrence() {
4245
backgroundTimeCalculateHandler?(timeInterval)
4346
}
4447
}
48+
49+
@objc
50+
private func willEnterBackgroundNotification() {
51+
shouldCalculate = true
52+
}
4553
}
4654

0 commit comments

Comments
 (0)