Files
swiftcontrol/lib/services/overlay/trainer_overlay_controller.dart
Jonas Bark abf52e4d7a overlay: re-resolve live FitnessBikeDefinition + fix macOS close + recordError
Fixes the user-reported bugs after the auto-show landed:

1. Auto-opened overlay buttons did nothing
   The trainer emulator rebinds a fresh FitnessBikeDefinition each time
   its transport restarts (e.g. when a trainer app connects), so the
   `def` captured at show() time can already be stale by the time the
   user taps + / −. Action handlers now re-resolve through a new
   `LiveDefinitionLookup` callback on every dispatch. Navigation's
   auto-show and OverlaySettingsSection's manual show both pass a
   lookup that reads `device.emulator.activeDefinition` live.

2. Toggling "Show overlay during ride" off didn't close the panel
   The vendored macOS plugin's `closeWindow` handler ignored its args
   and called `closeWindow(NSApp.mainWindow)`, which either terminated
   the app or no-op'd silently. Patched it to honor
   `isMainWindow: false` and close the (single) secondary window.

3. "Error in overlay.position.decode: Unexpected character: light"
   The vendored macOS plugin packs the Dart entrypoint args in the
   order [routeName, theme, argsJson]; the Windows runner uses
   `std::map` iteration order. The sub-window was indexing `args[1]`
   blindly. Now it scans `args[1..]` for the first JSON-looking
   payload with `x`/`y`.

Also:
- TrainerOverlayController.show now takes optional `liveDef`; all
  three implementations (desktop, android, ios) thread it through.
- Navigation initState no longer early-`return`s after auto-show, so
  the post-frame system-UI / changelog logic still runs.
- All silent `catch (_) {}` blocks in the overlay code now forward
  via `recordError(e, s, context: '...')`. New project memory
  entry [feedback_record_error.md] documents the rule.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 12:48:53 +02:00

59 lines
1.8 KiB
Dart

import 'package:bike_control/services/overlay/overlay_state.dart';
import 'package:flutter/foundation.dart';
import 'package:prop/emulators/definitions/fitness_bike_definition.dart';
/// Reasons `show()` may return `false`.
enum OverlayShowFailure {
permissionDenied,
systemDisabled,
unknown,
}
class OverlayShowResult {
final bool ok;
final OverlayShowFailure? failure;
final String? message;
const OverlayShowResult.ok()
: ok = true,
failure = null,
message = null;
const OverlayShowResult.fail(this.failure, {this.message}) : ok = false;
}
/// Looks up the current live [FitnessBikeDefinition] for the active trainer.
/// The emulator rebinds a fresh definition each time its transport starts,
/// so the controller can't cache the def captured at show() time — action
/// handlers must re-resolve it on every call or buttons silently no-op
/// against a stale instance.
typedef LiveDefinitionLookup = FitnessBikeDefinition? Function();
abstract class TrainerOverlayController {
ValueListenable<bool> get isShowing;
Future<OverlayShowResult> show(
FitnessBikeDefinition def,
Set<OverlayField> fields, {
LiveDefinitionLookup? liveDef,
});
Future<void> hide();
void updateFields(Set<OverlayField> fields);
}
class NoOpOverlayController implements TrainerOverlayController {
final ValueNotifier<bool> _showing = ValueNotifier(false);
@override
ValueListenable<bool> get isShowing => _showing;
@override
Future<OverlayShowResult> show(
FitnessBikeDefinition def,
Set<OverlayField> fields, {
LiveDefinitionLookup? liveDef,
}) async {
return const OverlayShowResult.fail(OverlayShowFailure.systemDisabled,
message: 'Overlay not supported on this platform');
}
@override
Future<void> hide() async {}
@override
void updateFields(Set<OverlayField> fields) {}
}