feat: use shifter trainer scan flow

This commit is contained in:
2026-04-28 21:31:52 +02:00
parent be1c39d5d7
commit 5285c44173
2 changed files with 265 additions and 234 deletions

View File

@ -10,8 +10,6 @@ import 'package:abawo_bt_app/util/bluetooth_settings.dart';
import 'package:abawo_bt_app/widgets/bike_scan_dialog.dart'; import 'package:abawo_bt_app/widgets/bike_scan_dialog.dart';
import 'package:abawo_bt_app/widgets/gear_ratio_editor_card.dart'; import 'package:abawo_bt_app/widgets/gear_ratio_editor_card.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart'
show DiscoveredDevice;
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:nb_utils/nb_utils.dart'; import 'package:nb_utils/nb_utils.dart';
@ -438,20 +436,6 @@ class _DeviceDetailsPageState extends ConsumerState<DeviceDetailsPage> {
return; return;
} }
_isAssignTrainerDialogOpen = true;
final DiscoveredDevice? selectedBike;
try {
selectedBike = await BikeScanDialog.show(
context,
excludedDeviceId: widget.deviceAddress,
);
} finally {
_isAssignTrainerDialogOpen = false;
}
if (selectedBike == null || !mounted) {
return;
}
await _startStatusStreamingIfNeeded(); await _startStatusStreamingIfNeeded();
final shifter = _shifterService; final shifter = _shifterService;
if (shifter == null) { if (shifter == null) {
@ -463,8 +447,26 @@ class _DeviceDetailsPageState extends ConsumerState<DeviceDetailsPage> {
); );
return; return;
} }
if (!mounted) {
return;
}
final result = await shifter.connectButtonToBike(selectedBike.id); _isAssignTrainerDialogOpen = true;
final TrainerScanResult? selectedTrainer;
try {
selectedTrainer = await BikeScanDialog.show(
context,
shifter: shifter,
);
} finally {
_isAssignTrainerDialogOpen = false;
}
if (selectedTrainer == null || !mounted) {
return;
}
final result =
await shifter.connectButtonToTrainer(selectedTrainer.address);
if (!mounted) { if (!mounted) {
return; return;
} }
@ -479,7 +481,13 @@ class _DeviceDetailsPageState extends ConsumerState<DeviceDetailsPage> {
} }
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Sent connect request for ${selectedBike.id}.')), SnackBar(
content: Text(
selectedTrainer.name.isEmpty
? 'Sent connect request for trainer.'
: 'Sent connect request for ${selectedTrainer.name}.',
),
),
); );
} }

View File

@ -1,39 +1,39 @@
import 'dart:async'; import 'dart:async';
import 'package:abawo_bt_app/controller/bluetooth.dart';
import 'package:abawo_bt_app/model/shifter_types.dart'; import 'package:abawo_bt_app/model/shifter_types.dart';
import 'package:abawo_bt_app/service/shifter_service.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
class BikeScanDialog extends ConsumerStatefulWidget { class BikeScanDialog extends StatefulWidget {
const BikeScanDialog({ const BikeScanDialog({
required this.excludedDeviceId, required this.shifter,
super.key, super.key,
}); });
final String excludedDeviceId; final ShifterService shifter;
static Future<DiscoveredDevice?> show( static Future<TrainerScanResult?> show(
BuildContext context, { BuildContext context, {
required String excludedDeviceId, required ShifterService shifter,
}) { }) {
return showDialog<DiscoveredDevice>( return showDialog<TrainerScanResult>(
context: context, context: context,
barrierDismissible: true, barrierDismissible: true,
builder: (_) => BikeScanDialog(excludedDeviceId: excludedDeviceId), builder: (_) => BikeScanDialog(shifter: shifter),
); );
} }
@override @override
ConsumerState<BikeScanDialog> createState() => _BikeScanDialogState(); State<BikeScanDialog> createState() => _BikeScanDialogState();
} }
class _BikeScanDialogState extends ConsumerState<BikeScanDialog> { class _BikeScanDialogState extends State<BikeScanDialog> {
bool _showAll = false; bool _showOnlyFtms = true;
bool _isStartingScan = true; bool _isStartingScan = true;
bool _isScanning = false;
String? _scanError; String? _scanError;
BluetoothController? _controller; final Map<String, TrainerScanResult> _resultsByAddress = {};
StreamSubscription<TrainerScanEvent>? _scanSubscription;
@override @override
void initState() { void initState() {
@ -42,16 +42,39 @@ class _BikeScanDialogState extends ConsumerState<BikeScanDialog> {
} }
Future<void> _startScan() async { Future<void> _startScan() async {
await _scanSubscription?.cancel();
if (_isScanning) {
await widget.shifter.stopTrainerScan();
}
setState(() { setState(() {
_isStartingScan = true; _isStartingScan = true;
_isScanning = false;
_scanError = null; _scanError = null;
_resultsByAddress.clear();
}); });
try { try {
final controller = await ref.read(bluetoothProvider.future); _scanSubscription = widget.shifter.subscribeToTrainerScanResults().listen(
_controller = controller; _handleScanEvent,
await controller.stopScan(); onError: (Object error) {
await controller.startScan(); if (!mounted) {
return;
}
setState(() {
_scanError = error.toString();
_isStartingScan = false;
_isScanning = false;
});
},
);
final startResult = await widget.shifter.startTrainerScan();
if (startResult.isErr()) {
_scanError = startResult.unwrapErr().toString();
} else {
_isScanning = true;
}
} catch (error) { } catch (error) {
_scanError = error.toString(); _scanError = error.toString();
} finally { } finally {
@ -63,15 +86,43 @@ class _BikeScanDialogState extends ConsumerState<BikeScanDialog> {
} }
} }
void _handleScanEvent(TrainerScanEvent event) {
if (!mounted) {
return;
}
setState(() {
_isStartingScan = false;
switch (event.kind) {
case TrainerScanEventKind.scanStarted:
_isScanning = true;
_scanError = null;
break;
case TrainerScanEventKind.device:
final result = event.result;
if (result != null) {
_resultsByAddress[result.address.key] = result;
}
break;
case TrainerScanEventKind.scanFinished:
case TrainerScanEventKind.scanCancelled:
_isScanning = false;
break;
}
});
}
@override @override
void dispose() { void dispose() {
_controller?.stopScan(); _scanSubscription?.cancel();
if (_isScanning) {
unawaited(widget.shifter.stopTrainerScan());
}
super.dispose(); super.dispose();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final btAsync = ref.watch(bluetoothProvider);
final size = MediaQuery.of(context).size; final size = MediaQuery.of(context).size;
final dialogWidth = size.width < 640 ? size.width - 24 : 560.0; final dialogWidth = size.width < 640 ? size.width - 24 : 560.0;
final dialogHeight = size.height < 780 ? size.height * 0.78 : 560.0; final dialogHeight = size.height < 780 ? size.height * 0.78 : 560.0;
@ -83,216 +134,85 @@ class _BikeScanDialogState extends ConsumerState<BikeScanDialog> {
child: SizedBox( child: SizedBox(
width: dialogWidth, width: dialogWidth,
height: dialogHeight, height: dialogHeight,
child: btAsync.when( child: Column(
loading: () => const Center(child: CircularProgressIndicator()),
error: (err, _) => Center(child: Text('Bluetooth error: $err')),
data: (controller) {
_controller ??= controller;
return Column(
children: [ children: [
_DialogHeader( _DialogHeader(
showAll: _showAll, showOnlyFtms: _showOnlyFtms,
isScanning: _isStartingScan, isScanning: _isStartingScan || _isScanning,
onChanged: (value) { onChanged: (value) {
setState(() { setState(() {
_showAll = value; _showOnlyFtms = value;
}); });
}, },
onRescan: _startScan, onRescan: _startScan,
), ),
Expanded( Expanded(child: _buildBody(context)),
child: _scanError != null ],
? _ScanMessage( ),
message: 'Could not start trainer scan: $_scanError', ),
);
}
Widget _buildBody(BuildContext context) {
if (_scanError != null) {
return _ScanMessage(
message: 'Could not start shifter trainer scan: $_scanError',
action: TextButton.icon( action: TextButton.icon(
onPressed: _startScan, onPressed: _startScan,
icon: const Icon(Icons.refresh), icon: const Icon(Icons.refresh),
label: const Text('Retry'), label: const Text('Retry'),
), ),
) );
: StreamBuilder<List<DiscoveredDevice>>(
stream: controller.scanResultsStream,
initialData: controller.scanResults,
builder: (context, snapshot) {
if (_isStartingScan &&
(snapshot.data == null ||
snapshot.data!.isEmpty)) {
return const Center(
child: CircularProgressIndicator());
} }
final devices = if (_isStartingScan && _resultsByAddress.isEmpty) {
_filteredDevices(snapshot.data ?? const []); return const Center(child: CircularProgressIndicator());
}
final devices = _filteredDevices();
if (devices.isEmpty) { if (devices.isEmpty) {
return const _ScanMessage( return _ScanMessage(
message: message: _isScanning
'No matching trainers nearby. Keep scanning or enable Show All to inspect every device in range.', ? 'The shifter is scanning. Nearby trainers will appear here as soon as the shifter reports them.'
: 'No matching trainers were reported by the shifter. Rescan with the trainer nearby and awake.',
); );
} }
return ListView.separated( return ListView.separated(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 20), padding: const EdgeInsets.fromLTRB(20, 0, 20, 20),
itemCount: devices.length, itemCount: devices.length,
separatorBuilder: (_, __) => separatorBuilder: (_, __) => const SizedBox(height: 12),
const SizedBox(height: 12), itemBuilder: (context, index) => _TrainerScanResultTile(
itemBuilder: (context, index) { result: devices[index],
final device = devices[index]; onTap: () => Navigator.of(context).pop(devices[index]),
final isFtms = _advertisesFtms(device);
return Material(
color: Theme.of(context).colorScheme.surface,
borderRadius: BorderRadius.circular(22),
child: InkWell(
borderRadius: BorderRadius.circular(22),
onTap: () =>
Navigator.of(context).pop(device),
child: Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(22),
border: Border.all(
color: Theme.of(context)
.colorScheme
.outlineVariant
.withValues(alpha: 0.55),
),
),
child: Row(
children: [
Container(
width: 50,
height: 50,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Theme.of(context)
.colorScheme
.primary
.withValues(alpha: 0.12),
),
child: Icon(
Icons.pedal_bike_rounded,
color: Theme.of(context)
.colorScheme
.primary,
),
),
const SizedBox(width: 14),
Expanded(
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Text(
device.name.isEmpty
? 'Unknown Device'
: device.name,
maxLines: 1,
overflow:
TextOverflow.ellipsis,
style: Theme.of(context)
.textTheme
.titleMedium
?.copyWith(
fontWeight:
FontWeight.w700,
),
),
const SizedBox(height: 4),
Text(
isFtms
? 'FTMS'
: 'Nearby trainer',
style: Theme.of(context)
.textTheme
.bodyMedium
?.copyWith(
color: Theme.of(context)
.colorScheme
.primary,
fontWeight:
FontWeight.w600,
),
),
const SizedBox(height: 4),
Text(
device.id,
maxLines: 1,
overflow:
TextOverflow.ellipsis,
style: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(
color: Theme.of(context)
.colorScheme
.onSurface
.withValues(
alpha: 0.62),
),
),
],
),
),
const SizedBox(width: 12),
Column(
crossAxisAlignment:
CrossAxisAlignment.end,
children: [
_RssiBadge(rssi: device.rssi),
const SizedBox(height: 12),
Icon(
Icons.chevron_right_rounded,
color: Theme.of(context)
.colorScheme
.onSurface
.withValues(alpha: 0.55),
),
],
),
],
),
),
),
);
},
);
},
),
),
],
);
},
),
), ),
); );
} }
List<DiscoveredDevice> _filteredDevices(List<DiscoveredDevice> devices) { List<TrainerScanResult> _filteredDevices() {
return devices.where((device) { final devices = _resultsByAddress.values.where((device) {
if (device.id == widget.excludedDeviceId) { return !_showOnlyFtms || device.ftmsDetected;
return false;
}
if (_showAll) {
return true;
}
return _advertisesFtms(device);
}).toList(growable: false); }).toList(growable: false);
devices.sort((a, b) {
final ftmsCompare = (b.ftmsDetected ? 1 : 0) - (a.ftmsDetected ? 1 : 0);
if (ftmsCompare != 0) {
return ftmsCompare;
} }
return b.rssi.compareTo(a.rssi);
bool _advertisesFtms(DiscoveredDevice device) { });
return device.serviceUuids.any(isFtmsUuid) || return devices;
device.serviceData.keys.any(isFtmsUuid);
} }
} }
class _DialogHeader extends StatelessWidget { class _DialogHeader extends StatelessWidget {
const _DialogHeader({ const _DialogHeader({
required this.showAll, required this.showOnlyFtms,
required this.isScanning, required this.isScanning,
required this.onChanged, required this.onChanged,
required this.onRescan, required this.onRescan,
}); });
final bool showAll; final bool showOnlyFtms;
final bool isScanning; final bool isScanning;
final ValueChanged<bool> onChanged; final ValueChanged<bool> onChanged;
final VoidCallback onRescan; final VoidCallback onRescan;
@ -319,7 +239,7 @@ class _DialogHeader extends StatelessWidget {
), ),
const SizedBox(height: 6), const SizedBox(height: 6),
Text( Text(
'Tap a nearby trainer to assign it to the connected shifter.', 'The shifter scans nearby trainers. Tap one to assign it.',
style: Theme.of(context).textTheme.bodyMedium?.copyWith( style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Theme.of(context) color: Theme.of(context)
.colorScheme .colorScheme
@ -344,13 +264,13 @@ class _DialogHeader extends StatelessWidget {
child: Row( child: Row(
children: [ children: [
Text( Text(
'Show All', 'FTMS only',
style: Theme.of(context).textTheme.bodyMedium?.copyWith( style: Theme.of(context).textTheme.bodyMedium?.copyWith(
fontWeight: FontWeight.w600, fontWeight: FontWeight.w600,
), ),
), ),
const SizedBox(width: 8), const SizedBox(width: 8),
Switch(value: showAll, onChanged: onChanged), Switch(value: showOnlyFtms, onChanged: onChanged),
], ],
), ),
), ),
@ -379,6 +299,109 @@ class _DialogHeader extends StatelessWidget {
} }
} }
class _TrainerScanResultTile extends StatelessWidget {
const _TrainerScanResultTile({
required this.result,
required this.onTap,
});
final TrainerScanResult result;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
final colorScheme = Theme.of(context).colorScheme;
final name = result.name.isEmpty ? 'Unknown Trainer' : result.name;
final typeLabel = result.ftmsDetected ? 'FTMS trainer' : 'Nearby device';
final addressText = _formatTrainerAddress(result.address);
return Material(
color: colorScheme.surface,
borderRadius: BorderRadius.circular(22),
child: InkWell(
borderRadius: BorderRadius.circular(22),
onTap: onTap,
child: Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(22),
border: Border.all(
color: colorScheme.outlineVariant.withValues(alpha: 0.55),
),
),
child: Row(
children: [
Container(
width: 50,
height: 50,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: colorScheme.primary.withValues(alpha: 0.12),
),
child: Icon(
Icons.pedal_bike_rounded,
color: colorScheme.primary,
),
),
const SizedBox(width: 14),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w700,
),
),
const SizedBox(height: 4),
Text(
typeLabel,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 4),
Text(
addressText,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color:
colorScheme.onSurface.withValues(alpha: 0.62),
),
),
],
),
),
const SizedBox(width: 12),
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
_RssiBadge(rssi: result.rssi),
const SizedBox(height: 12),
Icon(
Icons.chevron_right_rounded,
color: colorScheme.onSurface.withValues(alpha: 0.55),
),
],
),
],
),
),
),
);
}
String _formatTrainerAddress(TrainerAddress address) {
final flags = address.flags.toRadixString(16).padLeft(2, '0');
return '${formatMacAddressFromLittleEndian(address.bytes)} · flags 0x$flags';
}
}
class _ScanMessage extends StatelessWidget { class _ScanMessage extends StatelessWidget {
const _ScanMessage({ const _ScanMessage({
required this.message, required this.message,