Files
swiftcontrol/lib/widgets/menu.dart
Jonas Bark c1f1befe26 fix(onboarding): final-review fixes — sub-flow gating, prompts, exit affordance, test-mode card
Addresses F1-F8 from the whole-branch onboarding wizard review: gate the
press-a-button-to-continue listener on phase/connection/sub-flow-open state
via a new pure shouldAdvanceOnButtonPress(), prompt device sub-flows for
controllers already connected before step 3, show a "Setup needed" badge,
hide the test-mode card for purchased users, add a close affordance and
fullscreenDialog route, fix the stale connection-step target for
self-hosted BikeControl, pop the wizard after the Pro-options drawer
closes, and unwind back-navigation within the controller step first.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-09 22:16:50 +02:00

305 lines
11 KiB
Dart

import 'dart:async';
import 'dart:io';
import 'package:bike_control/bluetooth/devices/proxy/proxy_device.dart';
import 'package:bike_control/bluetooth/emulation/profiles/all_profiles.dart';
import 'package:bike_control/pages/markdown.dart';
import 'package:bike_control/pages/onboarding/onboarding_page.dart';
import 'package:bike_control/pages/paywall.dart';
import 'package:bike_control/pages/subscription.dart';
import 'package:bike_control/services/telemetry_snapshot.dart';
import 'package:bike_control/utils/core.dart';
import 'package:bike_control/utils/gear_readout.dart';
import 'package:bike_control/utils/i18n_extension.dart';
import 'package:bike_control/widgets/logviewer.dart';
import 'package:bike_control/widgets/title.dart';
import 'package:bike_control/widgets/ui/colors.dart';
import 'package:dartx/dartx.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart' show showLicensePage;
import 'package:flutter/services.dart';
import 'package:in_app_review/in_app_review.dart';
import 'package:keypress_simulator/keypress_simulator.dart';
import 'package:prop/emulators/definitions/fitness_bike_definition.dart';
import 'package:purchases_flutter/purchases_flutter.dart' show Purchases;
import 'package:shadcn_flutter/shadcn_flutter.dart';
import '../utils/iap/iap_manager.dart';
import 'package:bike_control/services/debug_diagnostics.dart';
import 'package:bike_control/main.dart' show recordError;
List<Widget> buildMenuButtons(BuildContext context) {
final iap = IAPManager.instance;
return [
// Pro/Subscription Button
Builder(
builder: (context) {
return Button(
style: ButtonStyle.primary()
.withBackgroundColor(color: iap.isProEnabled && false ? BKColor.mainEnd : null)
.withBorderRadius(
borderRadius: BorderRadius.circular(16),
),
onPressed: () {
openDrawer(
context: context,
builder: (c) => SubscriptionPage(),
position: OverlayPosition.end,
);
},
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.workspace_premium, size: 14),
const SizedBox(width: 4),
Text('Pro'),
],
),
);
},
),
if (IAPManager.instance.isPurchased.value || IAPManager.instance.isProEnabled) ...[
Gap(8),
Builder(
builder: (context) {
return IconButton(
variance: ButtonVariance.menu,
density: ButtonDensity.iconDense,
onPressed: () {
showDropdown(
context: context,
builder: (c) => DropdownMenu(
children: [
MenuButton(
leading: Icon(Icons.star_rate),
child: Text(context.i18n.leaveAReview),
onPressed: (c) async {
final InAppReview inAppReview = InAppReview.instance;
if (await inAppReview.isAvailable()) {
inAppReview.requestReview();
} else {
inAppReview.openStoreListing(appStoreId: 'id6753721284', microsoftStoreId: '9NP42GS03Z26');
}
},
),
],
),
);
},
icon: Icon(Icons.favorite_outline),
);
},
),
],
Gap(4),
BKMenuButton(),
];
}
Future<String> debugText({bool includeDiscovery = true}) async {
final userId = IAPManager.instance.isUsingRevenueCat ? (await Purchases.appUserID) : null;
final proxies = core.connection.proxyDevices;
final proxyBlock = proxies.isEmpty ? '-' : proxies.map(_describeProxyDevice).join('\n ');
String diagnostics;
try {
final diag = await DebugDiagnostics.gather(includeDiscovery: includeDiscovery);
diagnostics = diag.toText();
} catch (e, s) {
recordError(e, s, context: 'debugText.diagnostics');
diagnostics = 'Diagnostics: (unavailable)';
}
return '''
---
App Version: ${packageInfoValue?.version}${shorebirdPatch?.number != null ? '+${shorebirdPatch!.number}' : ''}
Update Track: ${IAPManager.instance.isBetaTester ? 'beta' : 'stable'}
Platform: ${Platform.operatingSystem} ${Platform.operatingSystemVersion}
Target: ${core.settings.getLastTarget()?.name ?? '-'}
Trainer App: ${core.settings.getTrainerApp()?.name ?? '-'}
Connected Controllers: ${core.connection.devices.map((e) => e.toString()).join(', ')}
Connected Trainers: ${core.logic.connectedTrainerConnections.map((e) => e.title).join(', ')}
Smart Trainers:
$proxyBlock
Status: ${IAPManager.instance.getStatusMessage()}${userId != null ? ' (User ID: $userId)' : ''}
$diagnostics
Logs:
${core.connection.lastLogEntries.reversed.joinToString(separator: '\n', transform: (e) => '${e.date.toString().split('.').first} - ${e.entry}')}
''';
}
/// Compact summary of a [ProxyDevice] for the support / feedback payload.
/// First line lists the bits that matter for diagnosing a Bridge / proxy
/// issue (state, retrofit mode, active definition class, firmware,
/// manufacturer). When the emulator has discovered non-standard BLE
/// services on the trainer, the same "Services & characteristics:" block
/// the chat freetext uses is appended on subsequent lines.
String _describeProxyDevice(ProxyDevice device) {
final emulator = device.emulator;
final state = !device.isConnected
? 'disconnected'
: !emulator.isStarted.value
? 'starting'
: emulator.isConnected.value
? 'bridged'
: 'started';
final mode = device.retrofitMode.value.name;
final def = emulator.fitnessBike;
final defKind = def == null ? 'none' : def.runtimeType.toString();
final parts = <String>[
device.scanResult.name ?? device.scanResult.deviceId,
'mode=$mode',
'state=$state',
'def=$defKind',
];
if (device.firmwareVersion != null) parts.add('fw=${device.firmwareVersion}');
if (device.manufacturerName != null) parts.add('mfg=${device.manufacturerName}');
if (def != null) {
parts.add(
'gear=${formatGearReadout(currentGear: def.currentGear.value, maxGear: def.maxGear, frontShiftEnabled: def.frontShiftEnabled, largeRing: def.frontRing.value == FrontRing.large)}',
);
parts.add('trainerMode=${def.trainerMode.value.name}');
}
final summary = parts.join(' · ');
final services = buildProxyServicesFreetext(device);
if (services == null) return summary;
// Indent the services block so it visibly belongs to its proxy entry.
final indented = services.split('\n').map((l) => ' $l').join('\n');
return '$summary\n$indented';
}
class BKMenuButton extends StatelessWidget {
const BKMenuButton({super.key});
@override
Widget build(BuildContext context) {
return IconButton(
variance: ButtonVariance.menu,
density: ButtonDensity.iconDense,
icon: Icon(Icons.more_vert),
onPressed: () => showDropdown(
context: context,
builder: (c) => DropdownMenu(
children: [
if (kDebugMode) ...[
MenuButton(
subMenu: [
for (final profile in allEmulationProfiles)
MenuButton(
onPressed: (c) {
core.emulation.start(profile);
unawaited(core.connection.performScanning());
},
child: Text(profile.name),
),
],
child: const Text('Emulate device'),
),
MenuButton(
child: Text(context.i18n.reset),
onPressed: (c) async {
await core.settings.reset();
},
),
MenuButton(
child: Text('Send Key'),
onPressed: (c) async {
await Future.delayed(Duration(seconds: 2));
await keyPressSimulator.simulateKeyDown(
PhysicalKeyboardKey.keyK,
[],
core.settings.getTrainerApp()?.packageName,
);
await keyPressSimulator.simulateKeyUp(
PhysicalKeyboardKey.keyK,
[],
core.settings.getTrainerApp()?.packageName,
);
},
),
MenuButton(
child: Text('Disconnect'),
onPressed: (c) async {
core.connection.disconnectAll();
},
),
MenuButton(
child: Text('Show Paywall'),
onPressed: (c) async {
openDrawer(
context: context,
builder: (c) => Paywall(),
position: OverlayPosition.bottom,
);
},
),
MenuDivider(),
],
if (kDebugMode) ...[
MenuButton(
child: Text('Reset IAP State'),
onPressed: (c) async {
IAPManager.instance.reset(false);
core.settings.init();
},
),
MenuDivider(),
],
MenuButton(
leading: Icon(Icons.tips_and_updates_outlined),
child: Text(context.i18n.onboardingMenuEntry),
onPressed: (c) async {
await Navigator.of(context).push(
MaterialPageRoute(fullscreenDialog: true, builder: (_) => OnboardingPage()),
);
},
),
MenuButton(
leading: Icon(Icons.logo_dev_sharp),
child: Text(context.i18n.logs),
onPressed: (c) async {
await context.push(LogViewer());
},
),
MenuButton(
leading: Icon(Icons.star_rate),
child: Text(context.i18n.leaveAReview),
onPressed: (c) async {
final InAppReview inAppReview = InAppReview.instance;
if (await inAppReview.isAvailable()) {
inAppReview.requestReview();
} else {
inAppReview.openStoreListing(appStoreId: 'id6753721284', microsoftStoreId: '9NP42GS03Z26');
}
},
),
MenuButton(
leading: Icon(Icons.update_outlined),
child: Text(context.i18n.changelog),
onPressed: (c) {
openDrawer(
context: context,
position: OverlayPosition.bottom,
builder: (c) => MarkdownPage(assetPath: 'CHANGELOG.md'),
);
},
),
MenuButton(
leading: Icon(Icons.policy_outlined),
child: Text(context.i18n.license),
onPressed: (c) {
showLicensePage(context: context);
},
),
],
),
),
);
}
}