I have the following code in my ProjectApp: App
.onChange(of: phase) { newValue in
if model.isStarted {
if newValue == .background {
lastActiveTimeStamp = Date()
}
if newValue == .active {
let diff = Date().timeIntervalSince(lastActiveTimeStamp)
lastActiveTimeStamp = Date()
model.totalSeconds += Int(diff)
}
}
}
This seems to be working well when I close the app (so it's in the background). However, when I swipe down the control center (or whatever it's called, where you can adjust brightness, volume etc) it starts adding random values. This happens because the app doesn't go into background phase, but rather becomes inactive. And then, when it's active again it adds the new diff. I tried changing newValue == .background to newValue != .active but that didn't help either.
How do I actually make my timer work correctly in all states? Thanks!