feat(database): Add database compression function (#1027)

* feat(database): Add database compression function

Add database compression functionality to the connection statistics page to reduce the size of the database file

Add multi-language support and related UI interactions

* fix(database): Update the description of database compression operations and fix the statistics cleanup logic

Update the description of database compression operations in the multilingual files to explicitly state that data will not be lost

Fix the connection statistics cleanup logic to ensure correct matching of server IDs

Add error handling for the compression operation to prevent the UI from freezing

* Update lib/l10n/app_en.arb

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
GT610
2026-01-26 13:40:42 +08:00
committed by GitHub
parent 9ac866644c
commit a0a62acdbc
29 changed files with 253 additions and 1 deletions

View File

@@ -1,3 +1,5 @@
import 'dart:io';
import 'package:fl_lib/fl_lib.dart';
import 'package:flutter/material.dart';
import 'package:server_box/core/extension/context/locale.dart';
@@ -16,6 +18,7 @@ class ConnectionStatsPage extends StatefulWidget {
class _ConnectionStatsPageState extends State<ConnectionStatsPage> {
List<ServerConnectionStats> _serverStats = [];
bool _isLoading = true;
bool _isCompacting = false;
@override
void initState() {
@@ -47,6 +50,13 @@ class _ConnectionStatsPageState extends State<ConnectionStatsPage> {
icon: const Icon(Icons.clear_all, color: Colors.red),
tooltip: libL10n.clear,
),
IconButton(
onPressed: _isCompacting ? null : _showCompactDialog,
icon: _isCompacting
? SizedLoading.small
: const Icon(Icons.compress),
tooltip: l10n.compactDatabase,
),
],
),
body: _buildBody,
@@ -196,6 +206,42 @@ class _ConnectionStatsPageState extends State<ConnectionStatsPage> {
),
);
}
void _showCompactDialog() {
final path = '${Paths.doc}${Pfs.seperator}connection_stats_enc.hive';
final file = File(path);
final oldSize = file.existsSync() ? file.lengthSync() : 0;
final sizeStr = oldSize < 1000 ? '$oldSize B' : oldSize < 1000 * 1000 ? '${(oldSize / 1000).toStringAsFixed(1)} KB' : '${(oldSize / (1000 * 1000)).toStringAsFixed(1)} MB';
context.showRoundDialog(
title: l10n.compactDatabase,
child: Text(l10n.compactDatabaseContent(sizeStr)),
actions: [
TextButton(onPressed: context.pop, child: Text(libL10n.cancel)),
TextButton(
onPressed: () async {
context.pop();
setState(() => _isCompacting = true);
try {
await Stores.connectionStats.compact();
final newSize = file.existsSync() ? file.lengthSync() : 0;
final newSizeStr = newSize < 1000 ? '$newSize B' : newSize < 1000 * 1000 ? '${(newSize / 1000).toStringAsFixed(1)} KB' : '${(newSize / (1000 * 1000)).toStringAsFixed(1)} MB';
if (mounted) {
setState(() => _isCompacting = false);
context.showSnackBar('${libL10n.success}: $sizeStr -> $newSizeStr');
}
} catch (e) {
if (mounted) {
setState(() => _isCompacting = false);
context.showSnackBar('${libL10n.error}: $e');
}
}
},
child: Text(l10n.confirm),
),
],
);
}
}
extension on _ConnectionStatsPageState {