Files
swiftcontrol/lib/pages/customize.dart
Jonas Bark 74bb2737ba fix: UI feedback round — remembered devices, Click V2 unlock, paywall, Pro dialog
- Remembered controllers offer a single "Forget" instead of two disconnect
  variants: there is no link to drop on a device that isn't here.
- Click V2: drop the unlock-mode dropdown and the new-unlock-method toggle.
  The explainer stays the one place the mode is chosen — it also does the
  follow-on work (un-ignoring the left puck, the right-side-only remap,
  cancelling the reset timer) that the bare dropdown never did. "Set up
  again" now sits on the right puck's card too.
- Paywall: the Base column reads "Basis"/"Base" rather than "VOLL"/"FULL",
  and the one-time Base card moves above the Pro plans so top-to-bottom
  matches the comparison table's left-to-right.
- A remembered controller's own settings page shows its button mapping
  again. The gate counted live controllers only and KeymapExplanation
  looked its filterDevice up in the live list, so the page fell through to
  a warning about connection methods, which had nothing to do with it.
- showGoProDialog takes a feature name for its title, so the keymap sync
  cloud icon says what it does instead of only "Pro Feature".

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

228 lines
8.7 KiB
Dart

import 'package:bike_control/bluetooth/devices/base_device.dart';
import 'package:bike_control/main.dart';
import 'package:bike_control/pages/trainer_connection_settings.dart';
import 'package:bike_control/utils/core.dart';
import 'package:bike_control/utils/i18n_extension.dart';
import 'package:bike_control/utils/iap/iap_manager.dart';
import 'package:bike_control/utils/keymap/apps/custom_app.dart';
import 'package:bike_control/utils/keymap/apps/supported_app.dart';
import 'package:bike_control/utils/keymap/manager.dart';
import 'package:bike_control/widgets/keymap_explanation.dart';
import 'package:bike_control/widgets/status_icon.dart';
import 'package:bike_control/widgets/ui/beta_pill.dart';
import 'package:bike_control/widgets/ui/openbikecontrol_logo.dart';
import 'package:bike_control/widgets/ui/warning.dart';
import 'package:shadcn_flutter/shadcn_flutter.dart';
class CustomizePage extends StatefulWidget {
final bool isMobile;
final BaseDevice? filterDevice;
const CustomizePage({super.key, required this.isMobile, this.filterDevice});
@override
State<CustomizePage> createState() => _CustomizeState();
}
class _CustomizeState extends State<CustomizePage> {
@override
void initState() {
IAPManager.instance.entitlements.addListener(_onIAPChange);
super.initState();
}
@override
void dispose() {
IAPManager.instance.entitlements.removeListener(_onIAPChange);
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
spacing: 12,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (core.actionHandler.supportedApp != null)
Row(
spacing: 8,
children: [
Expanded(
child: Select<SupportedApp?>(
value: core.actionHandler.supportedApp,
popup: SelectPopup(
items: SelectItemList(
children: [
..._getAllApps().map(
(a) => SelectItemButton(
value: a,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(child: Text(a.name)),
if (a is CustomApp)
BetaPill(text: 'CUSTOM')
else if (a.supports(AppConnectionMethod.obpBle) ||
a.supports(AppConnectionMethod.obpMdns) ||
a.supports(AppConnectionMethod.obpDirCon))
OpenBikeControlLogo(),
],
),
),
),
SelectItemButton(
value: CustomApp(profileName: 'New'),
child: Row(
spacing: 6,
children: [
Icon(Icons.add, color: Theme.of(context).colorScheme.mutedForeground),
Expanded(child: Text(context.i18n.createNewKeymap).normal.muted),
],
),
),
],
),
).call,
itemBuilder: (c, app) => Row(
spacing: 8,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(child: Text(screenshotMode ? 'Trainer app' : app!.name)),
if (app is CustomApp) BetaPill(text: 'CUSTOM'),
],
),
placeholder: Text(context.i18n.selectKeymap),
onChanged: (app) async {
if (app == null) {
return;
} else if (app.name == 'New') {
final profileName = await KeymapManager().showNewProfileDialog(context);
if (profileName != null && profileName.isNotEmpty) {
final customApp = CustomApp(profileName: profileName);
core.actionHandler.init(customApp);
await core.settings.setKeyMap(customApp);
setState(() {});
}
} else {
core.actionHandler.init(app);
await core.settings.setKeyMap(app);
setState(() {});
}
},
),
),
Tooltip(
tooltip: (c) => Text(context.i18n.synchronizeAcrossDevices),
child: StatusIcon(
status: IAPManager.instance.isProEnabled,
icon: Icons.cloud_upload,
started: IAPManager.instance.isProEnabled,
onPressed: IAPManager.instance.isProEnabled
? null
: () {
// The cloud icon carries no label of its own, so the
// dialog is where the rider finds out what it does.
IAPManager.instance.ensureProForFeature(
context,
featureName: context.i18n.synchronizeAcrossDevices,
);
},
),
),
KeymapManager().getManageProfileDialog(
context,
core.actionHandler.supportedApp is CustomApp ? core.actionHandler.supportedApp?.name : null,
onDone: () {
setState(() {});
},
),
],
),
if (!screenshotMode) Gap(12),
if (core.actionHandler.supportedApp != null && _hasSomethingToMap)
KeymapExplanation(
key: Key(core.actionHandler.supportedApp!.keymap.runtimeType.toString()),
keymap: core.actionHandler.supportedApp!.keymap,
filterDevice: widget.filterDevice,
onUpdate: () {
setState(() {});
},
)
else if (core.actionHandler.supportedApp == null)
Warning(
children: [
Text(context.i18n.noTrainerSelected).small,
Button.outline(
child: Text(context.i18n.openConnectionSettings),
onPressed: () async {
await context.push(const TrainerConnectionSettingsPage());
setState(() {});
},
),
],
)
else
Warning(
children: [
Text(context.i18n.noConnectionMethodSelected).small,
Button.outline(
child: Text(context.i18n.openConnectionSettings),
onPressed: () async {
await context.push(const TrainerConnectionSettingsPage());
setState(() {});
},
),
],
),
],
);
}
/// Whether there are any buttons worth showing a mapping table for.
///
/// [CustomizePage.filterDevice] is the strongest answer there is: the page is
/// embedded in that device's own settings, so the rider is looking straight
/// at the thing whose buttons they came to map. It counts even when the
/// device is only a remembered stand-in — a controller that is switched off
/// is still one whose mapping can be edited, and falling through to "no
/// connection method selected" both hid that and named a reason that had
/// nothing to do with it.
bool get _hasSomethingToMap =>
widget.filterDevice != null ||
core.connection.controllerDevices.isNotEmpty ||
core.connection.proxyDevices.any((e) => e.isConnected);
List<SupportedApp> _getAllApps() {
final baseApp = core.settings.getTrainerApp();
final customProfiles = core.settings.getCustomAppProfiles();
final customApps = customProfiles.map((profile) {
final customApp = CustomApp(profileName: profile);
final savedKeymap = core.settings.getCustomAppKeymap(profile);
if (savedKeymap != null) {
try {
customApp.decodeKeymap(savedKeymap);
} catch (e, s) {
recordError(e, s, context: 'getAllApps');
}
}
return customApp;
}).toList();
// If no custom profiles exist, add the default "Custom" one
if (customApps.isEmpty) {
customApps.add(CustomApp());
}
return [if (baseApp != null) baseApp, ...customApps];
}
void _onIAPChange() {
setState(() {});
}
}