3ec580c2d9
Servers become rows with a selection mark, addresses as subtitles and a Use/Edit/Remove menu plus an Add server row; the connection is a status row with address and Reconnect/Disconnect rows; Templates and Schedules are chevron rows in a Library group. Material groups are flat surfaceContainer surfaces with primary titleSmall headers (no Cards); Cupertino uses inset-grouped sections on the grouped background, switches for the ntfy choices. The trust warning is a footer, helper copy is shortened, Save workspace is a legible tonal/plain button, and the column starts at the bar's inset (max 720) instead of being centred. The page pads by the top media padding instead of a top SafeArea.
129 lines
4.1 KiB
Dart
129 lines
4.1 KiB
Dart
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 '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});
|
|
final ServerConnections connections;
|
|
|
|
Future<void> _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<void> _remove(BuildContext context, SavedServer server) async {
|
|
final cupertino = usesCupertinoHost(context);
|
|
final confirmed = cupertino
|
|
? await showCupertinoDialog<bool>(
|
|
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<bool>(
|
|
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));
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => ListenableBuilder(
|
|
listenable: connections,
|
|
builder: (context, _) {
|
|
final cupertino = usesCupertinoHost(context);
|
|
final error = connections.error;
|
|
return SettingsGroup(
|
|
title: 'Servers',
|
|
leadingRows: true,
|
|
footer: error == null
|
|
? null
|
|
: Text(error, style: TextStyle(color: settingsErrorColor(context))),
|
|
children: [
|
|
for (final server in connections.catalog.servers)
|
|
SavedServerRow(
|
|
server: server,
|
|
selected: server.id == connections.catalog.active?.id,
|
|
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)),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|