Files
pipelines/app/lib/features/settings/saved_server_editor.dart
T

159 lines
4.7 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../core/servers/server_catalog.dart';
import '../../ui/adaptive/host.dart';
Future<SavedServer?> editSavedServer(
BuildContext context, {
SavedServer? server,
}) {
final page = _ServerEditor(server: server);
return Navigator.of(context).push<SavedServer>(
usesCupertinoHost(context)
? CupertinoPageRoute(builder: (_) => page)
: MaterialPageRoute(builder: (_) => page),
);
}
class _ServerEditor extends StatefulWidget {
const _ServerEditor({this.server});
final SavedServer? server;
@override
State<_ServerEditor> createState() => _ServerEditorState();
}
class _ServerEditorState extends State<_ServerEditor> {
late final _name = TextEditingController(text: widget.server?.name ?? '');
late final _addresses = TextEditingController(
text: widget.server?.endpoints.join('\n') ?? '',
);
String? _error;
void _save() {
try {
final server = SavedServer.validated(
id:
widget.server?.id ??
'server-${DateTime.now().microsecondsSinceEpoch}',
name: _name.text,
legacyEndpoint: widget.server?.legacyEndpoint,
endpoints: _addresses.text
.split('\n')
.where((line) => line.trim().isNotEmpty),
);
Navigator.of(context).pop(server);
} catch (error) {
setState(() => _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,
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,
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 save = cupertino
? CupertinoButton.filled(
onPressed: _save,
child: const Text('Save server'),
)
: FilledButton(onPressed: _save, child: const Text('Save server'));
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: 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 cupertino
? CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(middle: Text(title)),
child: body,
)
: Scaffold(
appBar: AppBar(title: Text(title)),
body: body,
);
}
}