From de949efbff5c545c2b630a1cbc56a79567ba9f1d Mon Sep 17 00:00:00 2001 From: julian Date: Fri, 31 May 2024 11:59:43 -0600 Subject: [PATCH] show firo sparl electrumx data cache size and implement clearing of that cache when all electrumx cache for firo is cleared --- lib/db/sqlite/firo_cache.dart | 45 ++++++++++++++ .../wallet_settings_view.dart | 7 ++- .../spark_info.dart | 59 +++++++++++++++++++ .../wallet_settings_wallet_settings_view.dart | 34 +++++++++++ lib/route_generator.dart | 10 ++++ 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 lib/pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/spark_info.dart diff --git a/lib/db/sqlite/firo_cache.dart b/lib/db/sqlite/firo_cache.dart index 47cd680b3..d04eed56e 100644 --- a/lib/db/sqlite/firo_cache.dart +++ b/lib/db/sqlite/firo_cache.dart @@ -29,6 +29,34 @@ List _ffiHashTagsComputeWrapper(List base64Tags) { abstract class FiroCacheCoordinator { static Future init() => _FiroCache.init(); + static Future clearSharedCache() async { + return await _FiroCache._deleteAllCache(); + } + + static Future getSparkCacheSize() async { + final dir = await StackFileSystem.applicationSQLiteDirectory(); + final cacheFile = File("${dir.path}/${_FiroCache.sqliteDbFileName}"); + final int bytes; + if (await cacheFile.exists()) { + bytes = await cacheFile.length(); + } else { + bytes = 0; + } + + if (bytes < 1024) { + return '$bytes B'; + } else if (bytes < 1048576) { + final double kbSize = bytes / 1024; + return '${kbSize.toStringAsFixed(2)} KB'; + } else if (bytes < 1073741824) { + final double mbSize = bytes / 1048576; + return '${mbSize.toStringAsFixed(2)} MB'; + } else { + final double gbSize = bytes / 1073741824; + return '${gbSize.toStringAsFixed(2)} GB'; + } + } + static Future runFetchAndUpdateSparkUsedCoinTags( ElectrumXClient client, ) async { @@ -164,6 +192,23 @@ abstract class _FiroCache { ); } + static Future _deleteAllCache() async { + final start = DateTime.now(); + db.execute( + """ + DELETE FROM SparkSet; + DELETE FROM SparkCoin; + DELETE FROM SparkSetCoins; + DELETE FROM SparkUsedCoinTags; + VACUUM; + """, + ); + _debugLog( + "_deleteAllCache() " + "duration = ${DateTime.now().difference(start)}", + ); + } + static Future _createDb(String file) async { final db = sqlite3.open( file, diff --git a/lib/pages/settings_views/wallet_settings_view/wallet_settings_view.dart b/lib/pages/settings_views/wallet_settings_view/wallet_settings_view.dart index 1af5c6e6b..004bd57df 100644 --- a/lib/pages/settings_views/wallet_settings_view/wallet_settings_view.dart +++ b/lib/pages/settings_views/wallet_settings_view/wallet_settings_view.dart @@ -16,6 +16,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:tuple/tuple.dart'; import '../../../db/hive/db.dart'; +import '../../../db/sqlite/firo_cache.dart'; import '../../../models/epicbox_config_model.dart'; import '../../../notifications/show_flush_bar.dart'; import '../../../providers/global/wallets_provider.dart'; @@ -413,7 +414,8 @@ class _WalletSettingsViewState extends ConsumerState { ), ); - if (result == "OK" && mounted) { + if (result == "OK" && + context.mounted) { await showLoading( whileFuture: Future.wait( [ @@ -426,6 +428,9 @@ class _WalletSettingsViewState extends ConsumerState { .clearSharedTransactionCache( currency: coin, ), + if (coin is Firo) + FiroCacheCoordinator + .clearSharedCache(), ], ), context: context, diff --git a/lib/pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/spark_info.dart b/lib/pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/spark_info.dart new file mode 100644 index 000000000..7cb9b91f3 --- /dev/null +++ b/lib/pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/spark_info.dart @@ -0,0 +1,59 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../../../../db/sqlite/firo_cache.dart'; +import '../../../../themes/stack_colors.dart'; +import '../../../../utilities/text_styles.dart'; +import '../../../../widgets/background.dart'; +import '../../../../widgets/custom_buttons/app_bar_icon_button.dart'; +import '../../../../widgets/detail_item.dart'; + +class SparkInfoView extends ConsumerWidget { + const SparkInfoView({ + super.key, + }); + + static const String routeName = "/sparkInfo"; + + @override + Widget build(BuildContext context, WidgetRef ref) { + return Background( + child: Scaffold( + backgroundColor: Theme.of(context).extension()!.background, + appBar: AppBar( + leading: AppBarBackButton( + onPressed: () { + Navigator.of(context).pop(); + }, + ), + title: Text( + "Spark Info", + style: STextStyles.navBarTitle(context), + ), + ), + body: Padding( + padding: const EdgeInsets.all(16), + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + FutureBuilder( + future: FiroCacheCoordinator.getSparkCacheSize(), + builder: (_, snapshot) { + String detail = "Loading..."; + if (snapshot.connectionState == ConnectionState.done) { + detail = snapshot.data ?? detail; + } + + return DetailItem( + title: "Spark electrumx cache size", + detail: detail, + ); + }, + ), + ], + ), + ), + ), + ); + } +} diff --git a/lib/pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/wallet_settings_wallet_settings_view.dart b/lib/pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/wallet_settings_wallet_settings_view.dart index 7c2884258..9c874514e 100644 --- a/lib/pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/wallet_settings_wallet_settings_view.dart +++ b/lib/pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/wallet_settings_wallet_settings_view.dart @@ -24,6 +24,7 @@ import '../../../pinpad_views/lock_screen_view.dart'; import 'delete_wallet_warning_view.dart'; import 'lelantus_settings_view.dart'; import 'rename_wallet_view.dart'; +import 'spark_info.dart'; class WalletSettingsWalletSettingsView extends ConsumerWidget { const WalletSettingsWalletSettingsView({ @@ -216,6 +217,39 @@ class WalletSettingsWalletSettingsView extends ConsumerWidget { ), ), ), + const SizedBox( + height: 8, + ), + RoundedWhiteContainer( + padding: const EdgeInsets.all(0), + child: RawMaterialButton( + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular( + Constants.size.circularBorderRadius, + ), + ), + materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, + onPressed: () { + Navigator.of(context).pushNamed( + SparkInfoView.routeName, + ); + }, + child: Padding( + padding: const EdgeInsets.symmetric( + horizontal: 12.0, + vertical: 20, + ), + child: Row( + children: [ + Text( + "Spark info", + style: STextStyles.titleBold12(context), + ), + ], + ), + ), + ), + ), ], ), ), diff --git a/lib/route_generator.dart b/lib/route_generator.dart index 27d1eb4f7..ccedb0a6a 100644 --- a/lib/route_generator.dart +++ b/lib/route_generator.dart @@ -131,6 +131,7 @@ import 'pages/settings_views/wallet_settings_view/wallet_settings_wallet_setting import 'pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/delete_wallet_warning_view.dart'; import 'pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/lelantus_settings_view.dart'; import 'pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/rename_wallet_view.dart'; +import 'pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/spark_info.dart'; import 'pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/wallet_settings_wallet_settings_view.dart'; import 'pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/xpub_view.dart'; import 'pages/special/firo_rescan_recovery_error_dialog.dart'; @@ -1966,6 +1967,15 @@ class RouteGenerator { } return _routeError("${settings.name} invalid args: ${args.toString()}"); + case SparkInfoView.routeName: + return getRoute( + shouldUseMaterialRoute: useMaterialPageRoute, + builder: (_) => const SparkInfoView(), + settings: RouteSettings( + name: settings.name, + ), + ); + // == Desktop specific routes ============================================ case CreatePasswordView.routeName: if (args is bool) {