Keep the device registered on in-place "No connection" disconnect

The "No connection" picker entry disconnects without leaving the details
page, but Connection.disconnect(forget: false) removed the ProxyDevice
from the registry (isResetting is false). The open page kept the now
orphaned object: rediscovery built a *new* ProxyDevice while the page
still pointed at the old one, so selecting Virtual Shifting again
reconnected a device absent from bluetoothDevices — its BLE notifications
were rejected ("Device not found") and the connection was force-dropped.

Add a keepInList flag to disconnect; the in-place "No connection" path
passes it so the same device object stays registered (and its scan
result is retained to avoid a churned duplicate) and remains
reconnectable on the same card. The Disconnect buttons, which pop the
page, keep the default removing behaviour.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jonas Bark
2026-06-10 14:14:48 +02:00
parent 60817dabee
commit 0db8cf0c31
3 changed files with 47 additions and 6 deletions

View File

@@ -595,7 +595,12 @@ class Connection {
_connectionStreams.add(baseDevice);
}
Future<void> disconnect(BaseDevice device, {required bool persistForget, required bool forget}) async {
Future<void> disconnect(
BaseDevice device, {
required bool persistForget,
required bool forget,
bool keepInList = false,
}) async {
_cancelInactivityTimer(device);
if (device.isConnected) {
@@ -608,7 +613,11 @@ class Connection {
await core.settings.addIgnoredDevice(device.device.deviceId, device.toString());
_actionStreams.add(LogNotification('Device ignored: ${device.toString()}'));
}
if (!forget) {
// For an in-place disconnect (the "No connection" picker entry) keep the
// scan result so the scanner doesn't churn a fresh duplicate while the
// user stays on the details page — the same device object must remain
// reconnectable.
if (!forget && !keepInList) {
// allow reconnection
_lastScanResult.removeWhere((d) => d.deviceId == device.device.deviceId);
}
@@ -620,9 +629,12 @@ class Connection {
_connectionSubscriptions.remove(device);
// Remove device from the list — unless it is rebooting due to an
// automatic reset and will be back in a few seconds: keep its entry
// visible (greyed out) instead of letting it disappear.
if (forget || !device.isResetting) {
// automatic reset and will be back in a few seconds, or this is an
// in-place disconnect (keepInList) where the open details page still
// holds this exact object and must be able to reconnect it. Dropping it
// here would orphan that reference: rediscovery builds a *new* device,
// and the page's stale one fails to reconnect ("Device not found").
if (!keepInList && (forget || !device.isResetting)) {
devices.remove(device);
hasDevices.value = devices.isNotEmpty;
}

View File

@@ -120,7 +120,9 @@ class _ConnectionCardState extends State<ConnectionCard> {
if (selection == _ConnectSelection.none) {
if (connected) {
await core.settings.setAutoConnect(device.trainerKey, false);
await core.connection.disconnect(device, forget: false, persistForget: false);
// Disconnect in place: keep this device registered (and on the page) so
// the user can pick Virtual Shifting / Proxy again on the same object.
await core.connection.disconnect(device, forget: false, persistForget: false, keepInList: true);
}
return;
}

View File

@@ -88,5 +88,32 @@ void main() {
device.debugRebindEmulatorState();
expect(device.isStartedListenable.value, isTrue);
});
test('in-place disconnect keeps the device registered; a normal disconnect removes it', () async {
final inPlace = ProxyDevice(BleDevice(
deviceId: 'a',
name: 'KICKR A',
services: const ['00001826-0000-1000-8000-00805f9b34fb'],
));
final normal = ProxyDevice(BleDevice(
deviceId: 'b',
name: 'KICKR B',
services: const ['00001826-0000-1000-8000-00805f9b34fb'],
));
core.connection.devices
..clear()
..addAll([inPlace, normal]);
// "No connection" disconnects in place — the device must stay in the
// registry so the open details page can reconnect the same object.
// Without it the page's reference is orphaned and reconnect logs
// "Device not found".
await core.connection.disconnect(inPlace, forget: false, persistForget: false, keepInList: true);
expect(core.connection.devices, contains(inPlace));
// The Disconnect button (which also pops the page) removes it as before.
await core.connection.disconnect(normal, forget: false, persistForget: false);
expect(core.connection.devices, isNot(contains(normal)));
});
});
}