mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2026-02-23 16:45:27 +01:00
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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user