Lock toggling lelantus scanning behind a mutex

This commit is contained in:
sneurlax 2024-05-30 18:17:26 -05:00
parent 0f98c0be2c
commit d37d86759d
2 changed files with 35 additions and 19 deletions

View file

@ -47,7 +47,8 @@ class _LelantusSettingsViewState extends ConsumerState<LelantusSettingsView> {
bool _isInitialized = false; bool _isInitialized = false;
Wallet<CryptoCurrency>? wallet; Wallet<CryptoCurrency>? wallet;
bool? enableLelantusScanning = false; bool _enableLelantusScanning = false;
bool _isUpdatingLelantusScanning = false;
@override @override
void didChangeDependencies() { void didChangeDependencies() {
@ -63,7 +64,7 @@ class _LelantusSettingsViewState extends ConsumerState<LelantusSettingsView> {
// Parse otherDataJsonString to get the enableLelantusScanning value. // Parse otherDataJsonString to get the enableLelantusScanning value.
if (wallet?.info.otherDataJsonString != null) { if (wallet?.info.otherDataJsonString != null) {
final otherDataJson = json.decode(wallet!.info.otherDataJsonString!); final otherDataJson = json.decode(wallet!.info.otherDataJsonString!);
enableLelantusScanning = _enableLelantusScanning =
otherDataJson[WalletInfoKeys.enableLelantusScanning] as bool? ?? otherDataJson[WalletInfoKeys.enableLelantusScanning] as bool? ??
false; false;
} }
@ -106,16 +107,23 @@ class _LelantusSettingsViewState extends ConsumerState<LelantusSettingsView> {
height: 20, height: 20,
width: 40, width: 40,
child: DraggableSwitchButton( child: DraggableSwitchButton(
isOn: enableLelantusScanning ?? false, isOn: _enableLelantusScanning,
onValueChanged: (newValue) { onValueChanged: (newValue) async {
if (_isUpdatingLelantusScanning) return;
_isUpdatingLelantusScanning = true; // Lock mutex.
// Toggle enableLelantusScanning in wallet info. // Toggle enableLelantusScanning in wallet info.
wallet?.info.updateOtherData(newEntries: { await wallet?.info.updateOtherData(
newEntries: {
WalletInfoKeys.enableLelantusScanning: WalletInfoKeys.enableLelantusScanning:
!(enableLelantusScanning ?? false) !_enableLelantusScanning,
}, isar: ref.read(mainDBProvider).isar).then((value) { },
// Should setState be used here? isar: ref.read(mainDBProvider).isar,
enableLelantusScanning = );
!(enableLelantusScanning ?? false);
setState(() {
_enableLelantusScanning = !_enableLelantusScanning;
_isUpdatingLelantusScanning = false; // Free mutex.
}); });
}, },
), ),

View file

@ -60,6 +60,7 @@ class MoreFeaturesDialog extends ConsumerStatefulWidget {
class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> { class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
bool _enableLelantusScanning = false; bool _enableLelantusScanning = false;
bool _isUpdatingLelantusScanning = false; // Mutex.
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -171,15 +172,22 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
width: 40, width: 40,
child: DraggableSwitchButton( child: DraggableSwitchButton(
isOn: _enableLelantusScanning, isOn: _enableLelantusScanning,
onValueChanged: (newValue) { onValueChanged: (newValue) async {
if (_isUpdatingLelantusScanning) return;
_isUpdatingLelantusScanning = true; // Lock mutex.
// Toggle enableLelantusScanning in wallet info. // Toggle enableLelantusScanning in wallet info.
wallet.info.updateOtherData(newEntries: { await wallet.info.updateOtherData(
newEntries: {
WalletInfoKeys.enableLelantusScanning: WalletInfoKeys.enableLelantusScanning:
!(_enableLelantusScanning) !_enableLelantusScanning,
}, isar: ref.read(mainDBProvider).isar).then((value) { },
// Should setState be used here? isar: ref.read(mainDBProvider).isar,
_enableLelantusScanning = );
!(_enableLelantusScanning);
setState(() {
_enableLelantusScanning = !_enableLelantusScanning;
_isUpdatingLelantusScanning = false; // Free mutex.
}); });
}, },
), ),