initial commit

This commit is contained in:
2026-07-27 14:42:23 +02:00
commit 833c68a189
257 changed files with 17947 additions and 0 deletions
+267
View File
@@ -0,0 +1,267 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import '../../core/models/models.dart';
import '../../core/network/api_exception.dart';
import '../../core/theme/theme_x.dart';
import '../../core/widgets/backup_target_form_dialog.dart';
import '../../core/widgets/backup_targets_list.dart';
import '../../core/widgets/empty_state.dart';
import '../../core/widgets/error_banner.dart';
import '../../core/widgets/sparkline.dart';
import '../../data/repositories/node_repository.dart';
import 'widgets/backup_config_form_dialog.dart';
import 'widgets/backup_history_table.dart';
class BackupsScreen extends ConsumerWidget {
const BackupsScreen({super.key});
Future<void> _runNow(BuildContext context, WidgetRef ref) async {
final repo = ref.read(nodeRepositoryProvider);
if (repo == null) return;
try {
await repo.runBackup();
ref.invalidate(backupConfigProvider);
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Backup started')));
}
} catch (e) {
final message = e is ApiException ? e.userMessage : e.toString();
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message)));
}
}
}
Future<void> _addTarget(BuildContext context, WidgetRef ref) {
return showBackupTargetFormDialog(
context,
onSave: (draft) async {
final repo = ref.read(nodeRepositoryProvider);
if (repo == null) throw const ApiException.unknown('No active connection.');
await repo.addBackupTarget(draft);
ref.invalidate(backupConfigProvider);
},
);
}
Future<void> _editTarget(BuildContext context, WidgetRef ref, BackupTarget target) {
return showBackupTargetFormDialog(
context,
existing: target,
onSave: (draft) async {
final repo = ref.read(nodeRepositoryProvider);
if (repo == null) throw const ApiException.unknown('No active connection.');
await repo.updateBackupTarget(target.id, draft);
ref.invalidate(backupConfigProvider);
},
);
}
Future<void> _deleteTarget(BuildContext context, WidgetRef ref, BackupTarget target) async {
final confirmed = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Remove backup target?'),
content: Text('This removes "${target.name}" from the node\'s backup targets.'),
actions: [
TextButton(onPressed: () => Navigator.of(context).pop(false), child: const Text('Cancel')),
FilledButton(
style: FilledButton.styleFrom(backgroundColor: context.status.critical),
onPressed: () => Navigator.of(context).pop(true),
child: const Text('Remove'),
),
],
),
);
if (confirmed != true) return;
final repo = ref.read(nodeRepositoryProvider);
if (repo == null) return;
try {
await repo.deleteBackupTarget(target.id);
ref.invalidate(backupConfigProvider);
} catch (e) {
final message = e is ApiException ? e.userMessage : e.toString();
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message)));
}
}
}
Future<void> _runTarget(WidgetRef ref, BackupTarget target) async {
final repo = ref.read(nodeRepositoryProvider);
if (repo == null) throw const ApiException.unknown('No active connection.');
await repo.runBackupTarget(target.id);
}
Future<RunState> _fetchProgress(WidgetRef ref, BackupTarget target) async {
final repo = ref.read(nodeRepositoryProvider);
if (repo == null) return const RunState();
return repo.getBackupTargetProgress(target.id);
}
@override
Widget build(BuildContext context, WidgetRef ref) {
final configAsync = ref.watch(backupConfigProvider);
return configAsync.when(
loading: () => const Center(child: CircularProgressIndicator()),
error: (error, stackTrace) => Center(
child: ErrorBanner(
message: error is ApiException ? error.userMessage : error.toString(),
onRetry: () => ref.invalidate(backupConfigProvider),
),
),
data: (config) {
if (config == null) {
return const Center(
child: EmptyState(icon: Icons.cloud_outlined, title: 'No connection configured'),
);
}
return ListView(
padding: const EdgeInsets.all(24),
children: [
_ConfigCard(config: config, onRunNow: () => _runNow(context, ref)),
const SizedBox(height: 22),
Text('BACKUP TARGETS', style: context.text.titleSmall),
const SizedBox(height: 10),
Card(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: BackupTargetsList(
targets: config.targets,
onAdd: () => _addTarget(context, ref),
onEdit: (target) => _editTarget(context, ref, target),
onDelete: (target) => _deleteTarget(context, ref, target),
onRun: (target) => _runTarget(ref, target),
fetchProgress: (target) => _fetchProgress(ref, target),
onSettled: (target, state) => ref.invalidate(backupConfigProvider),
),
),
),
const SizedBox(height: 22),
_DurationTrendCard(history: config.history),
const SizedBox(height: 22),
Text('EXECUTION HISTORY', style: context.text.titleSmall),
const SizedBox(height: 10),
BackupHistoryTable(history: config.history, targets: config.targets),
],
);
},
);
}
}
class _ConfigCard extends StatelessWidget {
const _ConfigCard({required this.config, required this.onRunNow});
final BackupConfig config;
final VoidCallback onRunNow;
@override
Widget build(BuildContext context) {
final dateFormat = DateFormat('EEE, MMM d · HH:mm');
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('Node backup configuration', style: context.text.titleMedium),
Row(
children: [
OutlinedButton.icon(
onPressed: () => showBackupConfigFormDialog(context, existing: config),
icon: const Icon(Icons.edit_outlined, size: 16),
label: const Text('Edit'),
),
const SizedBox(width: 8),
FilledButton.icon(
onPressed: onRunNow,
icon: const Icon(Icons.cloud_outlined, size: 16),
label: const Text('Run all now'),
),
],
),
],
),
const Divider(height: 24),
_kv(context, 'Source path', config.sourcePath?.isNotEmpty == true ? config.sourcePath! : 'Not set'),
_kv(
context,
'Next run',
config.nextRun == null ? 'Not scheduled' : dateFormat.format(config.nextRun!.toLocal()),
),
_kv(
context,
'Last run',
config.lastRun == null ? 'Never' : dateFormat.format(config.lastRun!.toLocal()),
),
],
),
),
);
}
Widget _kv(BuildContext context, String label, String value) {
return Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Row(
children: [
SizedBox(width: 120, child: Text(label, style: context.text.bodySmall)),
Expanded(child: Text(value, style: context.text.bodyMedium)),
],
),
);
}
}
class _DurationTrendCard extends StatelessWidget {
const _DurationTrendCard({required this.history});
final List<BackupExecution> history;
@override
Widget build(BuildContext context) {
final sorted = [...history]..sort((a, b) => a.timestamp.compareTo(b.timestamp));
final recent = sorted.length > 10 ? sorted.sublist(sorted.length - 10) : sorted;
if (recent.isEmpty) {
return const SizedBox.shrink();
}
final values = [for (final e in recent) e.durationSeconds.toDouble()];
final latest = recent.last.durationSeconds;
final avg = values.reduce((a, b) => a + b) / values.length;
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
Sparkline(values: values),
const SizedBox(width: 20),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text('Duration trend', style: context.text.titleMedium),
const SizedBox(height: 4),
Text(
'Latest ${latest}s · last ${recent.length}-run avg ${avg.toStringAsFixed(1)}s',
style: context.text.bodySmall,
),
],
),
],
),
),
);
}
}
@@ -0,0 +1,101 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/models/models.dart';
import '../../../core/network/api_exception.dart';
import '../../../data/repositories/node_repository.dart';
/// Edit dialog for the node-level [BackupConfig]'s source path. Target
/// management (add/edit/remove/run/schedule) lives in the dedicated
/// [BackupTargetsList] on the Backups screen, not in this dialog.
Future<void> showBackupConfigFormDialog(BuildContext context, {required BackupConfig existing}) {
return showDialog<void>(
context: context,
builder: (context) => _BackupConfigFormDialog(existing: existing),
);
}
class _BackupConfigFormDialog extends ConsumerStatefulWidget {
const _BackupConfigFormDialog({required this.existing});
final BackupConfig existing;
@override
ConsumerState<_BackupConfigFormDialog> createState() => _BackupConfigFormDialogState();
}
class _BackupConfigFormDialogState extends ConsumerState<_BackupConfigFormDialog> {
late final _sourcePathController = TextEditingController(text: widget.existing.sourcePath);
bool _saving = false;
String? _error;
@override
void dispose() {
_sourcePathController.dispose();
super.dispose();
}
Future<void> _save() async {
setState(() {
_saving = true;
_error = null;
});
final repo = ref.read(nodeRepositoryProvider);
if (repo == null) {
setState(() {
_saving = false;
_error = 'No active connection.';
});
return;
}
final draft = widget.existing.copyWith(sourcePath: _sourcePathController.text.trim());
try {
await repo.updateBackup(draft);
ref.invalidate(backupConfigProvider);
if (mounted) Navigator.of(context).pop();
} catch (e) {
setState(() {
_saving = false;
_error = e is ApiException ? e.userMessage : e.toString();
});
}
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Edit backup configuration'),
content: SizedBox(
width: 420,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
controller: _sourcePathController,
decoration: const InputDecoration(labelText: 'Source path', hintText: '/opt'),
),
if (_error != null) ...[
const SizedBox(height: 12),
Text(_error!, style: TextStyle(color: Theme.of(context).colorScheme.error)),
],
],
),
),
actions: [
TextButton(
onPressed: _saving ? null : () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
FilledButton(
onPressed: _saving ? null : _save,
child: _saving
? const SizedBox(width: 16, height: 16, child: CircularProgressIndicator(strokeWidth: 2))
: const Text('Save'),
),
],
);
}
}
@@ -0,0 +1,73 @@
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../../../core/models/models.dart';
import '../../../core/models/severity_mapping.dart';
import '../../../core/theme/theme_x.dart';
import '../../../core/widgets/data_table_scaffold.dart';
import '../../../core/widgets/empty_state.dart';
import '../../../core/widgets/mono_text.dart';
import '../../../core/widgets/status_pill.dart';
class BackupHistoryTable extends StatelessWidget {
const BackupHistoryTable({super.key, required this.history, required this.targets});
final List<BackupExecution> history;
final List<BackupTarget> targets;
String _targetName(String? targetId) {
if (targetId == null) return '';
for (final t in targets) {
if (t.id == targetId) return t.name;
}
return targetId;
}
@override
Widget build(BuildContext context) {
final sorted = [...history]..sort((a, b) => b.timestamp.compareTo(a.timestamp));
final timeFormat = DateFormat('yyyy-MM-dd HH:mm');
return DataTableScaffold(
empty: const EmptyState(
icon: Icons.cloud_outlined,
title: 'No backup history yet',
message: 'Run a backup to see execution results here.',
),
columns: const [
DataColumn(label: Text('Timestamp')),
DataColumn(label: Text('Target')),
DataColumn(label: Text('Result')),
DataColumn(label: Text('Duration')),
DataColumn(label: Text('Message')),
],
rows: [
for (final execution in sorted)
DataRow(
cells: [
DataCell(MonoText(timeFormat.format(execution.timestamp.toLocal()))),
DataCell(Text(_targetName(execution.targetId))),
DataCell(
StatusPill(
label: execution.success ? 'success' : 'failed',
severity: severityForSuccess(execution.success),
),
),
DataCell(
Text(
'${execution.durationSeconds}s',
style: context.dataStyles.numericTabular,
),
),
DataCell(
ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 340),
child: MonoText(execution.message, small: true, maxLines: 1),
),
),
],
),
],
);
}
}