fix(settings): preserve server editor drafts until save succeeds
This commit is contained in:
@@ -4,11 +4,14 @@ import 'package:flutter/material.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<SavedServer?> editSavedServer(
|
||||
BuildContext context, {
|
||||
SavedServer? server,
|
||||
Future<void> Function(SavedServer)? onSave,
|
||||
}) {
|
||||
final page = _ServerEditor(server: server);
|
||||
final page = _ServerEditor(server: server, onSave: onSave);
|
||||
return Navigator.of(context).push<SavedServer>(
|
||||
usesCupertinoHost(context)
|
||||
? CupertinoPageRoute(builder: (_) => page)
|
||||
@@ -17,35 +20,51 @@ Future<SavedServer?> editSavedServer(
|
||||
}
|
||||
|
||||
class _ServerEditor extends StatefulWidget {
|
||||
const _ServerEditor({this.server});
|
||||
const _ServerEditor({this.server, this.onSave});
|
||||
final SavedServer? server;
|
||||
final Future<void> 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;
|
||||
|
||||
void _save() {
|
||||
Future<void> _save() async {
|
||||
if (_saving) return;
|
||||
try {
|
||||
final server = SavedServer.validated(
|
||||
id:
|
||||
widget.server?.id ??
|
||||
'server-${DateTime.now().microsecondsSinceEpoch}',
|
||||
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) {
|
||||
setState(() => _error = '$error');
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_saving = false;
|
||||
_error = '$error';
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +90,7 @@ class _ServerEditorState extends State<_ServerEditor> {
|
||||
CupertinoTextField(
|
||||
key: Key(key),
|
||||
controller: controller,
|
||||
enabled: !_saving,
|
||||
placeholder: multiline ? 'wss://server.example/connect' : 'Home',
|
||||
minLines: multiline ? 3 : 1,
|
||||
maxLines: multiline ? null : 1,
|
||||
@@ -85,6 +105,7 @@ class _ServerEditorState extends State<_ServerEditor> {
|
||||
return TextField(
|
||||
key: Key(key),
|
||||
controller: controller,
|
||||
enabled: !_saving,
|
||||
decoration: InputDecoration(
|
||||
labelText: label,
|
||||
border: const OutlineInputBorder(),
|
||||
@@ -100,12 +121,16 @@ class _ServerEditorState extends State<_ServerEditor> {
|
||||
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: _save,
|
||||
child: const Text('Save server'),
|
||||
onPressed: _saving ? null : _save,
|
||||
child: Text(saveLabel),
|
||||
)
|
||||
: FilledButton(onPressed: _save, child: const Text('Save server'));
|
||||
: FilledButton(
|
||||
onPressed: _saving ? null : _save,
|
||||
child: Text(saveLabel),
|
||||
);
|
||||
final body = SafeArea(
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
@@ -131,12 +156,15 @@ class _ServerEditorState extends State<_ServerEditor> {
|
||||
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,
|
||||
child: Semantics(
|
||||
liveRegion: true,
|
||||
child: Text(
|
||||
_error!,
|
||||
style: TextStyle(
|
||||
color: cupertino
|
||||
? CupertinoColors.systemRed.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.error,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -145,14 +173,17 @@ class _ServerEditorState extends State<_ServerEditor> {
|
||||
],
|
||||
),
|
||||
);
|
||||
return cupertino
|
||||
? CupertinoPageScaffold(
|
||||
navigationBar: CupertinoNavigationBar(middle: Text(title)),
|
||||
child: body,
|
||||
)
|
||||
: Scaffold(
|
||||
appBar: AppBar(title: Text(title)),
|
||||
body: body,
|
||||
);
|
||||
return PopScope(
|
||||
canPop: !_saving,
|
||||
child: cupertino
|
||||
? CupertinoPageScaffold(
|
||||
navigationBar: CupertinoNavigationBar(middle: Text(title)),
|
||||
child: body,
|
||||
)
|
||||
: Scaffold(
|
||||
appBar: AppBar(title: Text(title)),
|
||||
body: body,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,13 +8,30 @@ 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<void> _edit(BuildContext context, [SavedServer? server]) async {
|
||||
final edited = await editSavedServer(context, server: server);
|
||||
if (edited != null) await connections.save(edited);
|
||||
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<void> _remove(BuildContext context, SavedServer server) async {
|
||||
|
||||
Reference in New Issue
Block a user