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
lib
pages/settings_views/wallet_settings_view/wallet_settings_wallet_settings
pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/more_features
widgets/custom_buttons

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,13 +52,20 @@ class WalletSettingsWalletSettingsView extends ConsumerStatefulWidget {
class _WalletSettingsWalletSettingsViewState class _WalletSettingsWalletSettingsViewState
extends ConsumerState<WalletSettingsWalletSettingsView> { extends ConsumerState<WalletSettingsWalletSettingsView> {
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;
}
_switchReuseAddressToggledLock = true; // Lock mutex.
try {
if (_switchController.isOn?.call() != true) {
final canContinue = await showDialog<bool?>(
context: context, context: context,
builder: (context) { builder: (context) {
final isDesktop = Util.isDesktop;
return StackDialog( return StackDialog(
title: "Warning!", title: "Warning!",
message: message:
@ -90,42 +96,40 @@ class _WalletSettingsWalletSettingsViewState
), ),
); );
}, },
).then((confirmed) async { );
if (_switchReuseAddressToggledLock) {
return;
}
_switchReuseAddressToggledLock = true; // Lock mutex.
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 { } else {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData( await _updateAddressReuse(false);
newEntries: {
WalletInfoKeys.reuseAddress: false,
},
isar: ref.read(mainDBProvider).isar,
);
} }
} finally { } finally {
// ensure _switchReuseAddressToggledLock is set to false no matter what. // ensure _switchReuseAddressToggledLock is set to false no matter what.
_switchReuseAddressToggledLock = false; _switchReuseAddressToggledLock = false;
} }
}); }
} else {
Future<void> _updateAddressReuse(bool shouldReuse) async {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData( await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: { newEntries: {
WalletInfoKeys.reuseAddress: false, WalletInfoKeys.reuseAddress: shouldReuse,
}, },
isar: ref.read(mainDBProvider).isar, 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) {
@ -231,20 +235,21 @@ 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: MaterialTapTargetSize.shrinkWrap,
MaterialTapTargetSize.shrinkWrap,
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular( borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius, Constants.size.circularBorderRadius,
), ),
), ),
onPressed: null, onPressed: _switchReuseAddressToggled,
child: Padding( child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8), padding: const EdgeInsets.symmetric(
horizontal: 12.0,
vertical: 20,
),
child: Row( child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ children: [
@ -256,23 +261,21 @@ class _WalletSettingsWalletSettingsViewState
SizedBox( SizedBox(
height: 20, height: 20,
width: 40, width: 40,
child: IgnorePointer(
child: DraggableSwitchButton( child: DraggableSwitchButton(
isOn: ref.watch( isOn: ref.watch(
pWalletInfo(widget.walletId).select( pWalletInfo(widget.walletId).select(
(value) => value.otherData), (value) => value.otherData,
)[WalletInfoKeys.reuseAddress] ),
as bool? ?? )[WalletInfoKeys.reuseAddress] as bool? ??
false, false,
onValueChanged: (newValue) { controller: _switchController,
_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,13 +105,20 @@ 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;
}
_switchReuseAddressToggledLock = true; // Lock mutex.
try {
if (_switchController.isOn?.call() != true) {
final canContinue = await showDialog<bool?>(
context: context, context: context,
builder: (context) { builder: (context) {
final isDesktop = Util.isDesktop;
return DesktopDialog( return DesktopDialog(
maxWidth: 576, maxWidth: 576,
child: Column( child: Column(
@ -180,42 +186,40 @@ class _MoreFeaturesDialogState extends ConsumerState<MoreFeaturesDialog> {
), ),
); );
}, },
).then((confirmed) async { );
if (_switchReuseAddressToggledLock) {
return;
}
_switchReuseAddressToggledLock = true; // Lock mutex.
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 { } else {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData( await _updateAddressReuse(false);
newEntries: {
WalletInfoKeys.reuseAddress: false,
},
isar: ref.read(mainDBProvider).isar,
);
} }
} finally { } finally {
// ensure _switchReuseAddressToggledLock is set to false no matter what. // ensure _switchReuseAddressToggledLock is set to false no matter what.
_switchReuseAddressToggledLock = false; _switchReuseAddressToggledLock = false;
} }
}); }
} else {
Future<void> _updateAddressReuse(bool shouldReuse) async {
await ref.read(pWalletInfo(widget.walletId)).updateOtherData( await ref.read(pWalletInfo(widget.walletId)).updateOtherData(
newEntries: { newEntries: {
WalletInfoKeys.reuseAddress: false, WalletInfoKeys.reuseAddress: shouldReuse,
}, },
isar: ref.read(mainDBProvider).isar, 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) {
@ -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: IgnorePointer(
child: DraggableSwitchButton( child: DraggableSwitchButton(
isOn: ref.watch( isOn: ref.watch(
pWalletInfo(widget.walletId) pWalletInfo(widget.walletId)
.select((value) => value.otherData), .select((value) => value.otherData),
)[WalletInfoKeys.reuseAddress] as bool? ?? )[WalletInfoKeys.reuseAddress] as bool? ??
false, false,
onValueChanged: _switchReuseAddressToggled, 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;
} }