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 createState() => _StorageScreenState(); } class _StorageScreenState extends State { 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 _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( 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( 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, ); } }