From 1c07048808db94f1d3caeb7f566d3bc92dedd74f Mon Sep 17 00:00:00 2001 From: Omar Hatem Date: Thu, 30 Jan 2025 04:29:55 +0200 Subject: [PATCH] - Fix error popup showing multiple times (#1987) - Improve/generalize error handling --- cw_core/lib/exceptions.dart | 10 +++ cw_zano/lib/zano_wallet_exceptions.dart | 4 +- .../screens/restore/wallet_restore_page.dart | 88 +++++++++++++------ lib/view_model/wallet_creation_vm.dart | 7 +- 4 files changed, 79 insertions(+), 30 deletions(-) diff --git a/cw_core/lib/exceptions.dart b/cw_core/lib/exceptions.dart index 885f5cb2b7..364e332e7e 100644 --- a/cw_core/lib/exceptions.dart +++ b/cw_core/lib/exceptions.dart @@ -56,3 +56,13 @@ class CreateAssociatedTokenAccountException implements Exception { class SignSPLTokenTransactionRentException implements Exception {} class NoAssociatedTokenAccountException implements Exception {} + + +/// ============================================================================== +/// ============================================================================== + +class RestoreFromSeedException implements Exception { + final String message; + + RestoreFromSeedException(this.message); +} diff --git a/cw_zano/lib/zano_wallet_exceptions.dart b/cw_zano/lib/zano_wallet_exceptions.dart index 05af0b0ac2..a0a7b4939f 100644 --- a/cw_zano/lib/zano_wallet_exceptions.dart +++ b/cw_zano/lib/zano_wallet_exceptions.dart @@ -1,3 +1,5 @@ +import 'package:cw_core/exceptions.dart'; + class ZanoWalletException implements Exception { final String message; @@ -6,7 +8,7 @@ class ZanoWalletException implements Exception { String toString() => '${this.runtimeType} (message: $message)'; } -class RestoreFromSeedsException extends ZanoWalletException { +class RestoreFromSeedsException extends RestoreFromSeedException { RestoreFromSeedsException(String message) : super(message); } diff --git a/lib/src/screens/restore/wallet_restore_page.dart b/lib/src/screens/restore/wallet_restore_page.dart index beedee220b..7f34e9f0cd 100644 --- a/lib/src/screens/restore/wallet_restore_page.dart +++ b/lib/src/screens/restore/wallet_restore_page.dart @@ -23,7 +23,51 @@ import 'package:mobx/mobx.dart'; import 'package:smooth_page_indicator/smooth_page_indicator.dart'; class WalletRestorePage extends BasePage { - WalletRestorePage(this.walletRestoreViewModel, this.seedSettingsViewModel) + WalletRestorePage(this.walletRestoreViewModel, this.seedSettingsViewModel); + + @override + Widget middle(BuildContext context) => Observer( + builder: (_) => Text( + walletRestoreViewModel.mode == WalletRestoreMode.seed + ? S.current.restore_title_from_seed + : S.current.restore_title_from_keys, + style: TextStyle( + fontSize: 18.0, + fontWeight: FontWeight.bold, + fontFamily: 'Lato', + color: titleColor(context)), + )); + + final WalletRestoreViewModel walletRestoreViewModel; + final SeedSettingsViewModel seedSettingsViewModel; + + @override + Function(BuildContext)? get pushToNextWidget => (context) { + FocusScopeNode currentFocus = FocusScope.of(context); + if (!currentFocus.hasPrimaryFocus) { + currentFocus.focusedChild?.unfocus(); + } + }; + + @override + Widget body(BuildContext context) { + return WalletRestorePageBody(walletRestoreViewModel, seedSettingsViewModel); + } +} + +class WalletRestorePageBody extends StatefulWidget { + WalletRestorePageBody(this.walletRestoreViewModel, this.seedSettingsViewModel); + + final WalletRestoreViewModel walletRestoreViewModel; + final SeedSettingsViewModel seedSettingsViewModel; + + @override + State createState() => + _WalletRestorePageBodyState(walletRestoreViewModel, seedSettingsViewModel); +} + +class _WalletRestorePageBodyState extends State { + _WalletRestorePageBodyState(this.walletRestoreViewModel, this.seedSettingsViewModel) : walletRestoreFromSeedFormKey = GlobalKey(), walletRestoreFromKeysFormKey = GlobalKey(), _pages = [], @@ -54,8 +98,10 @@ class WalletRestorePage extends BasePage { _validateOnChange(isPolyseed: isPolyseed); }, displayWalletPassword: walletRestoreViewModel.hasWalletPassword, - onPasswordChange: (String password) => walletRestoreViewModel.walletPassword = password, - onRepeatedPasswordChange: (String repeatedPassword) => walletRestoreViewModel.repeatedWalletPassword = repeatedPassword)); + onPasswordChange: (String password) => + walletRestoreViewModel.walletPassword = password, + onRepeatedPasswordChange: (String repeatedPassword) => + walletRestoreViewModel.repeatedWalletPassword = repeatedPassword)); break; case WalletRestoreMode.keys: _pages.add(WalletRestoreFromKeysFrom( @@ -69,8 +115,10 @@ class WalletRestorePage extends BasePage { }, displayPrivateKeyField: walletRestoreViewModel.hasRestoreFromPrivateKey, displayWalletPassword: walletRestoreViewModel.hasWalletPassword, - onPasswordChange: (String password) => walletRestoreViewModel.walletPassword = password, - onRepeatedPasswordChange: (String repeatedPassword) => walletRestoreViewModel.repeatedWalletPassword = repeatedPassword, + onPasswordChange: (String password) => + walletRestoreViewModel.walletPassword = password, + onRepeatedPasswordChange: (String repeatedPassword) => + walletRestoreViewModel.repeatedWalletPassword = repeatedPassword, onHeightOrDateEntered: (value) => walletRestoreViewModel.isButtonEnabled = value)); break; default: @@ -79,21 +127,6 @@ class WalletRestorePage extends BasePage { }); } - bool _formProcessing = false; - - @override - Widget middle(BuildContext context) => Observer( - builder: (_) => Text( - walletRestoreViewModel.mode == WalletRestoreMode.seed - ? S.current.restore_title_from_seed - : S.current.restore_title_from_keys, - style: TextStyle( - fontSize: 18.0, - fontWeight: FontWeight.bold, - fontFamily: 'Lato', - color: titleColor(context)), - )); - final WalletRestoreViewModel walletRestoreViewModel; final SeedSettingsViewModel seedSettingsViewModel; final PageController _controller; @@ -102,20 +135,16 @@ class WalletRestorePage extends BasePage { final GlobalKey walletRestoreFromKeysFormKey; final FocusNode _blockHeightFocusNode; + bool _formProcessing = false; + // DerivationType derivationType = DerivationType.unknown; // String? derivationPath = null; DerivationInfo? derivationInfo; @override - Function(BuildContext)? get pushToNextWidget => (context) { - FocusScopeNode currentFocus = FocusScope.of(context); - if (!currentFocus.hasPrimaryFocus) { - currentFocus.focusedChild?.unfocus(); - } - }; + void initState() { + super.initState(); - @override - Widget body(BuildContext context) { reaction((_) => walletRestoreViewModel.state, (ExecutionState state) { if (state is FailureState) { WidgetsBinding.instance.addPostFrameCallback((_) { @@ -149,7 +178,10 @@ class WalletRestorePage extends BasePage { .currentState!.blockchainHeightKey.currentState!.dateController.text = ''; walletRestoreFromKeysFormKey.currentState!.nameTextEditingController.text = ''; }); + } + @override + Widget build(BuildContext context) { return KeyboardActions( config: KeyboardActionsConfig( keyboardActionsPlatform: KeyboardActionsPlatform.IOS, diff --git a/lib/view_model/wallet_creation_vm.dart b/lib/view_model/wallet_creation_vm.dart index c2cfcbd243..bd6732b4a6 100644 --- a/lib/view_model/wallet_creation_vm.dart +++ b/lib/view_model/wallet_creation_vm.dart @@ -10,6 +10,7 @@ import 'package:cake_wallet/store/app_store.dart'; import 'package:cake_wallet/store/settings_store.dart'; import 'package:cake_wallet/view_model/restore/restore_wallet.dart'; import 'package:cake_wallet/view_model/seed_settings_view_model.dart'; +import 'package:cw_core/exceptions.dart'; import 'package:cw_core/pathForWallet.dart'; import 'package:cw_core/utils/print_verbose.dart'; import 'package:cw_core/wallet_base.dart'; @@ -118,7 +119,11 @@ abstract class WalletCreationVMBase with Store { } catch (e, s) { printV("error: $e"); printV("stack: $s"); - state = FailureState(e.toString()); + String message = e.toString(); + if (e is RestoreFromSeedException) { + message = e.message; + } + state = FailureState(message); } }