ask before enabling toggle

This commit is contained in:
julian 2024-07-31 16:03:47 -06:00 committed by julian-CStack
parent 237eb5cd8e
commit d6c609fea4
3 changed files with 232 additions and 220 deletions

View file

@ -17,7 +17,6 @@ import '../../../../route_generator.dart';
import '../../../../themes/stack_colors.dart';
import '../../../../utilities/constants.dart';
import '../../../../utilities/text_styles.dart';
import '../../../../utilities/util.dart';
import '../../../../wallets/isar/models/wallet_info.dart';
import '../../../../wallets/isar/providers/wallet_info_provider.dart';
import '../../../../wallets/wallet/wallet_mixin_interfaces/lelantus_interface.dart';
@ -53,13 +52,20 @@ class WalletSettingsWalletSettingsView extends ConsumerStatefulWidget {
class _WalletSettingsWalletSettingsViewState
extends ConsumerState<WalletSettingsWalletSettingsView> {
late final DSBController _switchController;
bool _switchReuseAddressToggledLock = false; // Mutex.
Future<void> _switchReuseAddressToggled(bool newValue) async {
if (newValue) {
await showDialog(
Future<void> _switchReuseAddressToggled() async {
if (_switchReuseAddressToggledLock) {
return;
}
_switchReuseAddressToggledLock = true; // Lock mutex.
try {
if (_switchController.isOn?.call() != true) {
final canContinue = await showDialog<bool?>(
context: context,
builder: (context) {
final isDesktop = Util.isDesktop;
return StackDialog(
title: "Warning!",
message:
@ -90,42 +96,40 @@ class _WalletSettingsWalletSettingsViewState
),
);
},
).then((confirmed) async {
if (_switchReuseAddressToggledLock) {
return;
}
_switchReuseAddressToggledLock = true; // Lock mutex.
);
try {
if (confirmed == true) {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
WalletInfoKeys.reuseAddress: true,
},
isar: ref.read(mainDBProvider).isar,
);
if (canContinue == true) {
await _updateAddressReuse(true);
}
} else {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
WalletInfoKeys.reuseAddress: false,
},
isar: ref.read(mainDBProvider).isar,
);
await _updateAddressReuse(false);
}
} finally {
// ensure _switchReuseAddressToggledLock is set to false no matter what.
_switchReuseAddressToggledLock = false;
}
});
} else {
}
Future<void> _updateAddressReuse(bool shouldReuse) async {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
WalletInfoKeys.reuseAddress: false,
WalletInfoKeys.reuseAddress: shouldReuse,
},
isar: ref.read(mainDBProvider).isar,
);
if (_switchController.isOn != null) {
if (_switchController.isOn!.call() != shouldReuse) {
_switchController.activate?.call();
}
}
}
@override
void initState() {
_switchController = DSBController();
super.initState();
}
@override
Widget build(BuildContext context) {
@ -231,20 +235,21 @@ class _WalletSettingsWalletSettingsViewState
if (ref.watch(pWallets).getWallet(widget.walletId)
is MultiAddressInterface)
RoundedWhiteContainer(
child: Consumer(
builder: (_, ref, __) {
return RawMaterialButton(
padding: const EdgeInsets.all(0),
child: RawMaterialButton(
// splashColor: Theme.of(context).extension<StackColors>()!.highlight,
materialTapTargetSize:
MaterialTapTargetSize.shrinkWrap,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
),
onPressed: null,
onPressed: _switchReuseAddressToggled,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
padding: const EdgeInsets.symmetric(
horizontal: 12.0,
vertical: 20,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
@ -256,23 +261,21 @@ class _WalletSettingsWalletSettingsViewState
SizedBox(
height: 20,
width: 40,
child: IgnorePointer(
child: DraggableSwitchButton(
isOn: ref.watch(
pWalletInfo(widget.walletId).select(
(value) => value.otherData),
)[WalletInfoKeys.reuseAddress]
as bool? ??
(value) => value.otherData,
),
)[WalletInfoKeys.reuseAddress] as bool? ??
false,
onValueChanged: (newValue) {
_switchReuseAddressToggled(newValue);
},
controller: _switchController,
),
),
),
],
),
),
);
},
),
),
if (ref.watch(pWallets).getWallet(widget.walletId)

View file

@ -20,7 +20,6 @@ import '../../../../../providers/global/wallets_provider.dart';
import '../../../../../themes/stack_colors.dart';
import '../../../../../utilities/assets.dart';
import '../../../../../utilities/text_styles.dart';
import '../../../../../utilities/util.dart';
import '../../../../../wallets/crypto_currency/crypto_currency.dart';
import '../../../../../wallets/isar/models/wallet_info.dart';
import '../../../../../wallets/isar/providers/wallet_info_provider.dart';
@ -106,13 +105,20 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
}
}
late final DSBController _switchController;
bool _switchReuseAddressToggledLock = false; // Mutex.
Future<void> _switchReuseAddressToggled(bool newValue) async {
if (newValue) {
await showDialog(
Future<void> _switchReuseAddressToggled() async {
if (_switchReuseAddressToggledLock) {
return;
}
_switchReuseAddressToggledLock = true; // Lock mutex.
try {
if (_switchController.isOn?.call() != true) {
final canContinue = await showDialog<bool?>(
context: context,
builder: (context) {
final isDesktop = Util.isDesktop;
return DesktopDialog(
maxWidth: 576,
child: Column(
@ -180,42 +186,40 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
),
);
},
).then((confirmed) async {
if (_switchReuseAddressToggledLock) {
return;
}
_switchReuseAddressToggledLock = true; // Lock mutex.
);
try {
if (confirmed == true) {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
WalletInfoKeys.reuseAddress: true,
},
isar: ref.read(mainDBProvider).isar,
);
if (canContinue == true) {
await _updateAddressReuse(true);
}
} else {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
WalletInfoKeys.reuseAddress: false,
},
isar: ref.read(mainDBProvider).isar,
);
await _updateAddressReuse(false);
}
} finally {
// ensure _switchReuseAddressToggledLock is set to false no matter what.
_switchReuseAddressToggledLock = false;
}
});
} else {
}
Future<void> _updateAddressReuse(bool shouldReuse) async {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
WalletInfoKeys.reuseAddress: false,
WalletInfoKeys.reuseAddress: shouldReuse,
},
isar: ref.read(mainDBProvider).isar,
);
if (_switchController.isOn != null) {
if (_switchController.isOn!.call() != shouldReuse) {
_switchController.activate?.call();
}
}
}
@override
void initState() {
_switchController = DSBController();
super.initState();
}
@override
Widget build(BuildContext context) {
@ -370,19 +374,22 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
),
// reuseAddress preference.
_MoreFeaturesItemBase(
onPressed: _switchReuseAddressToggled,
child: Row(
children: [
const SizedBox(width: 3),
SizedBox(
height: 20,
width: 40,
child: IgnorePointer(
child: DraggableSwitchButton(
isOn: ref.watch(
pWalletInfo(widget.walletId)
.select((value) => value.otherData),
)[WalletInfoKeys.reuseAddress] as bool? ??
false,
onValueChanged: _switchReuseAddressToggled,
controller: _switchController,
),
),
),
const SizedBox(

View file

@ -77,6 +77,7 @@ class DraggableSwitchButtonState extends State<DraggableSwitchButton> {
_enabled = widget.enabled;
valueListener = _isOn ? ValueNotifier(1.0) : ValueNotifier(0.0);
widget.controller?.isOn = () => _isOn;
widget.controller?.activate = () {
_isOn = !_isOn;
// widget.onValueChanged?.call(_isOn);
@ -212,4 +213,5 @@ class DraggableSwitchButtonState extends State<DraggableSwitchButton> {
class DSBController {
VoidCallback? activate;
bool Function()? isOn;
}