Keep custom mappings on buttons that share a name with another controller (#516)

A saved keymap stores a button by name only. On reload, `leftSteer` decodes
to the Elite Sterzo's button, not the KICKR BIKE's, so the KICKR's steer
buttons lost their mapping after every restart and got the Navigate Left/Right
click default instead: holding the button fired a click, never the long press.

addNewButtons now copies the rider's saved pairs onto a connected button whose
name they carry (as it already did for renamed Elite Square buttons), and
exact duplicate pairs are dropped on load so the keymap stops growing.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jonasbark
2026-11-24 10:39:11 +01:00
committed by GitHub
parent f366eff986
commit 55c44cd8c6
3 changed files with 161 additions and 7 deletions

View File

@@ -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;
}

View File

@@ -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)) {

View File

@@ -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<void> 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<void>.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<void> connect() async {}
}