Add server storage overview and retention controls to Settings
This commit is contained in:
@@ -13,6 +13,7 @@ import 'settings_controls.dart';
|
||||
import 'settings_font_section.dart';
|
||||
import '../../ui/theme/app_font_controller.dart';
|
||||
import 'settings_group.dart';
|
||||
import 'storage/settings_storage_section.dart';
|
||||
import 'settings_page_body.dart';
|
||||
import 'settings_notifications_section.dart';
|
||||
import 'settings_ntfy_section.dart';
|
||||
@@ -196,6 +197,10 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
controller: _controller,
|
||||
workspaceField: _workspaceField,
|
||||
),
|
||||
_single(
|
||||
'Data management',
|
||||
SettingsStorageSection(gateway: widget.client),
|
||||
),
|
||||
// A device-local choice: a browser and a phone keep their own.
|
||||
SettingsFontSection(
|
||||
controller: widget.fonts ?? AppFontController.instance,
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../settings_row.dart';
|
||||
import '../settings_theme.dart';
|
||||
import 'storage_screen.dart';
|
||||
|
||||
class SettingsStorageSection extends StatelessWidget {
|
||||
const SettingsStorageSection({super.key, required this.gateway});
|
||||
final ApiGateway gateway;
|
||||
@override
|
||||
Widget build(BuildContext context) => SettingsRow(
|
||||
leading: Icon(
|
||||
usesCupertinoControls(context)
|
||||
? CupertinoIcons.archivebox
|
||||
: Icons.storage_outlined,
|
||||
),
|
||||
title: const Text('Storage'),
|
||||
subtitle: const Text('Usage, worktrees and retention policy'),
|
||||
chevron: true,
|
||||
onTap: () {
|
||||
final page = StorageScreen(gateway: gateway);
|
||||
Navigator.of(context).push(
|
||||
usesCupertinoControls(context)
|
||||
? CupertinoPageRoute<void>(builder: (_) => page)
|
||||
: MaterialPageRoute<void>(builder: (_) => page),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../settings_group.dart';
|
||||
import 'storage_controls.dart';
|
||||
import 'storage_models.dart';
|
||||
|
||||
class StorageCleanup extends StatelessWidget {
|
||||
const StorageCleanup({super.key, required this.data, this.onApply});
|
||||
final JsonMap data;
|
||||
final VoidCallback? onApply;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final applied = data['applied'] == true;
|
||||
final items = storageRows(data['items']);
|
||||
return SettingsGroup(
|
||||
title: applied ? 'Cleanup result' : 'Cleanup preview',
|
||||
children: [
|
||||
storagePadding(
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
applied
|
||||
? '${storageInt(data['removed_count'])} items removed · ${storageBytes(storageInt(data['removed_bytes']))} reclaimed'
|
||||
: '${storageInt(data['candidate_count'])} items · ${storageBytes(storageInt(data['reclaimable_bytes']))} reclaimable',
|
||||
),
|
||||
Text('${storageInt(data['protected_count'])} items protected'),
|
||||
if (data['target_reached'] == false &&
|
||||
storageInt(data['target_bytes']) > 0)
|
||||
const Text(
|
||||
'Protected or retained data prevents reaching the size limit.',
|
||||
),
|
||||
if (!applied)
|
||||
const Text(
|
||||
'Review the items below. Cleanup permanently removes eligible run history, artifacts and selected worktrees.',
|
||||
),
|
||||
if (data['errors'] case final List errors when errors.isNotEmpty)
|
||||
Text(errors.join('\n')),
|
||||
],
|
||||
),
|
||||
),
|
||||
for (final item in items)
|
||||
storagePadding(
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'${item['protected'] == true
|
||||
? 'Protected'
|
||||
: applied
|
||||
? 'Selected'
|
||||
: 'Remove'} · ${item['kind']} ${item['id'] ?? ''} · ${storageBytes(storageInt(item['bytes']))}',
|
||||
),
|
||||
if (item['path'] case final String path when path.isNotEmpty)
|
||||
Text(path),
|
||||
if (item['reason'] case final String reason
|
||||
when reason.isNotEmpty)
|
||||
Text(reason),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (!applied && storageInt(data['candidate_count']) > 0)
|
||||
StorageAction(
|
||||
label: 'Apply cleanup',
|
||||
onPressed: onApply,
|
||||
destructive: true,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../../../core/api_gateway.dart';
|
||||
import 'storage_models.dart';
|
||||
|
||||
class StorageController extends ChangeNotifier {
|
||||
StorageController(this.gateway);
|
||||
final ApiGateway gateway;
|
||||
JsonMap? overview;
|
||||
JsonMap? preview;
|
||||
JsonMap? result;
|
||||
String? error;
|
||||
String? notice;
|
||||
bool busy = false;
|
||||
bool _disposed = false;
|
||||
int _revision = 0;
|
||||
|
||||
void invalidatePreview() {
|
||||
_revision++;
|
||||
preview = null;
|
||||
result = null;
|
||||
notice = null;
|
||||
_notify();
|
||||
}
|
||||
|
||||
Future<JsonMap> _call(String type, [JsonMap fields = const {}]) async {
|
||||
final reply = await gateway.call(type, fields);
|
||||
if (reply['error'] case final String message when message.isNotEmpty) {
|
||||
throw StateError(message);
|
||||
}
|
||||
return reply;
|
||||
}
|
||||
|
||||
JsonMap _overview(Object? value) {
|
||||
final data = storageMap(value);
|
||||
if (data['total_bytes'] is! num ||
|
||||
(data['total_bytes'] as num) < 0 ||
|
||||
data['categories'] is! List ||
|
||||
data['policy'] is! Map) {
|
||||
throw StateError(
|
||||
'The server returned incomplete storage information. Refresh or update the server.',
|
||||
);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
Future<void> load() => _operation(() async {
|
||||
final reply = await _call('get_storage');
|
||||
if (reply['storage'] is! Map) {
|
||||
throw StateError('Storage information is unavailable on this server.');
|
||||
}
|
||||
overview = _overview(reply['storage']);
|
||||
});
|
||||
|
||||
Future<void> save(StoragePolicy policy) => _operation(() async {
|
||||
final reply = await _call('set_storage_policy', {
|
||||
'storage_policy': policy.toJson(),
|
||||
});
|
||||
if (reply['storage'] is! Map) {
|
||||
throw StateError('The server did not confirm the saved policy.');
|
||||
}
|
||||
overview = _overview(reply['storage']);
|
||||
preview = null;
|
||||
result = null;
|
||||
notice = 'Retention policy saved on this server.';
|
||||
});
|
||||
|
||||
Future<void> plan(StoragePolicy policy) => _operation(() async {
|
||||
final revision = _revision;
|
||||
final reply = await _call('preview_storage_cleanup', {
|
||||
'storage_policy': policy.toJson(),
|
||||
});
|
||||
if (revision != _revision) return;
|
||||
if (reply['storage_cleanup'] is! Map) {
|
||||
throw StateError('The server did not return a cleanup preview.');
|
||||
}
|
||||
preview = storageMap(reply['storage_cleanup']);
|
||||
result = null;
|
||||
});
|
||||
|
||||
Future<void> apply(StoragePolicy policy) => _operation(() async {
|
||||
final token = preview?['preview_token'];
|
||||
if (token is! String || token.isEmpty) {
|
||||
throw StateError('Preview cleanup again before removing data.');
|
||||
}
|
||||
final reply = await _call('apply_storage_cleanup', {
|
||||
'storage_policy': policy.toJson(),
|
||||
'storage_cleanup_token': token,
|
||||
});
|
||||
if (reply['storage_cleanup'] is! Map) {
|
||||
throw StateError('The server did not return a cleanup result.');
|
||||
}
|
||||
result = storageMap(reply['storage_cleanup']);
|
||||
preview = null;
|
||||
final current = await _call('get_storage');
|
||||
overview = _overview(current['storage']);
|
||||
});
|
||||
|
||||
Future<void> _operation(Future<void> Function() action) async {
|
||||
if (busy || _disposed) return;
|
||||
busy = true;
|
||||
error = null;
|
||||
notice = null;
|
||||
_notify();
|
||||
try {
|
||||
await action();
|
||||
} catch (exception) {
|
||||
error = exception is StateError
|
||||
? exception.message.toString()
|
||||
: '$exception';
|
||||
} finally {
|
||||
busy = false;
|
||||
_notify();
|
||||
}
|
||||
}
|
||||
|
||||
void _notify() {
|
||||
if (!_disposed) notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../settings_theme.dart';
|
||||
|
||||
class StorageAction extends StatelessWidget {
|
||||
const StorageAction({
|
||||
super.key,
|
||||
required this.label,
|
||||
required this.onPressed,
|
||||
this.destructive = false,
|
||||
});
|
||||
final String label;
|
||||
final VoidCallback? onPressed;
|
||||
final bool destructive;
|
||||
@override
|
||||
Widget build(BuildContext context) => usesCupertinoControls(context)
|
||||
? CupertinoButton(
|
||||
onPressed: onPressed,
|
||||
child: Text(
|
||||
label,
|
||||
style: destructive
|
||||
? TextStyle(
|
||||
color: CupertinoColors.destructiveRed.resolveFrom(context),
|
||||
)
|
||||
: null,
|
||||
),
|
||||
)
|
||||
: TextButton(
|
||||
onPressed: onPressed,
|
||||
style: destructive
|
||||
? TextButton.styleFrom(
|
||||
foregroundColor: Theme.of(context).colorScheme.error,
|
||||
)
|
||||
: null,
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
|
||||
class StorageSwitch extends StatelessWidget {
|
||||
const StorageSwitch({
|
||||
super.key,
|
||||
required this.label,
|
||||
required this.value,
|
||||
required this.onChanged,
|
||||
});
|
||||
final String label;
|
||||
final bool value;
|
||||
final ValueChanged<bool>? onChanged;
|
||||
@override
|
||||
Widget build(BuildContext context) => Row(
|
||||
children: [
|
||||
Expanded(child: Text(label)),
|
||||
Semantics(
|
||||
label: label,
|
||||
child: usesCupertinoControls(context)
|
||||
? CupertinoSwitch(value: value, onChanged: onChanged)
|
||||
: Switch(value: value, onChanged: onChanged),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
class StorageInput extends StatelessWidget {
|
||||
const StorageInput({
|
||||
super.key,
|
||||
required this.label,
|
||||
required this.controller,
|
||||
required this.onChanged,
|
||||
required this.enabled,
|
||||
this.decimal = false,
|
||||
});
|
||||
final String label;
|
||||
final TextEditingController controller;
|
||||
final ValueChanged<String> onChanged;
|
||||
final bool enabled;
|
||||
final bool decimal;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final keyboard = TextInputType.numberWithOptions(decimal: decimal);
|
||||
return usesCupertinoControls(context)
|
||||
? Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(label),
|
||||
const SizedBox(height: 8),
|
||||
Semantics(
|
||||
label: label,
|
||||
child: CupertinoTextField(
|
||||
controller: controller,
|
||||
enabled: enabled,
|
||||
keyboardType: keyboard,
|
||||
onChanged: onChanged,
|
||||
placeholder: 'No limit',
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
: TextField(
|
||||
controller: controller,
|
||||
enabled: enabled,
|
||||
keyboardType: keyboard,
|
||||
onChanged: onChanged,
|
||||
decoration: InputDecoration(
|
||||
labelText: label,
|
||||
hintText: 'No limit',
|
||||
border: const OutlineInputBorder(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget storagePadding(Widget child) =>
|
||||
Padding(padding: const EdgeInsets.all(16), child: child);
|
||||
@@ -0,0 +1,80 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
JsonMap storageMap(Object? value) =>
|
||||
value is Map ? Map<String, dynamic>.from(value) : <String, dynamic>{};
|
||||
int storageInt(Object? value) => value is num ? value.toInt() : 0;
|
||||
List<JsonMap> storageRows(Object? value) =>
|
||||
value is List ? value.whereType<Map>().map(storageMap).toList() : const [];
|
||||
|
||||
class StoragePolicy {
|
||||
const StoragePolicy({
|
||||
this.enabled = false,
|
||||
this.maxAgeDays = 0,
|
||||
this.maxBytes = 0,
|
||||
this.includeWorktrees = false,
|
||||
});
|
||||
factory StoragePolicy.fromJson(Object? value) {
|
||||
final data = storageMap(value);
|
||||
return StoragePolicy(
|
||||
enabled: data['enabled'] == true,
|
||||
maxAgeDays: storageInt(data['max_age_days']),
|
||||
maxBytes: storageInt(data['max_bytes']),
|
||||
includeWorktrees: data['include_worktrees'] == true,
|
||||
);
|
||||
}
|
||||
final bool enabled;
|
||||
final int maxAgeDays;
|
||||
final int maxBytes;
|
||||
final bool includeWorktrees;
|
||||
JsonMap toJson() => {
|
||||
'enabled': enabled,
|
||||
'max_age_days': maxAgeDays,
|
||||
'max_bytes': maxBytes,
|
||||
'include_worktrees': includeWorktrees,
|
||||
};
|
||||
}
|
||||
|
||||
String storageBytes(int bytes) {
|
||||
if (bytes < 1024) return '$bytes B';
|
||||
const units = ['KiB', 'MiB', 'GiB', 'TiB'];
|
||||
var amount = bytes / 1024;
|
||||
var index = 0;
|
||||
while (amount >= 1024 && index < units.length - 1) {
|
||||
amount /= 1024;
|
||||
index++;
|
||||
}
|
||||
return '${amount.toStringAsFixed(amount < 10 ? 2 : 1)} ${units[index]}';
|
||||
}
|
||||
|
||||
/// Blank limits mean unbounded; no implicit deletion defaults.
|
||||
StoragePolicy parseStoragePolicy({
|
||||
required bool enabled,
|
||||
required String days,
|
||||
required String gib,
|
||||
required bool includeWorktrees,
|
||||
}) {
|
||||
final age = days.trim().isEmpty ? 0 : int.tryParse(days.trim());
|
||||
final size = gib.trim().isEmpty ? 0.0 : double.tryParse(gib.trim());
|
||||
if (age == null || age < 0 || age > 36500) {
|
||||
throw const FormatException(
|
||||
'Enter a whole number of days from 1 to 36500, or leave blank.',
|
||||
);
|
||||
}
|
||||
if (size == null || !size.isFinite || size < 0 || size > 1048576) {
|
||||
throw const FormatException(
|
||||
'Enter a storage limit in GiB, or leave blank.',
|
||||
);
|
||||
}
|
||||
final bytes = (size * 1024 * 1024 * 1024).round();
|
||||
if (enabled && age == 0 && bytes == 0) {
|
||||
throw const FormatException(
|
||||
'Set an age or size limit before enabling automatic cleanup.',
|
||||
);
|
||||
}
|
||||
return StoragePolicy(
|
||||
enabled: enabled,
|
||||
maxAgeDays: age,
|
||||
maxBytes: bytes,
|
||||
includeWorktrees: includeWorktrees,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import '../settings_group.dart';
|
||||
import 'storage_controls.dart';
|
||||
|
||||
class StoragePolicyForm extends StatelessWidget {
|
||||
const StoragePolicyForm({
|
||||
super.key,
|
||||
required this.days,
|
||||
required this.gib,
|
||||
required this.enabled,
|
||||
required this.worktrees,
|
||||
required this.busy,
|
||||
required this.onEnabled,
|
||||
required this.onWorktrees,
|
||||
required this.onEdit,
|
||||
required this.onSave,
|
||||
required this.onPreview,
|
||||
});
|
||||
final TextEditingController days;
|
||||
final TextEditingController gib;
|
||||
final bool enabled, worktrees, busy;
|
||||
final ValueChanged<bool> onEnabled, onWorktrees;
|
||||
final VoidCallback onEdit, onSave, onPreview;
|
||||
@override
|
||||
Widget build(BuildContext context) => SettingsGroup(
|
||||
title: 'Retention policy',
|
||||
footer: const Text(
|
||||
'Leave a limit blank to keep it unbounded. Age removes expired eligible data; the size limit removes the oldest eligible items first. Dirty or unmerged worktrees, active sessions and active runs are protected. Chat history is kept. Automatic cleanup runs hourly when enabled. Preview uses the limits above. Saving applies this policy to the connected server for every device.',
|
||||
),
|
||||
children: [
|
||||
storagePadding(
|
||||
StorageSwitch(
|
||||
label: 'Automatic cleanup',
|
||||
value: enabled,
|
||||
onChanged: busy ? null : onEnabled,
|
||||
),
|
||||
),
|
||||
storagePadding(
|
||||
StorageInput(
|
||||
label: 'Retain data for days',
|
||||
controller: days,
|
||||
enabled: !busy,
|
||||
onChanged: (_) => onEdit(),
|
||||
),
|
||||
),
|
||||
storagePadding(
|
||||
StorageInput(
|
||||
label: 'Maximum storage in GiB',
|
||||
controller: gib,
|
||||
enabled: !busy,
|
||||
decimal: true,
|
||||
onChanged: (_) => onEdit(),
|
||||
),
|
||||
),
|
||||
storagePadding(
|
||||
StorageSwitch(
|
||||
label: 'Include eligible worktrees',
|
||||
value: worktrees,
|
||||
onChanged: busy ? null : onWorktrees,
|
||||
),
|
||||
),
|
||||
storagePadding(
|
||||
Wrap(
|
||||
spacing: 12,
|
||||
children: [
|
||||
StorageAction(
|
||||
label: 'Save policy',
|
||||
onPressed: busy ? null : onSave,
|
||||
),
|
||||
StorageAction(
|
||||
label: 'Preview cleanup',
|
||||
onPressed: busy ? null : onPreview,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../settings_page_body.dart';
|
||||
import '../settings_theme.dart';
|
||||
import 'storage_cleanup.dart';
|
||||
import 'storage_controller.dart';
|
||||
import 'storage_controls.dart';
|
||||
import 'storage_models.dart';
|
||||
import 'storage_policy_form.dart';
|
||||
import 'storage_usage.dart';
|
||||
|
||||
class StorageScreen extends StatefulWidget {
|
||||
const StorageScreen({super.key, required this.gateway});
|
||||
final ApiGateway gateway;
|
||||
@override
|
||||
State<StorageScreen> createState() => _StorageScreenState();
|
||||
}
|
||||
|
||||
class _StorageScreenState extends State<StorageScreen> {
|
||||
late StorageController _controller;
|
||||
final _days = TextEditingController();
|
||||
final _gib = TextEditingController();
|
||||
bool _enabled = false, _worktrees = false, _seeded = false;
|
||||
String? _validation;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_connect();
|
||||
}
|
||||
|
||||
void _connect() {
|
||||
_controller = StorageController(widget.gateway)..addListener(_changed);
|
||||
unawaited(_controller.load());
|
||||
}
|
||||
|
||||
void _changed() {
|
||||
if (!mounted) return;
|
||||
final overview = _controller.overview;
|
||||
if (!_seeded && overview != null) {
|
||||
final policy = StoragePolicy.fromJson(overview['policy']);
|
||||
_enabled = policy.enabled;
|
||||
_worktrees = policy.includeWorktrees;
|
||||
_days.text = policy.maxAgeDays == 0 ? '' : '${policy.maxAgeDays}';
|
||||
_gib.text = policy.maxBytes == 0
|
||||
? ''
|
||||
: (policy.maxBytes / (1024 * 1024 * 1024))
|
||||
.toStringAsFixed(6)
|
||||
.replaceFirst(RegExp(r'\.?0+$'), '');
|
||||
_seeded = true;
|
||||
}
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant StorageScreen oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.gateway != widget.gateway) {
|
||||
_controller.dispose();
|
||||
_seeded = false;
|
||||
_days.clear();
|
||||
_gib.clear();
|
||||
_enabled = false;
|
||||
_worktrees = false;
|
||||
_validation = null;
|
||||
_connect();
|
||||
}
|
||||
}
|
||||
|
||||
void _edit() {
|
||||
_validation = null;
|
||||
_controller.invalidatePreview();
|
||||
}
|
||||
|
||||
StoragePolicy? _policy() {
|
||||
try {
|
||||
return parseStoragePolicy(
|
||||
enabled: _enabled,
|
||||
days: _days.text,
|
||||
gib: _gib.text,
|
||||
includeWorktrees: _worktrees,
|
||||
);
|
||||
} on FormatException catch (error) {
|
||||
setState(() => _validation = error.message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _apply() async {
|
||||
final policy = _policy();
|
||||
if (policy == null || _controller.preview == null) return;
|
||||
final preview = _controller.preview!;
|
||||
final message =
|
||||
'Permanently remove ${storageInt(preview['candidate_count'])} eligible items and reclaim up to ${storageBytes(storageInt(preview['reclaimable_bytes']))}? This cannot be undone.';
|
||||
final bool? confirmed;
|
||||
if (usesCupertinoControls(context)) {
|
||||
confirmed = await showCupertinoDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => CupertinoAlertDialog(
|
||||
title: const Text('Apply cleanup?'),
|
||||
content: Text(message),
|
||||
actions: [
|
||||
CupertinoDialogAction(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
isDestructiveAction: true,
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Remove items'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
} else {
|
||||
confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Apply cleanup?'),
|
||||
content: Text(message),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Remove items'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
if (confirmed == true && mounted) await _controller.apply(policy);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
_days.dispose();
|
||||
_gib.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino = usesCupertinoControls(context);
|
||||
final content = SettingsPageBody(
|
||||
children: [
|
||||
if (_controller.busy)
|
||||
const Center(child: CircularProgressIndicator.adaptive()),
|
||||
if (_validation ?? _controller.error case final String error)
|
||||
Semantics(liveRegion: true, child: Text(error)),
|
||||
if (_controller.notice case final String notice)
|
||||
Semantics(liveRegion: true, child: Text(notice)),
|
||||
if (_controller.overview case final JsonMap overview) ...[
|
||||
StorageUsage(data: overview),
|
||||
StoragePolicyForm(
|
||||
days: _days,
|
||||
gib: _gib,
|
||||
enabled: _enabled,
|
||||
worktrees: _worktrees,
|
||||
busy: _controller.busy,
|
||||
onEnabled: (value) {
|
||||
_enabled = value;
|
||||
_edit();
|
||||
},
|
||||
onWorktrees: (value) {
|
||||
_worktrees = value;
|
||||
_edit();
|
||||
},
|
||||
onEdit: _edit,
|
||||
onSave: () {
|
||||
final policy = _policy();
|
||||
if (policy != null) unawaited(_controller.save(policy));
|
||||
},
|
||||
onPreview: () {
|
||||
final policy = _policy();
|
||||
if (policy != null) unawaited(_controller.plan(policy));
|
||||
},
|
||||
),
|
||||
],
|
||||
if (_controller.preview case final JsonMap preview)
|
||||
StorageCleanup(
|
||||
data: preview,
|
||||
onApply: _controller.busy ? null : _apply,
|
||||
),
|
||||
if (_controller.result case final JsonMap result)
|
||||
StorageCleanup(data: result),
|
||||
StorageAction(
|
||||
label: 'Refresh storage',
|
||||
onPressed: _controller.busy
|
||||
? null
|
||||
: () => unawaited(_controller.load()),
|
||||
),
|
||||
],
|
||||
);
|
||||
return cupertino
|
||||
? CupertinoPageScaffold(
|
||||
navigationBar: const CupertinoNavigationBar(
|
||||
middle: Text('Storage'),
|
||||
),
|
||||
child: SafeArea(child: content),
|
||||
)
|
||||
: Scaffold(
|
||||
appBar: AppBar(title: const Text('Storage')),
|
||||
body: content,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import '../settings_group.dart';
|
||||
import 'storage_controls.dart';
|
||||
import 'storage_models.dart';
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
class StorageUsage extends StatelessWidget {
|
||||
const StorageUsage({super.key, required this.data});
|
||||
final JsonMap data;
|
||||
@override
|
||||
Widget build(BuildContext context) => SettingsGroup(
|
||||
title: 'Server storage',
|
||||
footer: const Text(
|
||||
'Combined file sizes on the connected server, with shared files counted once. Protected data may prevent reaching a size limit.',
|
||||
),
|
||||
children: [
|
||||
storagePadding(
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'${data['partial'] == true ? 'At least ' : ''}${storageBytes(storageInt(data['total_bytes']))} used',
|
||||
style: DefaultTextStyle.of(
|
||||
context,
|
||||
).style.copyWith(fontWeight: FontWeight.bold),
|
||||
),
|
||||
if (_measured(context) case final String measured)
|
||||
Text('Measured $measured'),
|
||||
if (data['partial'] == true)
|
||||
const Text('Measurement is incomplete. See details below.'),
|
||||
],
|
||||
),
|
||||
),
|
||||
for (final category in storageRows(data['categories']))
|
||||
storagePadding(
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Wrap(
|
||||
alignment: WrapAlignment.spaceBetween,
|
||||
spacing: 16,
|
||||
children: [
|
||||
Text('${category['label'] ?? category['id']}'),
|
||||
Text(
|
||||
'${category['partial'] == true ? 'At least ' : ''}${storageBytes(storageInt(category['bytes']))}',
|
||||
),
|
||||
],
|
||||
),
|
||||
Text('${storageInt(category['files'])} files'),
|
||||
if (category['path'] case final String path when path.isNotEmpty)
|
||||
Text(path, softWrap: true),
|
||||
if (category['error'] case final String error
|
||||
when error.isNotEmpty)
|
||||
Text(error),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (data['warnings'] case final List warnings when warnings.isNotEmpty)
|
||||
storagePadding(Text(warnings.join('\n'))),
|
||||
],
|
||||
);
|
||||
String? _measured(BuildContext context) {
|
||||
final time = DateTime.tryParse('${data['measured_at'] ?? ''}')?.toLocal();
|
||||
if (time == null) return null;
|
||||
final material = Localizations.of<MaterialLocalizations>(
|
||||
context,
|
||||
MaterialLocalizations,
|
||||
);
|
||||
if (material != null) {
|
||||
return '${time.day} ${material.formatMonthYear(time)} · ${material.formatTimeOfDay(TimeOfDay.fromDateTime(time), alwaysUse24HourFormat: true)}';
|
||||
}
|
||||
final local = CupertinoLocalizations.of(context);
|
||||
return '${time.day} ${local.datePickerMonth(time.month)} ${time.year} · ${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user