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 '../../../../themes/stack_colors.dart';
import '../../../../utilities/constants.dart'; import '../../../../utilities/constants.dart';
import '../../../../utilities/text_styles.dart'; import '../../../../utilities/text_styles.dart';
import '../../../../utilities/util.dart';
import '../../../../wallets/isar/models/wallet_info.dart'; import '../../../../wallets/isar/models/wallet_info.dart';
import '../../../../wallets/isar/providers/wallet_info_provider.dart'; import '../../../../wallets/isar/providers/wallet_info_provider.dart';
import '../../../../wallets/wallet/wallet_mixin_interfaces/lelantus_interface.dart'; import '../../../../wallets/wallet/wallet_mixin_interfaces/lelantus_interface.dart';
@ -53,78 +52,83 @@ class WalletSettingsWalletSettingsView extends ConsumerStatefulWidget {
class _WalletSettingsWalletSettingsViewState class _WalletSettingsWalletSettingsViewState
extends ConsumerState<WalletSettingsWalletSettingsView> { extends ConsumerState<WalletSettingsWalletSettingsView> {
bool _switchReuseAddressToggledLock = false; // Mutex. late final DSBController _switchController;
Future<void> _switchReuseAddressToggled(bool newValue) async {
if (newValue) {
await showDialog(
context: context,
builder: (context) {
final isDesktop = Util.isDesktop;
return StackDialog(
title: "Warning!",
message:
"Reusing addresses reduces your privacy and security. Are you sure you want to reuse addresses by default?",
leftButton: TextButton(
style: Theme.of(context)
.extension<StackColors>()!
.getSecondaryEnabledButtonStyle(context),
child: Text(
"Cancel",
style: STextStyles.itemSubtitle12(context),
),
onPressed: () {
Navigator.of(context).pop(false);
},
),
rightButton: TextButton(
style: Theme.of(context)
.extension<StackColors>()!
.getPrimaryEnabledButtonStyle(context),
child: Text(
"Continue",
style: STextStyles.button(context),
),
onPressed: () {
Navigator.of(context).pop(true);
},
),
);
},
).then((confirmed) async {
if (_switchReuseAddressToggledLock) {
return;
}
_switchReuseAddressToggledLock = true; // Lock mutex.
try { bool _switchReuseAddressToggledLock = false; // Mutex.
if (confirmed == true) { Future<void> _switchReuseAddressToggled() async {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData( if (_switchReuseAddressToggledLock) {
newEntries: { return;
WalletInfoKeys.reuseAddress: true,
},
isar: ref.read(mainDBProvider).isar,
);
} else {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
WalletInfoKeys.reuseAddress: false,
},
isar: ref.read(mainDBProvider).isar,
);
}
} finally {
// ensure _switchReuseAddressToggledLock is set to false no matter what.
_switchReuseAddressToggledLock = false;
}
});
} else {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
WalletInfoKeys.reuseAddress: false,
},
isar: ref.read(mainDBProvider).isar,
);
} }
_switchReuseAddressToggledLock = true; // Lock mutex.
try {
if (_switchController.isOn?.call() != true) {
final canContinue = await showDialog<bool?>(
context: context,
builder: (context) {
return StackDialog(
title: "Warning!",
message:
"Reusing addresses reduces your privacy and security. Are you sure you want to reuse addresses by default?",
leftButton: TextButton(
style: Theme.of(context)
.extension<StackColors>()!
.getSecondaryEnabledButtonStyle(context),
child: Text(
"Cancel",
style: STextStyles.itemSubtitle12(context),
),
onPressed: () {
Navigator.of(context).pop(false);
},
),
rightButton: TextButton(
style: Theme.of(context)
.extension<StackColors>()!
.getPrimaryEnabledButtonStyle(context),
child: Text(
"Continue",
style: STextStyles.button(context),
),
onPressed: () {
Navigator.of(context).pop(true);
},
),
);
},
);
if (canContinue == true) {
await _updateAddressReuse(true);
}
} else {
await _updateAddressReuse(false);
}
} finally {
// ensure _switchReuseAddressToggledLock is set to false no matter what.
_switchReuseAddressToggledLock = false;
}
}
Future<void> _updateAddressReuse(bool shouldReuse) async {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
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 @override
@ -186,7 +190,7 @@ class _WalletSettingsWalletSettingsViewState
), ),
), ),
if (ref.watch(pWallets).getWallet(widget.walletId) if (ref.watch(pWallets).getWallet(widget.walletId)
is RbfInterface) is RbfInterface)
const SizedBox( const SizedBox(
height: 8, height: 8,
), ),
@ -231,48 +235,47 @@ class _WalletSettingsWalletSettingsViewState
if (ref.watch(pWallets).getWallet(widget.walletId) if (ref.watch(pWallets).getWallet(widget.walletId)
is MultiAddressInterface) is MultiAddressInterface)
RoundedWhiteContainer( RoundedWhiteContainer(
child: Consumer( padding: const EdgeInsets.all(0),
builder: (_, ref, __) { child: RawMaterialButton(
return RawMaterialButton( // splashColor: Theme.of(context).extension<StackColors>()!.highlight,
// splashColor: Theme.of(context).extension<StackColors>()!.highlight, materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
materialTapTargetSize: shape: RoundedRectangleBorder(
MaterialTapTargetSize.shrinkWrap, borderRadius: BorderRadius.circular(
shape: RoundedRectangleBorder( Constants.size.circularBorderRadius,
borderRadius: BorderRadius.circular( ),
Constants.size.circularBorderRadius, ),
onPressed: _switchReuseAddressToggled,
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 12.0,
vertical: 20,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Reuse receiving address",
style: STextStyles.titleBold12(context),
textAlign: TextAlign.left,
), ),
), SizedBox(
onPressed: null, height: 20,
child: Padding( width: 40,
padding: const EdgeInsets.symmetric(vertical: 8), child: IgnorePointer(
child: Row( child: DraggableSwitchButton(
mainAxisAlignment: MainAxisAlignment.spaceBetween, isOn: ref.watch(
children: [ pWalletInfo(widget.walletId).select(
Text( (value) => value.otherData,
"Reuse receiving address", ),
style: STextStyles.titleBold12(context), )[WalletInfoKeys.reuseAddress] as bool? ??
textAlign: TextAlign.left, false,
controller: _switchController,
), ),
SizedBox( ),
height: 20,
width: 40,
child: DraggableSwitchButton(
isOn: ref.watch(
pWalletInfo(widget.walletId).select(
(value) => value.otherData),
)[WalletInfoKeys.reuseAddress]
as bool? ??
false,
onValueChanged: (newValue) {
_switchReuseAddressToggled(newValue);
},
),
),
],
), ),
), ],
); ),
}, ),
), ),
), ),
if (ref.watch(pWallets).getWallet(widget.walletId) 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 '../../../../../themes/stack_colors.dart';
import '../../../../../utilities/assets.dart'; import '../../../../../utilities/assets.dart';
import '../../../../../utilities/text_styles.dart'; import '../../../../../utilities/text_styles.dart';
import '../../../../../utilities/util.dart';
import '../../../../../wallets/crypto_currency/crypto_currency.dart'; import '../../../../../wallets/crypto_currency/crypto_currency.dart';
import '../../../../../wallets/isar/models/wallet_info.dart'; import '../../../../../wallets/isar/models/wallet_info.dart';
import '../../../../../wallets/isar/providers/wallet_info_provider.dart'; import '../../../../../wallets/isar/providers/wallet_info_provider.dart';
@ -106,117 +105,122 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
} }
} }
late final DSBController _switchController;
bool _switchReuseAddressToggledLock = false; // Mutex. bool _switchReuseAddressToggledLock = false; // Mutex.
Future<void> _switchReuseAddressToggled(bool newValue) async { Future<void> _switchReuseAddressToggled() async {
if (newValue) { if (_switchReuseAddressToggledLock) {
await showDialog( return;
context: context, }
builder: (context) { _switchReuseAddressToggledLock = true; // Lock mutex.
final isDesktop = Util.isDesktop;
return DesktopDialog( try {
maxWidth: 576, if (_switchController.isOn?.call() != true) {
child: Column( final canContinue = await showDialog<bool?>(
mainAxisSize: MainAxisSize.min, context: context,
children: [ builder: (context) {
Row( return DesktopDialog(
mainAxisAlignment: MainAxisAlignment.spaceBetween, maxWidth: 576,
children: [ child: Column(
Padding( mainAxisSize: MainAxisSize.min,
padding: const EdgeInsets.only(left: 32), children: [
child: Text( Row(
"Warning!", mainAxisAlignment: MainAxisAlignment.spaceBetween,
style: STextStyles.desktopH3(context),
),
),
const DesktopDialogCloseButton(),
],
),
Padding(
padding: const EdgeInsets.only(
top: 8,
left: 32,
right: 32,
bottom: 32,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [ children: [
Text( Padding(
"Reusing addresses reduces your privacy and security. Are you sure you want to reuse addresses by default?", padding: const EdgeInsets.only(left: 32),
style: STextStyles.desktopTextSmall(context), child: Text(
), "Warning!",
const SizedBox( style: STextStyles.desktopH3(context),
height: 43, ),
),
Row(
children: [
Expanded(
child: SecondaryButton(
buttonHeight: ButtonHeight.l,
onPressed: () {
Navigator.of(context).pop(false);
},
label: "Cancel",
),
),
const SizedBox(
width: 16,
),
Expanded(
child: PrimaryButton(
buttonHeight: ButtonHeight.l,
onPressed: () {
Navigator.of(context).pop(true);
},
label: "Continue",
),
),
],
), ),
const DesktopDialogCloseButton(),
], ],
), ),
), Padding(
], padding: const EdgeInsets.only(
), top: 8,
); left: 32,
}, right: 32,
).then((confirmed) async { bottom: 32,
if (_switchReuseAddressToggledLock) { ),
return; child: Column(
} mainAxisSize: MainAxisSize.min,
_switchReuseAddressToggledLock = true; // Lock mutex. children: [
Text(
"Reusing addresses reduces your privacy and security. Are you sure you want to reuse addresses by default?",
style: STextStyles.desktopTextSmall(context),
),
const SizedBox(
height: 43,
),
Row(
children: [
Expanded(
child: SecondaryButton(
buttonHeight: ButtonHeight.l,
onPressed: () {
Navigator.of(context).pop(false);
},
label: "Cancel",
),
),
const SizedBox(
width: 16,
),
Expanded(
child: PrimaryButton(
buttonHeight: ButtonHeight.l,
onPressed: () {
Navigator.of(context).pop(true);
},
label: "Continue",
),
),
],
),
],
),
),
],
),
);
},
);
try { if (canContinue == true) {
if (confirmed == true) { await _updateAddressReuse(true);
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
WalletInfoKeys.reuseAddress: true,
},
isar: ref.read(mainDBProvider).isar,
);
} else {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
WalletInfoKeys.reuseAddress: false,
},
isar: ref.read(mainDBProvider).isar,
);
}
} finally {
// ensure _switchReuseAddressToggledLock is set to false no matter what.
_switchReuseAddressToggledLock = false;
} }
}); } else {
} else { await _updateAddressReuse(false);
await ref.read(pWalletInfo(widget.walletId)).updateOtherData( }
newEntries: { } finally {
WalletInfoKeys.reuseAddress: false, // ensure _switchReuseAddressToggledLock is set to false no matter what.
}, _switchReuseAddressToggledLock = false;
isar: ref.read(mainDBProvider).isar,
);
} }
} }
Future<void> _updateAddressReuse(bool shouldReuse) async {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final wallet = ref.watch( final wallet = ref.watch(
@ -370,19 +374,22 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
), ),
// reuseAddress preference. // reuseAddress preference.
_MoreFeaturesItemBase( _MoreFeaturesItemBase(
onPressed: _switchReuseAddressToggled,
child: Row( child: Row(
children: [ children: [
const SizedBox(width: 3), const SizedBox(width: 3),
SizedBox( SizedBox(
height: 20, height: 20,
width: 40, width: 40,
child: DraggableSwitchButton( child: IgnorePointer(
isOn: ref.watch( child: DraggableSwitchButton(
pWalletInfo(widget.walletId) isOn: ref.watch(
.select((value) => value.otherData), pWalletInfo(widget.walletId)
)[WalletInfoKeys.reuseAddress] as bool? ?? .select((value) => value.otherData),
false, )[WalletInfoKeys.reuseAddress] as bool? ??
onValueChanged: _switchReuseAddressToggled, false,
controller: _switchController,
),
), ),
), ),
const SizedBox( const SizedBox(

View file

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