diff --git a/lib/pages/onboarding/onboarding_methods.dart b/lib/pages/onboarding/onboarding_methods.dart index 4f1af4ee..720d720f 100644 --- a/lib/pages/onboarding/onboarding_methods.dart +++ b/lib/pages/onboarding/onboarding_methods.dart @@ -137,7 +137,14 @@ Future verifyEnabledNetworkMethod( if (!onboardingMethodEnabled(OnboardingMethod.network, app)) return; final requirements = localNetworkRequirements(); if (requirements.isEmpty) return; - if (await _satisfy(context, requirements)) return; + if (await _satisfy(context, requirements)) { + // Granted — actually bring the method up. The launch-time start was skipped + // while the wizard held the screen, so without this the method reads as + // enabled but advertises nothing. + core.logic.startEnabledConnectionMethod(userInitiated: true); + onUpdate(); + return; + } if (!context.mounted) return; await setOnboardingMethodEnabled(context, OnboardingMethod.network, app, false, onUpdate: onUpdate); } diff --git a/lib/pages/onboarding/onboarding_page.dart b/lib/pages/onboarding/onboarding_page.dart index ec931da7..9ac75d1e 100644 --- a/lib/pages/onboarding/onboarding_page.dart +++ b/lib/pages/onboarding/onboarding_page.dart @@ -779,6 +779,7 @@ class _OnboardingPageState extends State { onPressed: () async { try { await core.settings.setOnboardingState(Settings.onboardingStateCompleted); + core.logic.startEnabledConnectionMethod(userInitiated: true); if (!mounted || !context.mounted) return; // Platform-correct paywall: RevenueCat's hosted sheet on // iOS/Android, the in-app Paywall drawer on desktop. Going @@ -801,6 +802,9 @@ class _OnboardingPageState extends State { onPressed: () async { try { await core.settings.setOnboardingState(Settings.onboardingStateCompleted); + // The launch-time start was skipped while the wizard held the + // screen; leaving it is when the enabled methods must come up. + core.logic.startEnabledConnectionMethod(userInitiated: true); if (context.mounted) Navigator.of(context).pop(); } catch (e, s) { recordError(e, s, context: 'onboarding done start riding'); @@ -816,6 +820,7 @@ class _OnboardingPageState extends State { Future _onWelcomeLater() async { try { await core.settings.setOnboardingState(Settings.onboardingStateCompleted); + core.logic.startEnabledConnectionMethod(userInitiated: true); } catch (e, s) { recordError(e, s, context: 'onboarding welcome later'); } diff --git a/lib/utils/core.dart b/lib/utils/core.dart index 1c89a15c..d062e57a 100644 --- a/lib/utils/core.dart +++ b/lib/utils/core.dart @@ -489,7 +489,11 @@ class CoreLogic { } } - void startEnabledConnectionMethod() async { + /// [userInitiated] opts out of the onboarding deferral below. Use it when the + /// rider just did something that should visibly take effect — granting a + /// permission, finishing the wizard — as opposed to the unsolicited call at + /// app launch. + void startEnabledConnectionMethod({bool userInitiated = false}) async { if (screenshotMode) { return; } @@ -497,7 +501,7 @@ class CoreLogic { // setOnboardingMethodEnabled, which starts servers directly), so nothing is // lost by holding off — and a permission prompt or warning toast arriving // from behind it explains nothing. - if (deferLaunchPermissions) { + if (!userInitiated && deferLaunchPermissions) { return; } if (isZwiftBleEnabled && diff --git a/test/onboarding_methods_test.dart b/test/onboarding_methods_test.dart index dae8f1b9..2bb79ef9 100644 --- a/test/onboarding_methods_test.dart +++ b/test/onboarding_methods_test.dart @@ -9,9 +9,11 @@ import 'package:bike_control/main.dart' show OtherLocalizationsDelegate, screens import 'package:bike_control/pages/onboarding/onboarding_methods.dart'; import 'package:bike_control/services/local_network_access.dart'; import 'package:bike_control/utils/core.dart'; +import 'package:bike_control/utils/settings/settings.dart'; import 'package:bike_control/utils/keymap/apps/supported_app.dart'; import 'package:bike_control/utils/requirements/multi.dart'; import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:local_network_permission/local_network_permission.dart'; import 'package:shadcn_flutter/shadcn_flutter.dart'; @@ -191,4 +193,75 @@ Future main() async { } // Local Network is Apple-only; localNetworkRequirements() is empty elsewhere. }, skip: !(Platform.isMacOS || Platform.isIOS)); + + testWidgets('granting Local Network brings the network method up', (tester) async { + // Regression: the permission was granted and the method stayed enabled, but + // nothing advertised — the launch-time start had been skipped while the + // wizard held the screen, and the grant path never started it. + screenshotMode = false; + addTearDown(() => screenshotMode = true); + LocalNetworkAccess.resetForTest(); + addTearDown(LocalNetworkAccess.resetForTest); + tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( + LocalNetworkPermission.channel, + (call) async => call.method == 'check' ? 'granted' : null, + ); + addTearDown( + () => tester.binding.defaultBinaryMessenger + .setMockMethodCallHandler(LocalNetworkPermission.channel, null), + ); + debugDefaultTargetPlatformOverride = TargetPlatform.macOS; + + // There is no nsd plugin under `flutter test`; record the advertise attempt + // rather than let the emulator's catchError undo the setting. + final mdns = []; + tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( + const MethodChannel('com.haberey/nsd'), + (call) async { + mdns.add(call.method); + return null; + }, + ); + addTearDown(() => tester.binding.defaultBinaryMessenger + .setMockMethodCallHandler(const MethodChannel('com.haberey/nsd'), null)); + + // The wizard is still on screen, so the launch-time start is deferred. + await core.settings.setOnboardingState(Settings.onboardingStatePending); + addTearDown(() => core.settings.setOnboardingState(Settings.onboardingStateCompleted)); + + final app = SupportedApp.supportedApps.firstWhere( + (a) => onboardingMethodVisible(OnboardingMethod.network, a), + ); + core.settings.setTrainerApp(app); + core.settings.setObpMdnsEnabled(true); + core.settings.setZwiftMdnsEmulatorEnabled(true); + + late BuildContext ctx; + await tester.pumpWidget( + ShadcnApp( + localizationsDelegates: [ + ...ShadcnLocalizations.localizationsDelegates, + const OtherLocalizationsDelegate(), + AppLocalizations.delegate, + ], + supportedLocales: AppLocalizations.delegate.supportedLocales, + home: Scaffold(child: Builder(builder: (c) { + ctx = c; + return const SizedBox.shrink(); + })), + ), + ); + + try { + var updates = 0; + await verifyEnabledNetworkMethod(ctx, app, onUpdate: () => updates++); + await tester.pump(const Duration(milliseconds: 50)); + + expect(mdns, isNotEmpty, + reason: 'granting the permission must bring the method up and advertise over mDNS'); + expect(updates, greaterThan(0), reason: 'the tile has to be told to redraw'); + } finally { + debugDefaultTargetPlatformOverride = null; + } + }, skip: !(Platform.isMacOS || Platform.isIOS)); }