fix(onboarding): bring the network method up once Local Network is granted

Two gaps, both downstream of the launch-time deferral.

verifyEnabledNetworkMethod returned on success without starting anything, so
granting the permission left the method reading as enabled while advertising
nothing — the launch-time start had already been skipped while the wizard held
the screen, and nothing re-ran it.

The wizard's three exits (done + paywall, done + start riding, welcome/later)
mark onboarding completed and pop, but none started the enabled methods either.
Before the deferral the launch call had already done that; afterwards a method
carried over from a previous session stayed down until the next launch. That
one bites without any permission involved.

startEnabledConnectionMethod gains userInitiated, which opts out of the
deferral: app launch and trainer_setup keep the default and still hold off
while the wizard owns the screen, while the grant path and the three exits pass
true. The existing !isStarted.value guards mean nothing double-starts.

The test asserts the mDNS registration attempt actually reaches the channel,
rather than a proxy for it: the first version asserted the method stayed
enabled and failed even with the fix, because there is no nsd plugin under
`flutter test` and startServer()'s catchError switches the setting back off.
Checked both ways — without the start it fails Expected: non-empty / Actual: [].

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jonas Bark
2026-08-21 10:55:26 +02:00
parent a488b0169f
commit f99052d420
4 changed files with 92 additions and 3 deletions

View File

@@ -137,7 +137,14 @@ Future<void> 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);
}

View File

@@ -779,6 +779,7 @@ class _OnboardingPageState extends State<OnboardingPage> {
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<OnboardingPage> {
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<OnboardingPage> {
Future<void> _onWelcomeLater() async {
try {
await core.settings.setOnboardingState(Settings.onboardingStateCompleted);
core.logic.startEnabledConnectionMethod(userInitiated: true);
} catch (e, s) {
recordError(e, s, context: 'onboarding welcome later');
}

View File

@@ -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 &&

View File

@@ -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<void> 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 = <String>[];
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));
}