Skip to content

Commit

Permalink
- Fix error popup showing multiple times (#1987)
Browse files Browse the repository at this point in the history
- Improve/generalize error handling
  • Loading branch information
OmarHatem28 authored Jan 30, 2025
1 parent f90b171 commit 1c07048
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 30 deletions.
10 changes: 10 additions & 0 deletions cw_core/lib/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 3 additions & 1 deletion cw_zano/lib/zano_wallet_exceptions.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:cw_core/exceptions.dart';

class ZanoWalletException implements Exception {
final String message;

Expand All @@ -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);
}

Expand Down
88 changes: 60 additions & 28 deletions lib/src/screens/restore/wallet_restore_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<WalletRestorePageBody> createState() =>
_WalletRestorePageBodyState(walletRestoreViewModel, seedSettingsViewModel);
}

class _WalletRestorePageBodyState extends State<WalletRestorePageBody> {
_WalletRestorePageBodyState(this.walletRestoreViewModel, this.seedSettingsViewModel)
: walletRestoreFromSeedFormKey = GlobalKey<WalletRestoreFromSeedFormState>(),
walletRestoreFromKeysFormKey = GlobalKey<WalletRestoreFromKeysFromState>(),
_pages = [],
Expand Down Expand Up @@ -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(
Expand All @@ -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:
Expand All @@ -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;
Expand All @@ -102,20 +135,16 @@ class WalletRestorePage extends BasePage {
final GlobalKey<WalletRestoreFromKeysFromState> 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((_) {
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion lib/view_model/wallet_creation_vm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 1c07048

Please sign in to comment.