Files
swiftcontrol/test/services/overlay
Jonas Bark b85264369a fix(overlay/ios): reuse the Live Activity instead of orphaning one per ride
A rider's diagnostics (iOS 26.6, 6.4.3) carried this four times inside a
minute, with "virtual shifting isn't working" attached:

  PlatformException(LIVE_ACTIVITY_ERROR, can't launch live activity,
  Maximum number of activities for target already exists)

That is ActivityKit's `targetMaximumExceeded`. On iOS the Live Activity IS
the gear overlay — the only place a rider can read BikeControl's virtual
gear, because trainer apps never show it — so once it fires the readout is
dead until the phone reboots.

show() minted a fresh random activity name and called createActivity every
time, and the id it got back lived only in this isolate's memory. Every
force-quit, engine restart or swiped-away activity therefore left an
activity behind that still held one of ActivityKit's per-target slots, and
nothing ever went looking for it. After enough rides the target is full and
every request throws.

- Query what iOS actually holds, adopt the one activity that is still
  live (updateActivity writes under its own attributes prefix, so an
  activity from a previous launch picks up fresh gear data), and end
  everything else before requesting anything.
- Collapse overlapping show() calls onto one in-flight future, so a
  trainer app flapping its connection can't queue up requests.
- Retry once after sweeping everything when the request still throws:
  `Activity.activities` reads empty for a beat after a cold start, which
  the plugin works around the same way in createOrUpdateActivity.
- Ask for removeWhenAppIsKilled so a graceful termination frees the slot.
- Use a stable activity name, which also stops each ride from leaking a
  new set of never-collected App Group UserDefaults keys.
- A show() that starts nothing now reports isShowing == false, instead of
  leaving the Overlay switch and the auto-show guard claiming an overlay
  the rider cannot see.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-20 11:07:00 +02:00
..