allow clearing of spark cache on desktop

This commit is contained in:
julian 2024-06-07 10:30:35 -06:00
parent 6e3d96cdfa
commit 82fb542740

View file

@ -8,12 +8,11 @@
*
*/
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/flutter_svg.dart';
import '../../../../../db/sqlite/firo_cache.dart';
import '../../../../../providers/db/main_db_provider.dart';
import '../../../../../providers/global/prefs_provider.dart';
import '../../../../../providers/global/wallets_provider.dart';
@ -23,10 +22,13 @@ import '../../../../../utilities/text_styles.dart';
import '../../../../../wallets/crypto_currency/coins/banano.dart';
import '../../../../../wallets/crypto_currency/coins/firo.dart';
import '../../../../../wallets/isar/models/wallet_info.dart';
import '../../../../../wallets/isar/providers/wallet_info_provider.dart';
import '../../../../../wallets/wallet/wallet_mixin_interfaces/cash_fusion_interface.dart';
import '../../../../../wallets/wallet/wallet_mixin_interfaces/coin_control_interface.dart';
import '../../../../../wallets/wallet/wallet_mixin_interfaces/lelantus_interface.dart';
import '../../../../../wallets/wallet/wallet_mixin_interfaces/ordinals_interface.dart';
import '../../../../../wallets/wallet/wallet_mixin_interfaces/paynym_interface.dart';
import '../../../../../wallets/wallet/wallet_mixin_interfaces/spark_interface.dart';
import '../../../../../widgets/custom_buttons/draggable_switch_button.dart';
import '../../../../../widgets/desktop/desktop_dialog.dart';
import '../../../../../widgets/desktop/desktop_dialog_close_button.dart';
@ -59,9 +61,26 @@ class MoreFeaturesDialog extends ConsumerStatefulWidget {
}
class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
bool _enableLelantusScanning = false;
bool _isUpdatingLelantusScanning = false; // Mutex.
Future<void> _switchToggled(bool newValue) async {
if (_isUpdatingLelantusScanning) return;
_isUpdatingLelantusScanning = true; // Lock mutex.
try {
// Toggle enableLelantusScanning in wallet info.
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
WalletInfoKeys.enableLelantusScanning: newValue,
},
isar: ref.read(mainDBProvider).isar,
);
} finally {
// ensure _isUpdatingLelantusScanning is set to false no matter what
_isUpdatingLelantusScanning = false;
}
}
@override
Widget build(BuildContext context) {
final wallet = ref.watch(
@ -70,14 +89,6 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
),
);
// Parse otherDataJsonString to get the enableLelantusScanning value.
if (wallet.info.otherDataJsonString != null) {
final otherDataJson = json.decode(wallet.info.otherDataJsonString!);
_enableLelantusScanning =
otherDataJson[WalletInfoKeys.enableLelantusScanning] as bool? ??
false;
}
final coinControlPrefEnabled = ref.watch(
prefsChangeNotifierProvider.select(
(value) => value.enableCoinControl,
@ -108,7 +119,7 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
label: "Anonymize funds",
detail: "Anonymize funds",
iconAsset: Assets.svg.recycle,
onPressed: () => widget.onAnonymizeAllPressed?.call(),
onPressed: () async => widget.onAnonymizeAllPressed?.call(),
),
// TODO: [prio=med]
// if (manager.hasWhirlpoolSupport)
@ -123,73 +134,53 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
label: "Coin control",
detail: "Control, freeze, and utilize outputs at your discretion",
iconAsset: Assets.svg.coinControl.gamePad,
onPressed: () => widget.onCoinControlPressed?.call(),
onPressed: () async => widget.onCoinControlPressed?.call(),
),
if (wallet is PaynymInterface)
_MoreFeaturesItem(
label: "PayNym",
detail: "Increased address privacy using BIP47",
iconAsset: Assets.svg.robotHead,
onPressed: () => widget.onPaynymPressed?.call(),
onPressed: () async => widget.onPaynymPressed?.call(),
),
if (wallet is OrdinalsInterface)
_MoreFeaturesItem(
label: "Ordinals",
detail: "View and control your ordinals in Stack",
iconAsset: Assets.svg.ordinal,
onPressed: () => widget.onOrdinalsPressed?.call(),
onPressed: () async => widget.onOrdinalsPressed?.call(),
),
if (wallet.info.coin is Banano)
_MoreFeaturesItem(
label: "MonKey",
detail: "Generate Banano MonKey",
iconAsset: Assets.svg.monkey,
onPressed: () => widget.onMonkeyPressed?.call(),
onPressed: () async => widget.onMonkeyPressed?.call(),
),
if (wallet is CashFusionInterface)
_MoreFeaturesItem(
label: "Fusion",
detail: "Decentralized mixing protocol",
iconAsset: Assets.svg.cashFusion,
onPressed: () => widget.onFusionPressed?.call(),
onPressed: () async => widget.onFusionPressed?.call(),
),
if (wallet.info.coin is Firo)
Padding(
padding: const EdgeInsets.symmetric(
vertical: 6,
horizontal: 32,
),
child: RoundedContainer(
color: Colors.transparent,
borderColor: Theme.of(context)
.extension<StackColors>()!
.textFieldDefaultBG,
if (wallet is SparkInterface)
const _MoreFeaturesClearSparkCacheItem(),
if (wallet is LelantusInterface)
_MoreFeaturesItemBase(
child: Row(
children: [
SizedBox(width: 3),
const SizedBox(width: 3),
SizedBox(
height: 20,
width: 40,
child: DraggableSwitchButton(
isOn: _enableLelantusScanning,
onValueChanged: (newValue) async {
if (_isUpdatingLelantusScanning) return;
_isUpdatingLelantusScanning = true; // Lock mutex.
// Toggle enableLelantusScanning in wallet info.
await wallet.info.updateOtherData(
newEntries: {
WalletInfoKeys.enableLelantusScanning:
!_enableLelantusScanning,
},
isar: ref.read(mainDBProvider).isar,
);
setState(() {
_enableLelantusScanning = !_enableLelantusScanning;
_isUpdatingLelantusScanning = false; // Free mutex.
});
},
isOn: ref.watch(
pWalletInfo(widget.walletId)
.select((value) => value.otherData),
)[WalletInfoKeys.enableLelantusScanning] as bool? ??
false,
onValueChanged: _switchToggled,
),
),
const SizedBox(
@ -202,16 +193,11 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
"Scan for Lelantus transactions",
style: STextStyles.w600_20(context),
),
// Text(
// detail,
// style: STextStyles.desktopTextExtraExtraSmall(context),
// ),
],
),
],
),
),
),
const SizedBox(
height: 28,
),
@ -221,7 +207,7 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
}
}
class _MoreFeaturesItem extends StatelessWidget {
class _MoreFeaturesItem extends StatefulWidget {
const _MoreFeaturesItem({
super.key,
required this.label,
@ -236,6 +222,78 @@ class _MoreFeaturesItem extends StatelessWidget {
final String label;
final String detail;
final String iconAsset;
final Future<void> Function()? onPressed;
@override
State<_MoreFeaturesItem> createState() => _MoreFeaturesItemState();
}
class _MoreFeaturesItemState extends State<_MoreFeaturesItem> {
bool _onPressedLock = false;
@override
Widget build(BuildContext context) {
return _MoreFeaturesItemBase(
onPressed: () async {
if (_onPressedLock) {
return;
}
_onPressedLock = true;
try {
await widget.onPressed?.call();
} finally {
_onPressedLock = false;
}
},
child: Row(
children: [
RoundedContainer(
padding: const EdgeInsets.all(0),
color: Theme.of(context).extension<StackColors>()!.settingsIconBack,
width: _MoreFeaturesItem.iconSizeBG,
height: _MoreFeaturesItem.iconSizeBG,
radiusMultiplier: _MoreFeaturesItem.iconSizeBG,
child: Center(
child: SvgPicture.asset(
widget.iconAsset,
width: _MoreFeaturesItem.iconSize,
height: _MoreFeaturesItem.iconSize,
color: Theme.of(context)
.extension<StackColors>()!
.settingsIconIcon,
),
),
),
const SizedBox(
width: 16,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.label,
style: STextStyles.w600_20(context),
),
Text(
widget.detail,
style: STextStyles.desktopTextExtraExtraSmall(context),
),
],
),
],
),
);
}
}
class _MoreFeaturesItemBase extends StatelessWidget {
const _MoreFeaturesItemBase({
super.key,
required this.child,
this.onPressed,
});
final Widget child;
final VoidCallback? onPressed;
@override
@ -250,20 +308,61 @@ class _MoreFeaturesItem extends StatelessWidget {
borderColor:
Theme.of(context).extension<StackColors>()!.textFieldDefaultBG,
onPressed: onPressed,
child: child,
),
);
}
}
class _MoreFeaturesClearSparkCacheItem extends StatefulWidget {
const _MoreFeaturesClearSparkCacheItem({
super.key,
});
static const double iconSizeBG = 46;
static const double iconSize = 24;
@override
State<_MoreFeaturesClearSparkCacheItem> createState() =>
_MoreFeaturesClearSparkCacheItemState();
}
class _MoreFeaturesClearSparkCacheItemState
extends State<_MoreFeaturesClearSparkCacheItem> {
bool _onPressedLock = false;
static const label = "Reset Spark electrumx cache";
@override
Widget build(BuildContext context) {
return _MoreFeaturesItemBase(
onPressed: () async {
if (_onPressedLock) {
return;
}
_onPressedLock = true;
try {
await FiroCacheCoordinator.clearSharedCache();
setState(() {
// trigger rebuild for cache size display
});
} finally {
_onPressedLock = false;
}
},
child: Row(
children: [
RoundedContainer(
padding: const EdgeInsets.all(0),
color:
Theme.of(context).extension<StackColors>()!.settingsIconBack,
width: iconSizeBG,
height: iconSizeBG,
radiusMultiplier: iconSizeBG,
color: Theme.of(context).extension<StackColors>()!.settingsIconBack,
width: _MoreFeaturesItem.iconSizeBG,
height: _MoreFeaturesItem.iconSizeBG,
radiusMultiplier: _MoreFeaturesItem.iconSizeBG,
child: Center(
child: SvgPicture.asset(
iconAsset,
width: iconSize,
height: iconSize,
Assets.svg.x,
width: _MoreFeaturesItem.iconSize,
height: _MoreFeaturesItem.iconSize,
color: Theme.of(context)
.extension<StackColors>()!
.settingsIconIcon,
@ -280,15 +379,19 @@ class _MoreFeaturesItem extends StatelessWidget {
label,
style: STextStyles.w600_20(context),
),
Text(
detail,
FutureBuilder(
future: FiroCacheCoordinator.getSparkCacheSize(),
builder: (_, snapshot) {
return Text(
snapshot.data ?? "",
style: STextStyles.desktopTextExtraExtraSmall(context),
);
},
),
],
),
],
),
),
);
}
}