feat(app): phone onboarding (server, then password) kept across launches; a known server opens straight into the app; a refused session asks for the password

This commit is contained in:
2026-10-23 23:52:34 +01:00
parent 57504b73d4
commit 636115bb22
12 changed files with 650 additions and 33 deletions
+12 -4
View File
@@ -18,9 +18,16 @@ enum RunSubscriptionState { pending, live, refused }
/// `subscribe` on every connected transition because a new socket has no
/// watch, and drops it deliberately with `unsubscribe` when the app stops.
class AppSession extends ChangeNotifier {
AppSession({required Uri? endpoint, PipelinesClient? client})
: _initialEndpoint = endpoint,
_client = client ?? PipelinesClient() {
/// [knownServer]: this device has signed in to [endpoint] before, so the
/// app opens straight away and reconnects behind a banner instead of the
/// full connection screen.
AppSession({
required Uri? endpoint,
PipelinesClient? client,
bool knownServer = false,
}) : _initialEndpoint = endpoint,
_client = client ?? PipelinesClient(),
_everConnected = knownServer {
_states = _client.states.listen(_onState);
_frames = _client.frames.listen(_onFrame);
}
@@ -30,7 +37,8 @@ class AppSession extends ChangeNotifier {
late final StreamSubscription<ConnectionState> _states;
late final StreamSubscription<JsonMap> _frames;
ConnectionState _connection = const ConnectionState(ConnectionKind.idle);
bool _subscribed = false, _disposed = false, _everConnected = false;
bool _subscribed = false, _disposed = false;
bool _everConnected;
int _connectionEpoch = 0;
RunSubscriptionState _subscription = RunSubscriptionState.pending;
String _subscriptionError = '';
+16 -6
View File
@@ -4,14 +4,15 @@ import 'package:flutter/widgets.dart';
import 'app_url_strategy.dart';
import 'pipelines_app.dart';
import 'pipelines_root.dart';
import 'server_endpoint.dart';
import '../features/settings/settings_store.dart';
/// Native builds must supply their server explicitly; there is no baked host.
const configuredServerUrl = String.fromEnvironment('PIPELINES_SERVER_URL');
/// Resolves the launch endpoint, loading the last saved native endpoint only
/// when no build-time endpoint was supplied.
/// Resolves the launch endpoint: on a phone the server saved at onboarding,
/// else the build's default (which only pre-fills onboarding until saved).
Future<ServerEndpoint> resolveLaunchServer({
Uri? base,
String configured = configuredServerUrl,
@@ -20,14 +21,15 @@ Future<ServerEndpoint> resolveLaunchServer({
}) async {
WidgetsFlutterBinding.ensureInitialized();
var endpoint = configured;
if (!web && endpoint.trim().isEmpty) {
if (!web) {
try {
endpoint =
final saved =
(await (settingsStore ?? const SharedPreferencesSettingsStore())
.load())
.serverUrl;
if (saved.trim().isNotEmpty) endpoint = saved;
} catch (_) {
endpoint = '';
// Keep the build's default.
}
}
return ServerEndpoint.resolve(
@@ -52,5 +54,13 @@ Future<void> runPipelinesApp({
configured: configured,
settingsStore: settingsStore,
);
runApp(PipelinesApp(server: server));
runApp(
kIsWeb
? PipelinesApp(server: server)
: PipelinesRoot(
server: server,
buildDefault: configured,
signedIn: ServerSession.cookie != null,
),
);
}
+9 -1
View File
@@ -48,6 +48,7 @@ class PipelinesApp extends StatefulWidget {
this.client,
this.cupertino,
this.routeInformationProvider,
this.knownServer = false,
});
final ServerEndpoint server;
@@ -57,6 +58,9 @@ class PipelinesApp extends StatefulWidget {
/// Test seam: the Router's initial and subsequent route information.
final RouteInformationProvider? routeInformationProvider;
/// The device has signed in to [server] before (see [AppSession]).
final bool knownServer;
@override
State<PipelinesApp> createState() => _PipelinesAppState();
}
@@ -115,7 +119,11 @@ class _PipelinesAppState extends State<PipelinesApp>
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_session = AppSession(endpoint: widget.server.uri, client: widget.client);
_session = AppSession(
endpoint: widget.server.uri,
client: widget.client,
knownServer: widget.knownServer,
);
_ownsProvider = widget.routeInformationProvider == null;
_provider =
widget.routeInformationProvider ??
+108
View File
@@ -0,0 +1,108 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../core/session/server_session.dart';
import '../features/onboarding/onboarding_flow.dart';
import '../features/settings/settings_store.dart';
import '../ui/theme/app_theme.dart';
import 'pipelines_app.dart';
import 'server_endpoint.dart';
/// The phone app's root: onboarding until a server and its password are
/// known, then the app. A saved session the server later refuses brings the
/// password step back; an unreachable server just reconnects in the app.
class PipelinesRoot extends StatefulWidget {
const PipelinesRoot({
super.key,
required this.server,
this.buildDefault = '',
this.signedIn = false,
this.check = ServerSession.check,
this.settingsStore = const SharedPreferencesSettingsStore(),
});
final ServerEndpoint server;
/// The server this build was made for; it pre-fills onboarding.
final String buildDefault;
/// A session is kept on this device.
final bool signedIn;
final ServerCheck check;
final SettingsStore settingsStore;
@override
State<PipelinesRoot> createState() => _PipelinesRootState();
}
class _PipelinesRootState extends State<PipelinesRoot> {
late ServerEndpoint _server = widget.server;
late bool _onboarding = widget.server.uri == null || !widget.signedIn;
bool _refused = false;
int _generation = 0;
@override
void initState() {
super.initState();
if (!_onboarding) _verify(widget.server.uri!);
}
/// A kept session the server now refuses asks for the password again.
Future<void> _verify(Uri endpoint) async {
final state = await widget.check(endpoint);
if (!mounted || state != SessionState.signedOut) return;
setState(() {
_refused = true;
_onboarding = true;
});
}
void _done(Uri endpoint) {
setState(() {
_server = ServerEndpoint.resolve(
base: Uri.base,
web: false,
configured: endpoint.toString(),
);
_onboarding = false;
_refused = false;
_generation++;
});
}
bool get _cupertino =>
defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.macOS;
@override
Widget build(BuildContext context) {
if (!_onboarding) {
return PipelinesApp(
key: ValueKey(_generation),
server: _server,
knownServer: true,
);
}
final flow = OnboardingFlow(
initialServer: _server.uri?.toString() ?? widget.buildDefault,
startAtPassword: _refused,
saveServer: widget.settingsStore.saveServerUrl,
onDone: _done,
);
return _cupertino
? CupertinoApp(
title: 'Pipelines',
theme: appCupertinoTheme,
debugShowCheckedModeBanner: false,
home: flow,
)
: MaterialApp(
title: 'Pipelines',
theme: appLightTheme(),
darkTheme: appDarkTheme(),
debugShowCheckedModeBanner: false,
home: flow,
);
}
}