From d37d86759d73afbdf1a8455ba7ac71d7e390663c Mon Sep 17 00:00:00 2001 From: sneurlax Date: Thu, 30 May 2024 18:17:26 -0500 Subject: [PATCH] Lock toggling lelantus scanning behind a mutex --- .../lelantus_settings_view.dart | 30 ++++++++++++------- .../more_features/more_features_dialog.dart | 24 ++++++++++----- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/lib/pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/lelantus_settings_view.dart b/lib/pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/lelantus_settings_view.dart index ad53ccacf..1f8c342b0 100644 --- a/lib/pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/lelantus_settings_view.dart +++ b/lib/pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings/lelantus_settings_view.dart @@ -47,7 +47,8 @@ class _LelantusSettingsViewState extends ConsumerState { bool _isInitialized = false; Wallet? wallet; - bool? enableLelantusScanning = false; + bool _enableLelantusScanning = false; + bool _isUpdatingLelantusScanning = false; @override void didChangeDependencies() { @@ -63,7 +64,7 @@ class _LelantusSettingsViewState extends ConsumerState { // Parse otherDataJsonString to get the enableLelantusScanning value. if (wallet?.info.otherDataJsonString != null) { final otherDataJson = json.decode(wallet!.info.otherDataJsonString!); - enableLelantusScanning = + _enableLelantusScanning = otherDataJson[WalletInfoKeys.enableLelantusScanning] as bool? ?? false; } @@ -106,16 +107,23 @@ class _LelantusSettingsViewState extends ConsumerState { height: 20, width: 40, child: DraggableSwitchButton( - isOn: enableLelantusScanning ?? false, - onValueChanged: (newValue) { + isOn: _enableLelantusScanning, + onValueChanged: (newValue) async { + if (_isUpdatingLelantusScanning) return; + _isUpdatingLelantusScanning = true; // Lock mutex. + // Toggle enableLelantusScanning in wallet info. - wallet?.info.updateOtherData(newEntries: { - WalletInfoKeys.enableLelantusScanning: - !(enableLelantusScanning ?? false) - }, isar: ref.read(mainDBProvider).isar).then((value) { - // Should setState be used here? - enableLelantusScanning = - !(enableLelantusScanning ?? false); + await wallet?.info.updateOtherData( + newEntries: { + WalletInfoKeys.enableLelantusScanning: + !_enableLelantusScanning, + }, + isar: ref.read(mainDBProvider).isar, + ); + + setState(() { + _enableLelantusScanning = !_enableLelantusScanning; + _isUpdatingLelantusScanning = false; // Free mutex. }); }, ), diff --git a/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/more_features/more_features_dialog.dart b/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/more_features/more_features_dialog.dart index f1a9815c4..1ab2a6781 100644 --- a/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/more_features/more_features_dialog.dart +++ b/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/more_features/more_features_dialog.dart @@ -60,6 +60,7 @@ class MoreFeaturesDialog extends ConsumerStatefulWidget { class _MoreFeaturesDialogState extends ConsumerState { bool _enableLelantusScanning = false; + bool _isUpdatingLelantusScanning = false; // Mutex. @override Widget build(BuildContext context) { @@ -171,15 +172,22 @@ class _MoreFeaturesDialogState extends ConsumerState { width: 40, child: DraggableSwitchButton( isOn: _enableLelantusScanning, - onValueChanged: (newValue) { + onValueChanged: (newValue) async { + if (_isUpdatingLelantusScanning) return; + _isUpdatingLelantusScanning = true; // Lock mutex. + // Toggle enableLelantusScanning in wallet info. - wallet.info.updateOtherData(newEntries: { - WalletInfoKeys.enableLelantusScanning: - !(_enableLelantusScanning) - }, isar: ref.read(mainDBProvider).isar).then((value) { - // Should setState be used here? - _enableLelantusScanning = - !(_enableLelantusScanning); + await wallet.info.updateOtherData( + newEntries: { + WalletInfoKeys.enableLelantusScanning: + !_enableLelantusScanning, + }, + isar: ref.read(mainDBProvider).isar, + ); + + setState(() { + _enableLelantusScanning = !_enableLelantusScanning; + _isUpdatingLelantusScanning = false; // Free mutex. }); }, ),