Files
swiftcontrol/lib/pages/proxy_device_details/front_shift_card.dart
Jonas Bark 9960a33fea feat: animated Virtual Shifting preview in the trainer onboarding step
Replaces the three benefit tiles with a three-scene stage that shows what
Virtual Shifting does in the widgets it actually does it with, and rebuilds
the scan as its own card with the found trainers inside it.

- Scene 1 pairs smart trainers with the training apps BikeControl supports.
- Scene 2 drives the gear editor's own ratio curve through its presets, so
  GearRatioCurve is split into a definition-bound wrapper plus a value-driven
  GearRatioCurveView, and the presets move to gear_ratio_presets.dart where
  both the editor and the preview read them.
- Scene 3 introduces FrontShiftVisual (chainrings + resulting ratio range),
  which FrontShiftCard now shows above its teeth steppers in place of the
  bare ratio factor.

The stage holds still under reduced motion and screenshotMode, opening on
initialScene so each scene can be captured deterministically.

Also localizes the ratio curve's hardcoded labels and pluralizes the
"trainers found" count.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-13 08:00:06 +02:00

120 lines
5.1 KiB
Dart

import 'package:bike_control/bluetooth/devices/proxy/proxy_device.dart';
import 'package:bike_control/gen/l10n.dart';
import 'package:bike_control/models/shifting_config.dart';
import 'package:bike_control/pages/proxy_device_details/front_shift_visual.dart';
import 'package:bike_control/utils/core.dart';
import 'package:bike_control/widgets/ui/setting_tile.dart';
import 'package:bike_control/widgets/ui/stepper_control.dart';
import 'package:prop/emulators/definitions/fitness_bike_definition.dart';
import 'package:shadcn_flutter/shadcn_flutter.dart';
/// The "Virtual front derailleur" setting: an enable toggle plus small/large
/// chainring steppers and the resulting ratio factor. Persists to the active
/// [ShiftingConfig] for [device] AND applies to the live [definition] so the
/// change takes effect immediately (mirroring the other cards on this page —
/// otherwise the gear settings only apply after a reconnect/mode-switch).
class FrontShiftCard extends StatelessWidget {
const FrontShiftCard({super.key, required this.device, required this.definition});
final ProxyDevice device;
final FitnessBikeDefinition definition;
Future<void> _update(ShiftingConfig Function(ShiftingConfig) mutate) async {
final current = core.shiftingConfigs.activeFor(device.trainerKey);
await core.shiftingConfigs.upsert(mutate(current));
}
@override
Widget build(BuildContext context) {
final config = core.shiftingConfigs.activeFor(device.trainerKey);
final enabled = config.frontShiftEnabled;
final small = config.smallChainringTeeth;
final large = config.largeChainringTeeth;
// The live curve if the definition has one, else whatever this config
// stores (or the stock curve for its gear count) — the picture should be
// right even before the trainer has published its ratios.
final live = definition.gearRatios.value;
final ratios = live.isNotEmpty
? live
: (config.gearRatios ?? FitnessBikeDefinition.defaultGearRatiosFor(config.maxGear));
return SettingTile(
icon: LucideIcons.bike,
title: AppLocalizations.of(context).frontShiftEnableLabel,
subtitle: AppLocalizations.of(context).frontShiftEnableDesc,
trailing: Switch(
value: enabled,
onChanged: (v) async {
definition.setFrontShiftEnabled(v);
await _update((c) => c.copyWith(frontShiftEnabled: v));
},
),
child: enabled
? Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
spacing: 12,
children: [
// Which ring is engaged changes on every front shift, so the
// picture listens rather than reading it once at build time.
ValueListenableBuilder<FrontRing>(
valueListenable: definition.frontRing,
builder: (context, ring, _) => FrontShiftVisual(
smallTeeth: small,
largeTeeth: large,
largeRingActive: ring == FrontRing.large,
ratios: ratios,
gearCount: config.maxGear,
),
),
Row(
children: [
Expanded(
child: Text(
AppLocalizations.of(context).frontShiftSmallRingLabel,
style: const TextStyle(fontSize: 13),
),
),
StepperControl(
value: small.toDouble(),
step: 1.0,
min: ShiftingConfig.chainringTeethMin.toDouble(),
// Keep small <= large: the large ring must stay the bigger
// (harder) one, else the front shift inverts or no-ops.
max: large.toDouble(),
format: (v) => v.toStringAsFixed(0),
onChanged: (v) async {
final next = v.toInt();
definition.setChainringTeeth(next, large);
await _update((c) => c.copyWith(smallChainringTeeth: next));
},
),
],
),
Row(
children: [
Expanded(
child: Text(
AppLocalizations.of(context).frontShiftLargeRingLabel,
style: const TextStyle(fontSize: 13),
),
),
StepperControl(
value: large.toDouble(),
step: 1.0,
min: small.toDouble(),
max: ShiftingConfig.chainringTeethMax.toDouble(),
format: (v) => v.toStringAsFixed(0),
onChanged: (v) async {
final next = v.toInt();
definition.setChainringTeeth(small, next);
await _update((c) => c.copyWith(largeChainringTeeth: next));
},
),
],
),
],
)
: null,
);
}
}