Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions Sources/MagicTimer/MagicTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ public class MagicTimer {
private var counter: MagicTimerCounterInterface
private var executive: MagicTimerExecutiveInterface
private var backgroundCalculator: MagicTimerBackgroundCalculatorInterface

// MARK: - Unavailable
/// A elapsed time that can observe
/// - Warning: renamed: "elapsedTimeDidChangeHandler"
public var observeElapsedTime: ((TimeInterval) -> Void)?

/// The current state of the timer.
/// - Warning: renamed: "lastState"
Expand Down Expand Up @@ -123,8 +118,9 @@ public class MagicTimer {
executive.fire {
self.backgroundCalculator.timerFiredDate = Date()
self.lastState = .fired
self.setCounter()
self.observeScheduleTimer()
}
}
}

/// Stop counting the timer.
Expand All @@ -151,6 +147,15 @@ public class MagicTimer {
}

// MARK: - Private methods
private func setCounter() {
switch countMode {
case .countDown(let fromSeconds):
counter.defultValue = fromSeconds
counter.resetToDefaultValue()
default: break
}
}

// It calculates the elapsed time user was in background.
private func calclulateBackgroundTime(elapsedTime: TimeInterval) {
switch countMode {
Expand All @@ -171,25 +176,24 @@ public class MagicTimer {
executive.scheduleTimerHandler = { [weak self] in
guard let self else { return }

switch self.countMode {
switch countMode {
case .stopWatch:
self.counter.add()
self.elapsedTime = self.counter.totalCountedValue
counter.add()
elapsedTime = counter.totalCountedValue
case .countDown(let fromSeconds):
// Checking if defaultValue plus fromSeconds not going to invalid format(negative seconds).
guard (self.defultValue + fromSeconds).truncatingRemainder(dividingBy: self.effectiveValue).isEqual(to: .zero) else {
guard (defultValue + fromSeconds).truncatingRemainder(dividingBy: effectiveValue).isEqual(to: .zero) else {
fatalError("The time does not lead to a valid format. Use valid effetiveValue")
}

self.counter.totalCountedValue = fromSeconds
guard counter.totalCountedValue.isBiggerThan(.zero) else {
executive.suspand {
self.lastState = .stopped
}
return
}
counter.subtract()
elapsedTime = self.counter.totalCountedValue
elapsedTime = counter.totalCountedValue
}
}
}
Expand Down