feat(app): configure and safely switch multi-address servers
This commit is contained in:
@@ -94,6 +94,7 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
|
||||
/// The destination that was open before Search, restored on close.
|
||||
AppRoute _searchReturn;
|
||||
bool _searchTransient = false;
|
||||
|
||||
/// The Settings route the Templates page closes back to.
|
||||
AppRoute _templatesReturn;
|
||||
@@ -210,10 +211,12 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
if (searchOpen) return;
|
||||
_searchReturn = _route;
|
||||
goTo(AppRoute.search);
|
||||
_searchTransient = true;
|
||||
}
|
||||
|
||||
void closeSearch() {
|
||||
if (!searchOpen) return;
|
||||
_searchTransient = false;
|
||||
goTo(_searchReturn);
|
||||
}
|
||||
|
||||
@@ -227,6 +230,7 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
Future<void> setNewRoutePath(AppRoute configuration) async {
|
||||
if (configuration == _route) return;
|
||||
_rootExit.clear();
|
||||
_searchTransient = false;
|
||||
_route = configuration;
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -239,7 +243,7 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
Future<bool> popRoute() async {
|
||||
final navigator = _navigatorKey.currentState;
|
||||
if (navigator != null && await navigator.maybePop()) return true;
|
||||
if (searchOpen) {
|
||||
if (searchOpen && (searchBuilder != null || _searchTransient)) {
|
||||
closeSearch();
|
||||
return true;
|
||||
}
|
||||
@@ -264,6 +268,7 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
AppRouteKind.runs ||
|
||||
AppRouteKind.chat ||
|
||||
AppRouteKind.settings => true,
|
||||
AppRouteKind.search => searchBuilder == null && !_searchTransient,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
@@ -306,6 +311,7 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
void goTo(AppRoute route) {
|
||||
if (route == _route) return;
|
||||
_rootExit.clear();
|
||||
_searchTransient = false;
|
||||
_route = route;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -25,7 +25,9 @@ class AppSession extends ChangeNotifier {
|
||||
required Uri? endpoint,
|
||||
PipelinesClient? client,
|
||||
bool knownServer = false,
|
||||
List<Uri>? addresses,
|
||||
}) : _initialEndpoint = endpoint,
|
||||
_initialAddresses = addresses,
|
||||
_client = client ?? PipelinesClient(),
|
||||
_everConnected = knownServer {
|
||||
_states = _client.states.listen(_onState);
|
||||
@@ -33,6 +35,7 @@ class AppSession extends ChangeNotifier {
|
||||
}
|
||||
|
||||
final Uri? _initialEndpoint;
|
||||
final List<Uri>? _initialAddresses;
|
||||
final PipelinesClient _client;
|
||||
late final StreamSubscription<ConnectionState> _states;
|
||||
late final StreamSubscription<JsonMap> _frames;
|
||||
@@ -69,7 +72,10 @@ class AppSession extends ChangeNotifier {
|
||||
/// so this is a no-op rather than a guessed endpoint.
|
||||
Future<void> start() async {
|
||||
if (_disposed || endpoint == null) return;
|
||||
await _client.connect(endpoint!);
|
||||
final addresses = _client.endpoints.isNotEmpty
|
||||
? _client.endpoints
|
||||
: _initialAddresses ?? [endpoint!];
|
||||
await _client.connectAny(addresses);
|
||||
}
|
||||
|
||||
/// Retries now: reopens a disconnected socket, or repeats a refused watch on
|
||||
|
||||
@@ -3,7 +3,6 @@ import 'package:flutter/foundation.dart';
|
||||
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';
|
||||
@@ -55,12 +54,10 @@ Future<void> runPipelinesApp({
|
||||
settingsStore: settingsStore,
|
||||
);
|
||||
runApp(
|
||||
kIsWeb
|
||||
? PipelinesApp(server: server)
|
||||
: PipelinesRoot(
|
||||
server: server,
|
||||
buildDefault: configured,
|
||||
signedIn: ServerSession.cookie != null,
|
||||
),
|
||||
PipelinesRoot(
|
||||
server: server,
|
||||
buildDefault: configured,
|
||||
signedIn: kIsWeb || ServerSession.cookie != null,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:flutter/scheduler.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../core/api_gateway.dart';
|
||||
import '../core/servers/server_scope.dart';
|
||||
import '../core/transport/pipelines_client.dart';
|
||||
import '../shell/adaptive_shell.dart';
|
||||
import '../shell/destination.dart';
|
||||
@@ -16,6 +17,8 @@ import '../features/attention/runtime/attention_store.dart';
|
||||
import '../features/runs/ui/run_search_selection.dart';
|
||||
import '../features/search/domain/search_target.dart';
|
||||
import '../features/settings/settings_screen.dart';
|
||||
import '../features/settings/settings_store.dart';
|
||||
import '../features/settings/server_connections.dart';
|
||||
import 'app.dart';
|
||||
import 'app_route.dart';
|
||||
import 'app_router.dart';
|
||||
@@ -49,6 +52,11 @@ class PipelinesApp extends StatefulWidget {
|
||||
this.cupertino,
|
||||
this.routeInformationProvider,
|
||||
this.knownServer = false,
|
||||
this.serverAddresses,
|
||||
this.serverId,
|
||||
this.servers,
|
||||
this.resetRoute = false,
|
||||
this.legacyPendingEndpoints = const [],
|
||||
});
|
||||
|
||||
final ServerEndpoint server;
|
||||
@@ -60,6 +68,11 @@ class PipelinesApp extends StatefulWidget {
|
||||
|
||||
/// The device has signed in to [server] before (see [AppSession]).
|
||||
final bool knownServer;
|
||||
final List<Uri>? serverAddresses;
|
||||
final String? serverId;
|
||||
final ServerConnections? servers;
|
||||
final bool resetRoute;
|
||||
final List<String> legacyPendingEndpoints;
|
||||
|
||||
@override
|
||||
State<PipelinesApp> createState() => _PipelinesAppState();
|
||||
@@ -97,7 +110,9 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
/// One notification capability for the app: the Runs attention surface
|
||||
/// publishes live transitions and Settings renders the same permission.
|
||||
late final NotificationController _notifications = NotificationController(
|
||||
store: const SharedPreferencesNotificationStore(),
|
||||
store: SharedPreferencesNotificationStore(
|
||||
serverId: serverStorageScope(widget.serverId),
|
||||
),
|
||||
);
|
||||
ChatQuestionNotices? _chatQuestionNotices;
|
||||
|
||||
@@ -107,7 +122,9 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
/// `run_event` frames are folded here so the count moves while another
|
||||
/// destination is shown, and a replaced socket drops the status baseline.
|
||||
late final AttentionController _attention = AttentionController(
|
||||
store: const SharedPreferencesAttentionStore(),
|
||||
store: SharedPreferencesAttentionStore(
|
||||
serverId: serverStorageScope(widget.serverId),
|
||||
),
|
||||
);
|
||||
StreamSubscription<JsonMap>? _attentionFrames;
|
||||
int _attentionEpoch = 0;
|
||||
@@ -123,12 +140,16 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
endpoint: widget.server.uri,
|
||||
client: widget.client,
|
||||
knownServer: widget.knownServer,
|
||||
addresses: widget.serverAddresses,
|
||||
);
|
||||
bindServerIdentity(_session.gateway, widget.serverId);
|
||||
_ownsProvider = widget.routeInformationProvider == null;
|
||||
_provider =
|
||||
widget.routeInformationProvider ??
|
||||
PlatformRouteInformationProvider(
|
||||
initialRouteInformation: initialRouteInformation(),
|
||||
initialRouteInformation: widget.resetRoute
|
||||
? RouteInformation(uri: Uri.parse('/pipelines'))
|
||||
: initialRouteInformation(),
|
||||
);
|
||||
// A malformed or unknown boot path renders Pipelines, so make the address
|
||||
// bar agree with the rendered destination instead of showing a dead link.
|
||||
@@ -143,7 +164,9 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
);
|
||||
}
|
||||
_cupertino = widget.cupertino ?? _platformIsCupertino;
|
||||
_notifications.onOpenRun = (id) => _delegate.goTo(AppRoute.run(id));
|
||||
_notifications.onOpenRun = (id) {
|
||||
if (mounted) _delegate.goTo(AppRoute.run(id));
|
||||
};
|
||||
unawaited(_notifications.load());
|
||||
unawaited(_attention.load());
|
||||
_attentionFrames = _session.gateway.frames
|
||||
@@ -171,7 +194,9 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
return route.kind == AppRouteKind.chat ? route.chatSessionId : null;
|
||||
},
|
||||
sessionTitle: _chatSessionTitle,
|
||||
openChat: (id) => _delegate.goTo(AppRoute.chatSession(id)),
|
||||
openChat: (id) {
|
||||
if (mounted) _delegate.goTo(AppRoute.chatSession(id));
|
||||
},
|
||||
bridge: _notifications.bridge,
|
||||
);
|
||||
_config = RouterConfig<AppRoute>(
|
||||
@@ -432,6 +457,7 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
AppDestination.pipelines: (_) => PipelinesDestination(
|
||||
session: _session,
|
||||
server: widget.server,
|
||||
legacyPendingEndpoints: widget.legacyPendingEndpoints,
|
||||
refreshToken: _pipelinesRefresh,
|
||||
onRunStarted: _runStarted,
|
||||
onViewPipelineRuns: (id) =>
|
||||
@@ -501,6 +527,10 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
showAndroidDownload: kIsWeb,
|
||||
initialEndpoint: _session.endpoint,
|
||||
embedded: true,
|
||||
servers: widget.servers,
|
||||
store: SharedPreferencesSettingsStore(
|
||||
serverId: widget.serverId ?? '',
|
||||
),
|
||||
onOpenTemplates: _delegate.openTemplates,
|
||||
notifications: _notifications,
|
||||
),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../core/servers/server_scope.dart';
|
||||
|
||||
import '../features/pipelines/screen.dart';
|
||||
import '../ui/adaptive/host.dart';
|
||||
@@ -25,6 +26,7 @@ class PipelinesDestination extends StatelessWidget {
|
||||
this.initialPipelineId,
|
||||
this.initialVersion = 0,
|
||||
this.initialStageKey,
|
||||
this.legacyPendingEndpoints = const [],
|
||||
});
|
||||
|
||||
final AppSession session;
|
||||
@@ -47,6 +49,7 @@ class PipelinesDestination extends StatelessWidget {
|
||||
|
||||
/// The definition stage a deep link names, or null for no stage request.
|
||||
final String? initialStageKey;
|
||||
final List<String> legacyPendingEndpoints;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => AnimatedBuilder(
|
||||
@@ -81,7 +84,10 @@ class PipelinesDestination extends StatelessWidget {
|
||||
|
||||
Widget _pipelinesScreen() => PipelinesScreen(
|
||||
gateway: session.gateway,
|
||||
endpoint: server.uri?.toString() ?? '',
|
||||
endpoint: serverIdentity(session.gateway) == null
|
||||
? server.uri?.toString() ?? ''
|
||||
: 'profile:${serverIdentity(session.gateway)}',
|
||||
legacyEndpoints: legacyPendingEndpoints,
|
||||
embedded: true,
|
||||
onRunStarted: onRunStarted,
|
||||
onViewPipelineRuns: onViewPipelineRuns,
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import '../core/session/server_session.dart';
|
||||
import '../core/servers/server_catalog.dart';
|
||||
import '../core/servers/server_catalog_store.dart';
|
||||
import '../core/servers/server_probe.dart';
|
||||
import '../core/transport/pipelines_client.dart';
|
||||
import '../features/onboarding/onboarding_flow.dart';
|
||||
import '../features/settings/server_connections.dart';
|
||||
import '../features/settings/saved_servers_section.dart';
|
||||
import '../features/settings/settings_store.dart';
|
||||
import '../ui/theme/app_theme.dart';
|
||||
import 'pipelines_app.dart';
|
||||
@@ -20,6 +27,9 @@ class PipelinesRoot extends StatefulWidget {
|
||||
this.signedIn = false,
|
||||
this.check = ServerSession.check,
|
||||
this.settingsStore = const SharedPreferencesSettingsStore(),
|
||||
this.catalogStore = const SharedPreferencesServerCatalogStore(),
|
||||
this.web = kIsWeb,
|
||||
this.clientFactory,
|
||||
});
|
||||
|
||||
final ServerEndpoint server;
|
||||
@@ -31,6 +41,9 @@ class PipelinesRoot extends StatefulWidget {
|
||||
final bool signedIn;
|
||||
final ServerCheck check;
|
||||
final SettingsStore settingsStore;
|
||||
final ServerCatalogStore catalogStore;
|
||||
final bool web;
|
||||
final PipelinesClient Function()? clientFactory;
|
||||
|
||||
@override
|
||||
State<PipelinesRoot> createState() => _PipelinesRootState();
|
||||
@@ -41,24 +54,148 @@ class _PipelinesRootState extends State<PipelinesRoot> {
|
||||
late bool _onboarding = widget.server.uri == null || !widget.signedIn;
|
||||
bool _refused = false;
|
||||
int _generation = 0;
|
||||
bool _booting = true;
|
||||
int _activation = 0;
|
||||
PipelinesClient? _client;
|
||||
SavedServer? _activeServer;
|
||||
late final ServerConnections _servers = ServerConnections(
|
||||
store: widget.catalogStore,
|
||||
onSelected: _selectServer,
|
||||
);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (!_onboarding) _verify(widget.server.uri!);
|
||||
_loadServers();
|
||||
}
|
||||
|
||||
Future<void> _loadServers() async {
|
||||
await _servers.load(legacyEndpoint: widget.server.uri?.toString() ?? '');
|
||||
if (!mounted) return;
|
||||
if (widget.web) {
|
||||
if (widget.server.uri != null) {
|
||||
_servers.alignBrowserOrigin(widget.server.uri!);
|
||||
}
|
||||
setState(() {
|
||||
_booting = false;
|
||||
_onboarding = false;
|
||||
_client = widget.clientFactory?.call();
|
||||
});
|
||||
return;
|
||||
}
|
||||
final persisted =
|
||||
_servers.catalog.active == null || await _servers.persist();
|
||||
if (!mounted) return;
|
||||
if (!persisted) {
|
||||
ServerSession.clearSelection();
|
||||
setState(() {
|
||||
_booting = false;
|
||||
_onboarding = true;
|
||||
});
|
||||
return;
|
||||
}
|
||||
await _activate(_servers.catalog.active, initial: true);
|
||||
}
|
||||
|
||||
Future<void> _selectServer(SavedServer? server) async {
|
||||
if (widget.web) {
|
||||
if (server == null) return;
|
||||
final endpoint = Uri.parse(server.endpoints.first);
|
||||
if (endpoint == _server.uri) {
|
||||
_servers.alignBrowserOrigin(endpoint);
|
||||
return;
|
||||
}
|
||||
final page = endpoint.replace(
|
||||
scheme: endpoint.scheme == 'wss' ? 'https' : 'http',
|
||||
path: '/',
|
||||
query: null,
|
||||
fragment: null,
|
||||
);
|
||||
if (!await launchUrl(page, webOnlyWindowName: '_self')) {
|
||||
if (_server.uri != null) _servers.alignBrowserOrigin(_server.uri!);
|
||||
throw StateError('Could not open the selected server.');
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (server != null &&
|
||||
server.id == _activeServer?.id &&
|
||||
listEquals(server.endpoints, _activeServer?.endpoints)) {
|
||||
_activeServer = server;
|
||||
return;
|
||||
}
|
||||
await _activate(server);
|
||||
}
|
||||
|
||||
Future<void> _activate(SavedServer? server, {bool initial = false}) async {
|
||||
final token = ++_activation;
|
||||
if (!initial) {
|
||||
setState(() => _booting = true);
|
||||
// Dispose the old app/socket before changing the credential namespace.
|
||||
await WidgetsBinding.instance.endOfFrame;
|
||||
}
|
||||
if (!mounted || token != _activation) return;
|
||||
if (server == null) {
|
||||
ServerSession.clearSelection();
|
||||
} else {
|
||||
await ServerSession.activateServer(
|
||||
id: server.id,
|
||||
endpoints: server.endpoints.map(Uri.parse),
|
||||
migrateLegacy: server.id == 'legacy-server',
|
||||
);
|
||||
}
|
||||
if (!mounted || token != _activation) return;
|
||||
_activeServer = server;
|
||||
setState(() {
|
||||
_server = ServerEndpoint.resolve(
|
||||
base: Uri.base,
|
||||
web: false,
|
||||
configured: server?.endpoints.first ?? '',
|
||||
);
|
||||
_onboarding =
|
||||
server == null ||
|
||||
(ServerSession.cookie == null &&
|
||||
!(initial && widget.signedIn && server.id == 'legacy-server'));
|
||||
_refused = false;
|
||||
_generation++;
|
||||
_booting = false;
|
||||
_client = _onboarding ? null : widget.clientFactory?.call();
|
||||
});
|
||||
if (!_onboarding && _server.uri != null) {
|
||||
_verify(_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;
|
||||
final token = _activation;
|
||||
final result = await probeServerAddresses(
|
||||
[
|
||||
endpoint,
|
||||
...?_activeServer?.endpoints
|
||||
.map(Uri.parse)
|
||||
.where((uri) => uri != endpoint),
|
||||
],
|
||||
check: widget.check,
|
||||
isCurrent: () => mounted && token == _activation,
|
||||
);
|
||||
if (!mounted ||
|
||||
token != _activation ||
|
||||
result.state != SessionState.signedOut) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_refused = true;
|
||||
_onboarding = true;
|
||||
_server = ServerEndpoint.resolve(
|
||||
base: Uri.base,
|
||||
web: false,
|
||||
configured: result.endpoint.toString(),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
void _done(Uri endpoint) {
|
||||
_activeServer = _servers.catalog.active;
|
||||
setState(() {
|
||||
_server = ServerEndpoint.resolve(
|
||||
base: Uri.base,
|
||||
@@ -68,27 +205,93 @@ class _PipelinesRootState extends State<PipelinesRoot> {
|
||||
_onboarding = false;
|
||||
_refused = false;
|
||||
_generation++;
|
||||
_client = widget.clientFactory?.call();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _saveOnboardingServer(String url) async {
|
||||
final token = _activation;
|
||||
final active = _servers.catalog.active;
|
||||
SavedServer? target;
|
||||
for (final server in _servers.catalog.servers) {
|
||||
if (server.endpoints.contains(url)) target = server;
|
||||
}
|
||||
target ??= SavedServer.validated(
|
||||
id: 'server-${DateTime.now().microsecondsSinceEpoch}',
|
||||
name: active == null ? 'My server' : 'New server',
|
||||
endpoints: [url],
|
||||
);
|
||||
if (!await _servers.save(target, activate: false) ||
|
||||
!await _servers.select(target.id, activate: false)) {
|
||||
throw StateError(_servers.error ?? 'Could not save the server.');
|
||||
}
|
||||
if (!mounted || token != _activation) {
|
||||
throw StateError('The selected server changed.');
|
||||
}
|
||||
await ServerSession.activateServer(
|
||||
id: target.id,
|
||||
endpoints: target.endpoints.map(Uri.parse),
|
||||
migrateLegacy: target.id == 'legacy-server',
|
||||
);
|
||||
await widget.settingsStore.saveServerUrl(url);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_activation++;
|
||||
_servers.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
bool get _cupertino =>
|
||||
defaultTargetPlatform == TargetPlatform.iOS ||
|
||||
defaultTargetPlatform == TargetPlatform.macOS;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_booting) {
|
||||
// A temporary App/Navigator would replace the browser's deep-link URL
|
||||
// before the real Router reads it. Loading must not own navigation.
|
||||
return MediaQuery.fromView(
|
||||
view: View.of(context),
|
||||
child: Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: _cupertino
|
||||
? const CupertinoTheme(
|
||||
data: appCupertinoTheme,
|
||||
child: Center(child: CupertinoActivityIndicator()),
|
||||
)
|
||||
: Theme(
|
||||
data: appLightTheme(),
|
||||
child: const Center(child: CircularProgressIndicator()),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (!_onboarding) {
|
||||
return PipelinesApp(
|
||||
key: ValueKey(_generation),
|
||||
server: _server,
|
||||
client: _client,
|
||||
knownServer: true,
|
||||
serverAddresses: widget.web
|
||||
? null
|
||||
: _servers.catalog.active?.endpoints.map(Uri.parse).toList(),
|
||||
serverId: _servers.catalog.active?.id,
|
||||
servers: _servers,
|
||||
legacyPendingEndpoints: [?_servers.catalog.active?.legacyEndpoint],
|
||||
resetRoute: !widget.web && _generation > 1,
|
||||
);
|
||||
}
|
||||
final flow = OnboardingFlow(
|
||||
initialServer: _server.uri?.toString() ?? widget.buildDefault,
|
||||
startAtPassword: _refused,
|
||||
saveServer: widget.settingsStore.saveServerUrl,
|
||||
saveServer: _saveOnboardingServer,
|
||||
onDone: _done,
|
||||
savedServers: SavedServersSection(connections: _servers),
|
||||
serverAddresses:
|
||||
_servers.catalog.active?.endpoints.map(Uri.parse).toList() ??
|
||||
const [],
|
||||
);
|
||||
return _cupertino
|
||||
? CupertinoApp(
|
||||
|
||||
Reference in New Issue
Block a user