diff --git a/lib/utils/keymap/apps/custom_app.dart b/lib/utils/keymap/apps/custom_app.dart index c507fd4d..59672c00 100644 --- a/lib/utils/keymap/apps/custom_app.dart +++ b/lib/utils/keymap/apps/custom_app.dart @@ -35,7 +35,9 @@ class CustomApp extends SupportedApp { return; } - final keyPairs = data.map((e) => KeyPair.decode(e)).whereNotNull().toList(); + // A pair copied onto another controller's same-named button (see + // Keymap.addNewButtons) is saved exactly like the original; one is enough. + final keyPairs = data.distinct().map((e) => KeyPair.decode(e)).whereNotNull().toList(); if (keyPairs.isEmpty) { return; } diff --git a/lib/utils/keymap/keymap.dart b/lib/utils/keymap/keymap.dart index 1aa8513e..a57732a4 100644 --- a/lib/utils/keymap/keymap.dart +++ b/lib/utils/keymap/keymap.dart @@ -227,15 +227,18 @@ class Keymap { } } - /// A custom keymap saved before a button was renamed still holds its - /// mapping under the old name. Copies the first saved pair per trigger — - /// the rider's own, any later ones being defaults appended on restarts. + /// A custom keymap stores a button by name only, so a reloaded mapping can + /// hold a different controller's button of the same name: decoding picks + /// the first known button called `leftSteer` (the Elite Sterzo's), not the + /// KICKR BIKE's. A keymap saved before a button was renamed holds its + /// mapping under the old name. Either way the rider's mapping is copied + /// onto [button] — the first saved pair per trigger, the rider's own, any + /// later ones being defaults appended on restarts. bool _adoptLegacyMapping(ControllerButton button) { if (!isCustom) return false; - final legacyName = EliteSquareButtons.legacyNames[button.name]; - if (legacyName == null) return false; + final savedName = EliteSquareButtons.legacyNames[button.name] ?? button.name; final legacyPairs = keyPairs - .where((kp) => kp.buttons.any((b) => b.name == legacyName && b.sourceDeviceId == button.sourceDeviceId)) + .where((kp) => kp.buttons.any((b) => b.name == savedName && b.sourceDeviceId == button.sourceDeviceId)) .toList(); if (legacyPairs.isEmpty) return false; for (final legacy in legacyPairs.distinctBy((kp) => kp.trigger)) { diff --git a/test/utils/keymap/custom_keymap_shared_button_name_test.dart b/test/utils/keymap/custom_keymap_shared_button_name_test.dart new file mode 100644 index 00000000..0a5d8180 --- /dev/null +++ b/test/utils/keymap/custom_keymap_shared_button_name_test.dart @@ -0,0 +1,149 @@ +import 'package:bike_control/bluetooth/devices/base_device.dart'; +import 'package:bike_control/bluetooth/devices/elite/elite_sterzo.dart'; +import 'package:bike_control/bluetooth/devices/wahoo/wahoo_kickr_bike_shift.dart'; +import 'package:bike_control/gen/l10n.dart'; +import 'package:bike_control/utils/actions/base_actions.dart'; +import 'package:bike_control/utils/core.dart'; +import 'package:bike_control/utils/keymap/apps/custom_app.dart'; +import 'package:bike_control/utils/keymap/buttons.dart'; +import 'package:bike_control/utils/keymap/keymap.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +/// A saved keymap stores a button by name only, and some names belong to +/// more than one controller: the KICKR BIKE's steer buttons and the Elite +/// Sterzo's are both `leftSteer`/`rightSteer`. Reloading must keep the +/// rider's mapping on the controller that is actually connected, instead of +/// handing it the button's built-in default. +Future main() async { + TestWidgetsFlutterBinding.ensureInitialized(); + await AppLocalizations.load(const Locale('en')); + + const kickrLeft = WahooKickrShiftButtons.leftSteer; + const kickrRight = WahooKickrShiftButtons.rightSteer; + + setUp(() async { + SharedPreferences.setMockInitialValues({'trainer_app': 'MyWhoosh'}); + core.settings.prefs = await SharedPreferences.getInstance(); + core.actionHandler = StubActions(); + }); + + /// Saves and reloads [app], then connects the KICKR BIKE, as an app start does. + CustomApp restart(CustomApp app) { + final reloaded = CustomApp(profileName: app.profileName)..decodeKeymap(app.encodeKeymap()); + core.actionHandler.init(reloaded); + reloaded.keymap.addNewButtons([kickrLeft, kickrRight]); + return reloaded; + } + + /// The rider's setup: steer on a long press only, nothing on a click. + CustomApp riderKeymap() { + final app = CustomApp(profileName: 'MyWhoosh'); + core.actionHandler.init(app); + for (final (button, action) in [(kickrLeft, InGameAction.steerLeft), (kickrRight, InGameAction.steerRight)]) { + app.keymap.addKeyPair( + KeyPair(buttons: [button], physicalKey: null, logicalKey: null, trigger: ButtonTrigger.singleClick), + ); + app.keymap.addKeyPair( + KeyPair( + buttons: [button], + physicalKey: null, + logicalKey: null, + trigger: ButtonTrigger.longPress, + inGameAction: action, + ), + ); + } + return app; + } + + test('KICKR BIKE steer long press survives restarts', () { + var app = riderKeymap(); + int? count; + + for (var i = 0; i < 3; i++) { + app = restart(app); + // The first start copies the mapping onto the KICKR's buttons; later + // starts must not keep adding to it. + count ??= app.keymap.keyPairs.length; + expect(app.keymap.getKeyPair(kickrLeft, trigger: ButtonTrigger.longPress)?.inGameAction, InGameAction.steerLeft); + expect( + app.keymap.getKeyPair(kickrRight, trigger: ButtonTrigger.longPress)?.inGameAction, + InGameAction.steerRight, + ); + expect(app.keymap.getKeyPair(kickrLeft, trigger: ButtonTrigger.singleClick)?.hasNoAction, isTrue); + expect(app.keymap.keyPairs.length, count); + } + }); + + test('a keymap that already picked up the Navigate default keeps the rider\'s own pairs', () { + // What 7.6.1 saved after a restart: the rider's pairs, then the steer + // button's default (Navigate Left/Right on a click) appended behind them. + final saved = riderKeymap(); + for (final button in [kickrLeft, kickrRight]) { + saved.keymap.addKeyPair( + KeyPair( + buttons: [button], + physicalKey: null, + logicalKey: null, + trigger: ButtonTrigger.singleClick, + inGameAction: button.action, + ), + ); + } + + final app = restart(saved); + + expect(app.keymap.getKeyPair(kickrLeft, trigger: ButtonTrigger.longPress)?.inGameAction, InGameAction.steerLeft); + expect(app.keymap.getKeyPair(kickrLeft, trigger: ButtonTrigger.singleClick)?.hasNoAction, isTrue); + }); + + test('holding the KICKR BIKE steer button after a restart steers', () async { + final app = restart(riderKeymap()); + final stubActions = core.actionHandler as StubActions; + final device = _KickrSteerDevice(); + addTearDown(device.disconnect); + expect(identical(core.actionHandler.supportedApp, app), isTrue); + + await device.handleButtonsClicked([kickrRight]); + await Future.delayed(const Duration(milliseconds: 700)); + await device.handleButtonsClicked([]); + + expect(stubActions.performedActions.map((a) => a.trigger).toSet(), {ButtonTrigger.longPress}); + }); + + test('the Sterzo keeps its own mapping beside the KICKR BIKE', () { + final app = CustomApp(profileName: 'Both') + ..decodeKeymap([ + KeyPair( + buttons: [SterzoButtons.leftSteer], + physicalKey: null, + logicalKey: null, + trigger: ButtonTrigger.longPress, + inGameAction: InGameAction.steerLeft, + ).encode(), + ]); + core.actionHandler.init(app); + app.keymap.addNewButtons([SterzoButtons.leftSteer, kickrLeft]); + + expect( + app.keymap.getKeyPair(SterzoButtons.leftSteer, trigger: ButtonTrigger.longPress)?.inGameAction, + InGameAction.steerLeft, + ); + expect(app.keymap.getKeyPair(kickrLeft, trigger: ButtonTrigger.longPress)?.inGameAction, InGameAction.steerLeft); + }); +} + +class _KickrSteerDevice extends BaseDevice { + _KickrSteerDevice() + : super( + 'KICKR BIKE', + uniqueId: 'kickr-bike', + availableButtons: WahooKickrShiftButtons.values, + icon: Icons.gamepad, + ); + + @override + Future connect() async {} +}