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

145 lines
4.6 KiB
Dart

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.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,
this.allowPrimarySelection,
});
final bool? allowPrimarySelection;
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;
final allowSelection = allowPrimarySelection ?? !kIsWeb;
return SettingsGroup(
title: 'Servers',
leadingRows: true,
footer: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('All saved machines connect for chat.'),
if (allowSelection)
const Text('The primary machine is used for pipelines and runs.'),
if (error != 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,
allowSelection: allowSelection,
primaryContext: true,
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)),
),
],
);
},
);
}