**Live Activity auto-renewal (8-hour limit workaround)**#539
Merged
bjorkert merged 11 commits intoloopandlearn:live-activityfrom Mar 13, 2026
Merged
**Live Activity auto-renewal (8-hour limit workaround)**#539bjorkert merged 11 commits intoloopandlearn:live-activityfrom
bjorkert merged 11 commits intoloopandlearn:live-activityfrom
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- staleDate on every ActivityContent now tracks the renewal deadline, so the system shows Apple's built-in stale indicator if renewal fails - Add laRenewalFailed StorageValue; set on Activity.request() failure, cleared on any successful LA start - Observe willEnterForegroundNotification: retry startIfNeeded() if a previous renewal attempt failed - New-first renewal order: request the replacement LA before ending the old one — if the request throws the existing LA stays alive so the user keeps live data until the system kills it at the 8-hour mark Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Restore renewalThreshold to 7.5 * 3600 (testing complete) - Add showRenewalOverlay: Bool to GlucoseSnapshot (Codable, default false) - GlucoseSnapshotBuilder sets it true when now >= laRenewBy - 1800 (30 minutes before the renewal deadline) - Lock screen: 60% gray overlay with "Tap to update" centered in white, layered above the existing isNotLooping overlay - DI expanded: RenewalOverlayView applied to leading/trailing/bottom regions; "Tap to update" text shown on the bottom region only - showRenewalOverlay resets to false automatically on renewal since laRenewBy is updated and the next snapshot rebuild clears the flag Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Overlay rendering:
- Replace Group{if condition{ZStack{RoundedRectangle...}}} with a
permanently-present ZStack toggled via .opacity(). The Group/if
pattern causes SwiftUI sizing ambiguity when the condition transitions
from false→true inside an .overlay(), producing a zero-size result.
The .opacity() approach keeps a stable view hierarchy.
- Same fix applied to RenewalOverlayView used on DI expanded regions.
Foreground restart:
- handleForeground() was calling startIfNeeded(), which finds the
still-alive (failed-to-renew) LA in Activity.activities and reuses
it, doing nothing useful. Fixed to manually nil out current, cancel
all tasks, await activity.end(.immediate), then startFromCurrentState().
Overlay timing:
- Changed warning window from 30 min (1800s) to 20 min (1200s) before
the renewal deadline, matching the intended test cadence.
Logging:
- handleForeground: log on every foreground event with laRenewalFailed value
- renewIfNeeded: log how many seconds past the deadline when firing
- GlucoseSnapshotBuilder: log when overlay activates with seconds to deadline
- performRefresh: log when sending an update with the overlay visible
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Normal renewal path (renewIfNeeded success): - Build a fresh snapshot with showRenewalOverlay: false for the new LA's initial content — it has a new deadline so the overlay should never be visible from the first frame. - Save that fresh snapshot to GlucoseSnapshotStore so the next duplicate check has the correct baseline and doesn't suppress the first real BG update. Foreground restart path (handleForeground): - Zero laRenewBy before calling startFromCurrentState() so GlucoseSnapshotBuilder computes showRenewalOverlay = false for the seed snapshot. startIfNeeded() then sets the new deadline after the request succeeds. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
With renewalThreshold=20min and the hardcoded 1200s warning window, renewBy-1200 = start, so showRenewalOverlay is always true from the moment the LA begins. Extract a named renewalWarning constant (5 min for testing) so the warning window is always less than the threshold. The builder now reads LiveActivityManager.renewalWarning instead of a hardcoded literal. Production values to restore before merging: renewalThreshold = 7.5 * 3600 renewalWarning = 20 * 60 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ynchronously APNSClient was missing showRenewalOverlay from the push payload, so background APNs updates never delivered the overlay flag to the extension — only foreground direct ActivityKit updates did. In handleForeground, laRenewBy is now zeroed synchronously before spawning the async end/restart Task. This means any snapshot built between the foreground notification and the new LA start (e.g. from viewDidAppear's startFromCurrentState) computes showRenewalOverlay = false rather than reading the stale expired deadline. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…e path Reset laRenewBy and laRenewalFailed synchronously before tearing down the failed LA, then await activity.end() before calling startFromCurrentState(). This guarantees Activity.activities is clear when startIfNeeded() runs, so it takes the fresh-request path and writes a new laRenewBy. startFromCurrentState rebuilds the snapshot with showRenewalOverlay=false (laRenewBy=0), saves it to the store, then startIfNeeded uses that clean snapshot as the seed. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Draft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Live Activity auto-renewal (8-hour limit workaround)
Apple enforces an 8-hour maximum lifetime on Live Activities in the Dynamic Island.
Approach
On every glucose refresh (~5 min), LiveActivityManager checks whether the current LA has passed its renewal deadline. If so, it:
If the request fails, laRenewalFailed is set and the existing LA is kept. On next foreground entry, the stale LA is ended and a fresh one is started.
Renewal warning overlay
Starting 20 minutes before the renewal deadline, a gray "Tap to update" overlay is rendered over the lock screen view and all expanded Dynamic Island regions. This prompts the user to bring the app to the foreground, where the LA can be renewed.
The overlay flag (showRenewalOverlay) is computed in GlucoseSnapshotBuilder and included in every APNs push payload so it is delivered reliably in the background.
Robustness
Files changed
File: Storage/Storage.swift
Change: Added laRenewBy: TimeInterval and laRenewalFailed: Bool
────────────────────────────────────────
File: LiveActivity/GlucoseSnapshot.swift
Change: Added showRenewalOverlay: Bool field
────────────────────────────────────────
File: LiveActivity/GlucoseSnapshotBuilder.swift
Change: Computes showRenewalOverlay from laRenewBy
────────────────────────────────────────
File: LiveActivity/APNSClient.swift
Change: Includes showRenewalOverlay in APNs push payload
────────────────────────────────────────
File: LiveActivity/LiveActivityManager.swift
Change: Renewal logic: renewIfNeeded(), endOrphanedActivities(), foreground retry, renewalThreshold/renewalWarning constants
────────────────────────────────────────
File: LoopFollowLAExtension/LoopFollowLiveActivity.swift
Change: "Tap to update" overlay on lock screen and Dynamic Island expanded regions
Testing checklist