import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import '../../core/servers/chat/machine_sign_in_dialog.dart'; import '../../core/servers/server_catalog.dart'; import '../../ui/adaptive/host.dart'; import 'saved_server_editor.dart'; import 'saved_server_row.dart'; import 'server_connections.dart'; import 'settings_group.dart'; import 'settings_row.dart'; import 'settings_theme.dart'; class _ServerSaveFailure implements Exception { const _ServerSaveFailure(this.message); final String message; @override String toString() => message; } class SavedServersSection extends StatelessWidget { const SavedServersSection({ super.key, required this.connections, this.allowPrimarySelection, }); final bool? allowPrimarySelection; final ServerConnections connections; Future _edit(BuildContext context, [SavedServer? server]) async { await editSavedServer( context, server: server, authenticator: connections.authenticator, onSave: (edited) async { if (!await connections.save(edited)) { throw _ServerSaveFailure( connections.error ?? 'The server configuration could not be saved.', ); } }, ); } Future _remove(BuildContext context, SavedServer server) async { final cupertino = usesCupertinoHost(context); final confirmed = cupertino ? await showCupertinoDialog( context: context, builder: (context) => CupertinoAlertDialog( title: Text('Remove ${server.name}?'), content: const Text( 'This removes the saved configuration, not data on the server.', ), actions: [ CupertinoDialogAction( onPressed: () => Navigator.pop(context, false), child: const Text('Cancel'), ), CupertinoDialogAction( isDestructiveAction: true, onPressed: () => Navigator.pop(context, true), child: const Text('Remove'), ), ], ), ) : await showDialog( context: context, builder: (context) => AlertDialog( title: Text('Remove ${server.name}?'), content: const Text( 'This removes the saved configuration, not data on the server.', ), actions: [ TextButton( onPressed: () => Navigator.pop(context, false), child: const Text('Cancel'), ), TextButton( onPressed: () => Navigator.pop(context, true), child: const Text('Remove'), ), ], ), ); if (confirmed == true) await connections.remove(server.id); } void _act(BuildContext context, SavedServer server, SavedServerAction act) { switch (act) { case SavedServerAction.use: unawaited(connections.select(server.id)); case SavedServerAction.edit: unawaited(_edit(context, server)); case SavedServerAction.remove: unawaited(_remove(context, server)); case SavedServerAction.signIn: final authenticator = connections.authenticator; if (authenticator == null) return; unawaited( showMachineSignIn( context, name: server.name, signIn: (password) => authenticator.authenticate(server, password), ), ); } } @override Widget build(BuildContext context) => ListenableBuilder( listenable: Listenable.merge([connections, ?connections.authenticator]), builder: (context, _) { final cupertino = usesCupertinoHost(context); final error = connections.error; final allowSelection = allowPrimarySelection ?? !kIsWeb; return SettingsGroup( title: 'Servers', leadingRows: true, footer: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text( 'Each entry is a separate machine with its own password; all of ' 'them connect for chat. Addresses inside one entry are routes ' 'to that same machine.', ), if (allowSelection) const Text('The primary machine is used for pipelines and runs.'), if (error != null) Text(error, style: TextStyle(color: settingsErrorColor(context))), ], ), children: [ for (final server in connections.catalog.servers) SavedServerRow( server: server, status: connections.authenticator?.status(server.id), selected: server.id == connections.catalog.active?.id, allowSelection: allowSelection, primaryContext: true, busy: connections.busy, onAction: (act) => _act(context, server, act), ), SettingsRow( action: true, enabled: !connections.busy, leading: Icon(cupertino ? CupertinoIcons.add : Icons.add), title: const Text('Add server'), onTap: () => unawaited(_edit(context)), ), ], ); }, ); }