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,78 +52,83 @@ class WalletSettingsWalletSettingsView extends ConsumerStatefulWidget {
class _WalletSettingsWalletSettingsViewState
extends ConsumerState<WalletSettingsWalletSettingsView> {
bool _switchReuseAddressToggledLock = false; // Mutex.
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.
late final DSBController _switchController;
try {
if (confirmed == 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 {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
WalletInfoKeys.reuseAddress: false,
},
isar: ref.read(mainDBProvider).isar,
);
bool _switchReuseAddressToggledLock = false; // Mutex.
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) {
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
@ -186,7 +190,7 @@ class _WalletSettingsWalletSettingsViewState
),
),
if (ref.watch(pWallets).getWallet(widget.walletId)
is RbfInterface)
is RbfInterface)
const SizedBox(
height: 8,
),
@ -231,48 +235,47 @@ class _WalletSettingsWalletSettingsViewState
if (ref.watch(pWallets).getWallet(widget.walletId)
is MultiAddressInterface)
RoundedWhiteContainer(
child: Consumer(
builder: (_, ref, __) {
return RawMaterialButton(
// splashColor: Theme.of(context).extension<StackColors>()!.highlight,
materialTapTargetSize:
MaterialTapTargetSize.shrinkWrap,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
padding: const EdgeInsets.all(0),
child: RawMaterialButton(
// splashColor: Theme.of(context).extension<StackColors>()!.highlight,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
shape: RoundedRectangleBorder(
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,
),
),
onPressed: null,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Reuse receiving address",
style: STextStyles.titleBold12(context),
textAlign: TextAlign.left,
SizedBox(
height: 20,
width: 40,
child: IgnorePointer(
child: DraggableSwitchButton(
isOn: ref.watch(
pWalletInfo(widget.walletId).select(
(value) => value.otherData,
),
)[WalletInfoKeys.reuseAddress] as bool? ??
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)

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,117 +105,122 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
}
}
late final DSBController _switchController;
bool _switchReuseAddressToggledLock = false; // Mutex.
Future<void> _switchReuseAddressToggled(bool newValue) async {
if (newValue) {
await showDialog(
context: context,
builder: (context) {
final isDesktop = Util.isDesktop;
return DesktopDialog(
maxWidth: 576,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.only(left: 32),
child: Text(
"Warning!",
style: STextStyles.desktopH3(context),
),
),
const DesktopDialogCloseButton(),
],
),
Padding(
padding: const EdgeInsets.only(
top: 8,
left: 32,
right: 32,
bottom: 32,
),
child: Column(
mainAxisSize: MainAxisSize.min,
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) {
return DesktopDialog(
maxWidth: 576,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
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",
),
),
],
Padding(
padding: const EdgeInsets.only(left: 32),
child: Text(
"Warning!",
style: STextStyles.desktopH3(context),
),
),
const DesktopDialogCloseButton(),
],
),
),
],
),
);
},
).then((confirmed) async {
if (_switchReuseAddressToggledLock) {
return;
}
_switchReuseAddressToggledLock = true; // Lock mutex.
Padding(
padding: const EdgeInsets.only(
top: 8,
left: 32,
right: 32,
bottom: 32,
),
child: Column(
mainAxisSize: MainAxisSize.min,
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 (confirmed == 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;
if (canContinue == true) {
await _updateAddressReuse(true);
}
});
} else {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: {
WalletInfoKeys.reuseAddress: false,
},
isar: ref.read(mainDBProvider).isar,
);
} 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
Widget build(BuildContext context) {
final wallet = ref.watch(
@ -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: DraggableSwitchButton(
isOn: ref.watch(
pWalletInfo(widget.walletId)
.select((value) => value.otherData),
)[WalletInfoKeys.reuseAddress] as bool? ??
false,
onValueChanged: _switchReuseAddressToggled,
child: IgnorePointer(
child: DraggableSwitchButton(
isOn: ref.watch(
pWalletInfo(widget.walletId)
.select((value) => value.otherData),
)[WalletInfoKeys.reuseAddress] as bool? ??
false,
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;
}