Implemented the sync between feature and live activity timers for the ActivityReporting service in the Sample app target.

This commit is contained in:
2026-07-06 23:33:57 +02:00
parent b3f0826122
commit 7ec938ceb3
@@ -46,7 +46,7 @@ final class ActivityReporting: Reporting {
content: .init(
state: .init(
state: .recording,
anchor: anchor,
anchor: anchor.addingTimeInterval(Constant.Time.flooring),
elapsed: .zero
),
staleDate: Date.now.addingTimeInterval(Constant.Time.stale)
@@ -65,7 +65,7 @@ final class ActivityReporting: Reporting {
await update(
state: .paused,
anchor: nil,
elapsed: elapsed
elapsed: elapsed.rounded(.down)
)
#endif
}
@@ -79,7 +79,7 @@ final class ActivityReporting: Reporting {
#if os(iOS)
await update(
state: .recording,
anchor: anchor,
anchor: anchor.addingTimeInterval(Constant.Time.flooring),
elapsed: .zero
)
#endif
@@ -95,7 +95,7 @@ final class ActivityReporting: Reporting {
await update(
state: .processing,
anchor: nil,
elapsed: elapsed
elapsed: elapsed.rounded(.down)
)
#endif
}
@@ -163,6 +163,12 @@ private extension ActivityReporting {
private enum Constant {
/// The time constants.
enum Time {
/// The half second added to a running timer's anchor. The system counts the Live Activity's timer up from the anchor and
/// formats it with round-to-nearest, whereas the feature's own timer floors the elapsed seconds; moving the anchor half a
/// second into the future turns that rounding back into flooring, so both timers show the same whole second instead of the
/// Live Activity ticking one second ahead for the back half of each second.
static let flooring: TimeInterval = 0.5
/// The span past which the system marks the recording Live Activity stale once the app stops updating it because it was
/// killed, for example so an abandoned activity stops looking live instead of lingering for the activity's full lifetime.
/// Generous enough to outlast a recording of any realistic length.