import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../ui/adaptive/segmented_bar.dart'; import '../../core/servers/server_catalog.dart'; import '../../ui/adaptive/host.dart'; /// When supplied, [onSave] must finish successfully before the route closes. /// Throwing keeps the draft open and displays the error for a retry. Future editSavedServer( BuildContext context, { SavedServer? server, Future Function(SavedServer)? onSave, }) { final page = _ServerEditor(server: server, onSave: onSave); return Navigator.of(context).push( usesCupertinoHost(context) ? CupertinoPageRoute(builder: (_) => page) : MaterialPageRoute(builder: (_) => page), ); } class _ServerEditor extends StatefulWidget { const _ServerEditor({this.server, this.onSave}); final SavedServer? server; final Future Function(SavedServer)? onSave; @override State<_ServerEditor> createState() => _ServerEditorState(); } class _ServerEditorState extends State<_ServerEditor> { // Keep the identity through retries, including a persisted profile whose // connection activation failed after the write succeeded. late final _id = widget.server?.id ?? 'server-${DateTime.now().microsecondsSinceEpoch}'; late final _name = TextEditingController(text: widget.server?.name ?? ''); late final _addresses = TextEditingController( text: widget.server?.endpoints.join('\n') ?? '', ); String? _error; bool _saving = false; Future _save() async { if (_saving) return; try { final server = SavedServer.validated( id: _id, name: _name.text, legacyEndpoint: widget.server?.legacyEndpoint, endpoints: _addresses.text .split('\n') .where((line) => line.trim().isNotEmpty), ); setState(() { _saving = true; _error = null; }); await widget.onSave?.call(server); if (!mounted) return; Navigator.of(context).pop(server); } catch (error) { if (mounted) { setState(() { _saving = false; _error = '$error'; }); } } } @override void dispose() { _name.dispose(); _addresses.dispose(); super.dispose(); } Widget _field( TextEditingController controller, String label, { required String key, bool multiline = false, }) { if (usesCupertinoHost(context)) { return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text(label), const SizedBox(height: 8), CupertinoTextField( key: Key(key), controller: controller, enabled: !_saving, placeholder: multiline ? 'wss://server.example/connect' : 'Home', minLines: multiline ? 3 : 1, maxLines: multiline ? null : 1, keyboardType: multiline ? TextInputType.multiline : TextInputType.text, autocorrect: !multiline, ), ], ); } return TextField( key: Key(key), controller: controller, enabled: !_saving, decoration: InputDecoration( labelText: label, border: const OutlineInputBorder(), ), minLines: multiline ? 3 : 1, maxLines: multiline ? null : 1, keyboardType: multiline ? TextInputType.multiline : TextInputType.text, autocorrect: !multiline, ); } @override Widget build(BuildContext context) { final cupertino = usesCupertinoHost(context); final title = widget.server == null ? 'Add server' : 'Edit server'; final saveLabel = _saving ? 'Saving server…' : 'Save server'; final save = cupertino ? CupertinoButton.filled( onPressed: _saving ? null : _save, child: Text(saveLabel), ) : FilledButton( onPressed: _saving ? null : _save, child: Text(saveLabel), ); final body = SafeArea( child: ListView( padding: const EdgeInsets.all(16), children: [ _field(_name, 'Server name', key: 'server-profile-name'), const SizedBox(height: 16), _field( _addresses, 'Addresses, in preferred order', key: 'server-profile-addresses', multiline: true, ), const SizedBox(height: 8), const Text( 'One ws:// or wss:// address per line. The first is preferred; ' 'the others are backups for this same server, such as its Tailscale and LAN addresses.', ), const SizedBox(height: 8), const Text( 'Only group addresses you trust as the same server. They share its saved sign-in. ' 'ws:// is unencrypted; use it only on a trusted LAN/VPN.', ), if (_error != null) Padding( padding: const EdgeInsets.only(top: 12), child: Semantics( liveRegion: true, child: Text( _error!, style: TextStyle( color: cupertino ? CupertinoColors.systemRed.resolveFrom(context) : Theme.of(context).colorScheme.error, ), ), ), ), const SizedBox(height: 16), Align(alignment: AlignmentDirectional.centerStart, child: save), ], ), ); return PopScope( canPop: !_saving, child: cupertino ? CupertinoPageScaffold( navigationBar: SegmentedBar(cupertino: true, title: title), child: body, ) : Scaffold( extendBodyBehindAppBar: true, appBar: SegmentedBar(cupertino: false, title: title), body: body, ), ); } }