From 7ec938ceb3365ca9bc08e49205380080a00f1e74 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Mon, 6 Jul 2026 23:33:57 +0200 Subject: [PATCH] Implemented the sync between feature and live activity timers for the ActivityReporting service in the Sample app target. --- .../Sources/Services/ActivityReporting.swift | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Apps/Attendi/Sources/Services/ActivityReporting.swift b/Apps/Attendi/Sources/Services/ActivityReporting.swift index 34f67d9..9f9f956 100644 --- a/Apps/Attendi/Sources/Services/ActivityReporting.swift +++ b/Apps/Attendi/Sources/Services/ActivityReporting.swift @@ -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.