import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../core/servers/server_catalog.dart'; import '../../ui/adaptive/host.dart'; import 'saved_server_editor.dart'; import 'server_connections.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}); final ServerConnections connections; Future _edit(BuildContext context, [SavedServer? server]) async { await editSavedServer( context, server: server, 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); } Widget _button(BuildContext context, String label, VoidCallback? press) => usesCupertinoHost(context) ? CupertinoButton(onPressed: press, child: Text(label)) : TextButton(onPressed: press, child: Text(label)); @override Widget build(BuildContext context) => ListenableBuilder( listenable: connections, builder: (context, _) => Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ for (final server in connections.catalog.servers) ...[ Text( server.name, style: const TextStyle(fontWeight: FontWeight.w600), ), if (server.id == connections.catalog.active?.id) const Text('Selected server'), for (final (index, address) in server.endpoints.indexed) Text('${index == 0 ? 'Preferred' : 'Backup $index'}: $address'), Wrap( spacing: 8, children: [ if (server.id != connections.catalog.active?.id) _button( context, 'Use ${server.name}', connections.busy ? null : () => unawaited(connections.select(server.id)), ), _button( context, 'Edit ${server.name}', connections.busy ? null : () => unawaited(_edit(context, server)), ), _button( context, 'Remove ${server.name}', connections.busy ? null : () => unawaited(_remove(context, server)), ), ], ), const SizedBox(height: 12), ], Align( alignment: AlignmentDirectional.centerStart, child: _button( context, 'Add server', connections.busy ? null : () => unawaited(_edit(context)), ), ), if (connections.error != null) Text( connections.error!, style: TextStyle( color: usesCupertinoHost(context) ? CupertinoColors.systemRed.resolveFrom(context) : Theme.of(context).colorScheme.error, ), ), ], ), ); }