feat: basic trainer support

This commit is contained in:
2026-11-24 20:02:05 +01:00
parent 30b25784c1
commit 9dece10e29
17 changed files with 3048 additions and 58 deletions

View File

@ -10,6 +10,7 @@ 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/firmware_update_fullscreen.dart';
import 'package:abawo_bt_app/widgets/gear_ratio_editor_card.dart';
import 'package:abawo_bt_app/widgets/simulation_card.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
@ -494,27 +495,43 @@ class _DeviceDetailsPageState extends ConsumerState<DeviceDetailsPage> {
}
_isAssignTrainerDialogOpen = true;
final TrainerScanResult? selectedTrainer;
final TrainerAssignment? assignment;
try {
selectedTrainer = await BikeScanDialog.show(
assignment = await BikeScanDialog.show(
context,
shifter: shifter,
);
} finally {
_isAssignTrainerDialogOpen = false;
}
if (selectedTrainer == null || !mounted) {
if (assignment == null || !mounted) {
return;
}
final selectedTrainer = assignment.trainer;
final result =
await shifter.connectButtonToTrainer(selectedTrainer.address);
final result = await shifter.connectButtonToTrainer(selectedTrainer.address,
protocol: assignment.protocol);
if (!mounted) {
return;
}
if (result.isErr()) {
final err = result.unwrapErr();
if (err.toString().contains('Firmware update required')) {
await showDialog<void>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Firmware update required'),
content: const Text(
'This trainer model is not available in the installed firmware. Choose Automatic detection if your trainer supports FTMS, or contact abawo support for compatible firmware. Automatic firmware downloads are not available yet.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Close'))
],
));
return;
}
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Connect request failed: $err')),
);
@ -1124,6 +1141,13 @@ class _DeviceDetailsPageState extends ConsumerState<DeviceDetailsPage> {
),
],
),
const SizedBox(height: 16),
if (_shifterService != null)
SimulationCard(
key: ObjectKey(_shifterService),
shifter: _shifterService!,
enabled: !_isFirmwareUpdateBusy,
),
] else if (isCurrentConnected) ...[
_PairingRequiredCard(
isChecking: _isPairingCheckRunning,

View File

@ -0,0 +1,119 @@
import 'package:abawo_bt_app/model/trainer_model.dart';
import 'package:flutter/material.dart';
class TrainerModelSearchPage extends StatefulWidget {
const TrainerModelSearchPage({super.key, this.canSelect = true, this.models});
final bool canSelect;
final Future<List<TrainerModel>>? models;
@override
State<TrainerModelSearchPage> createState() => _TrainerModelSearchPageState();
}
class _TrainerModelSearchPageState extends State<TrainerModelSearchPage> {
late Future<List<TrainerModel>> _models =
widget.models ?? TrainerModel.load();
String _query = '';
Future<void> _select(TrainerModel model) async {
final selected = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: Text(model.name),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(model.limitations),
const SizedBox(height: 12),
Text('Compatible firmware: ${model.bundleLabel}'),
const SizedBox(height: 12),
const Text(
'Your installed firmware must include this model. FTMS versions of this trainer may also work with Automatic detection.'),
const SizedBox(height: 12),
const Text(
'Support depends on the exact model and firmware revision. Not every listed model has been tested on hardware.'),
if (!widget.canSelect) ...[
const SizedBox(height: 12),
const Text('Firmware update required',
style: TextStyle(fontWeight: FontWeight.w600)),
const SizedBox(height: 6),
const Text(
'Model selection requires newer firmware. Automatic firmware downloads are not available yet. Contact abawo support for compatible firmware.'),
],
])),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Back')),
if (widget.canSelect)
FilledButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('Find nearby trainer')),
],
));
if (selected == true && mounted) Navigator.pop(context, model);
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Find trainer model')),
body: SafeArea(
child: Column(children: [
Padding(
padding: const EdgeInsets.all(18),
child: TextField(
autofocus: true,
decoration: const InputDecoration(
labelText: 'Search brand or model',
prefixIcon: Icon(Icons.search),
hintText: 'For example, Kettler C12'),
onChanged: (value) => setState(() => _query = value),
)),
Expanded(
child: FutureBuilder<List<TrainerModel>>(
future: _models,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(
child: TextButton(
onPressed: () =>
setState(() => _models = TrainerModel.load()),
child:
const Text('Could not load models. Retry')));
}
if (!snapshot.hasData) {
return const Center(child: CircularProgressIndicator());
}
final models = snapshot.data!
.where((model) => model.matches(_query))
.toList();
if (models.isEmpty) {
return const Center(
child: Padding(
padding: EdgeInsets.all(24),
child: Text(
'No matching model found. Try the brand name or check whether your trainer supports FTMS.')));
}
return ListView.separated(
padding: const EdgeInsets.fromLTRB(18, 0, 18, 18),
itemCount: models.length,
separatorBuilder: (_, __) => const SizedBox(height: 10),
itemBuilder: (context, index) {
final model = models[index];
return Card(
child: ListTile(
contentPadding: const EdgeInsets.symmetric(
horizontal: 18, vertical: 8),
title: Text(model.name),
subtitle:
Text('${model.limitations}\n${model.id}'),
isThreeLine: true,
trailing: const Icon(Icons.chevron_right),
onTap: () => _select(model)));
});
})),
])),
);
}