Persistent timers and stopwatches ensuring seamless state restoration.
Instantiate PersistableTimer with your choice of data source (UserDefaults or in-memory for testing and previewing purposes):
import PersistableTimer
let timer = PersistableTimer(dataSourceType: .inMemory)
let timer = PersistableTimer(dataSourceType: .userDefaults(.standard))
let timer = PersistableTimer(dataSourceType: .userDefaults(.standard), updateInterval: 0.5)
Start a stopwatch or countdown timer. Optionally force the start of a new timer even if another is running.
try await timer.start(type: .stopwatch)
// Start a countdown timer with a duration of 100 seconds
try await timer.start(type: .timer(duration: 100))
Pause and resume a running timer, or mark it as finished:
try await timer.pause()
try await timer.resume()
try await timer.finish(isResetTime: false)
Restore the timer's state after an app restart:
try timer.restore()
Subscribe to timer updates using the timeStream:
for await timeState in timer.timeStream {
// Update your UI with the current timeState
print(timerState.time)
}