diff --git a/lib/src/screens/restore/restore_options_page.dart b/lib/src/screens/restore/restore_options_page.dart index 472f311c9..d671230c4 100644 --- a/lib/src/screens/restore/restore_options_page.dart +++ b/lib/src/screens/restore/restore_options_page.dart @@ -31,30 +31,45 @@ class RestoreOptionsPage extends BasePage { final bool isNewInstall; + @override + Widget body(BuildContext context) { + return _RestoreOptionsBody(isNewInstall: isNewInstall, themeType: currentTheme.type); + } +} + +class _RestoreOptionsBody extends StatefulWidget { + const _RestoreOptionsBody({required this.isNewInstall, required this.themeType}); + + final bool isNewInstall; + final ThemeType themeType; + + @override + _RestoreOptionsBodyState createState() => _RestoreOptionsBodyState(); +} + +class _RestoreOptionsBodyState extends State<_RestoreOptionsBody> { + bool isRestoring = false; + bool get _doesSupportHardwareWallets { if (!DeviceInfo.instance.isMobile) { return false; } if (isMoneroOnly) { - return DeviceConnectionType.supportedConnectionTypes(WalletType.monero, Platform.isIOS) - .isNotEmpty; + return DeviceConnectionType.supportedConnectionTypes(WalletType.monero, Platform.isIOS).isNotEmpty; } return true; } @override - Widget body(BuildContext context) { + Widget build(BuildContext context) { final mainImageColor = Theme.of(context).extension()!.pageTitleTextColor; final brightImageColor = Theme.of(context).extension()!.textColor; - final imageColor = currentTheme.type == ThemeType.bright ? brightImageColor : mainImageColor; + final imageColor = widget.themeType == ThemeType.bright ? brightImageColor : mainImageColor; final imageLedger = Image.asset('assets/images/ledger_nano.png', width: 40, color: imageColor); final imageSeedKeys = Image.asset('assets/images/restore_wallet_image.png', color: imageColor); final imageBackup = Image.asset('assets/images/backup.png', color: imageColor); - final qrCode = Image.asset('assets/images/restore_qr.png', color: imageColor); - - return Center( child: Container( @@ -66,16 +81,17 @@ class RestoreOptionsPage extends BasePage { children: [ OptionTile( key: ValueKey('restore_options_from_seeds_button_key'), - onPressed: () => Navigator.pushNamed( - context, - Routes.restoreWalletFromSeedKeys, - arguments: isNewInstall, - ), + onPressed: () => + Navigator.pushNamed( + context, + Routes.restoreWalletFromSeedKeys, + arguments: widget.isNewInstall, + ), image: imageSeedKeys, title: S.of(context).restore_title_from_seed_keys, description: S.of(context).restore_description_from_seed_keys, ), - if (isNewInstall) + if (widget.isNewInstall) Padding( padding: EdgeInsets.only(top: 24), child: OptionTile( @@ -91,9 +107,8 @@ class RestoreOptionsPage extends BasePage { padding: EdgeInsets.only(top: 24), child: OptionTile( key: ValueKey('restore_options_from_hardware_wallet_button_key'), - onPressed: () => Navigator.pushNamed( - context, Routes.restoreWalletFromHardwareWallet, - arguments: isNewInstall), + onPressed: () => Navigator.pushNamed(context, Routes.restoreWalletFromHardwareWallet, + arguments: widget.isNewInstall), image: imageLedger, title: S.of(context).restore_title_from_hardware_wallet, description: S.of(context).restore_description_from_hardware_wallet, @@ -119,36 +134,47 @@ class RestoreOptionsPage extends BasePage { } void _onWalletCreateFailure(BuildContext context, String error) { - showPopUp( - context: context, - builder: (BuildContext context) { - return AlertWithOneAction( - alertTitle: S.current.error, - alertContent: error, - buttonText: S.of(context).ok, - buttonAction: () => Navigator.of(context).pop()); - }); + setState(() { + isRestoring = false; + }); + + WidgetsBinding.instance.addPostFrameCallback((_) { + showPopUp( + context: context, + builder: (BuildContext context) { + return AlertWithOneAction( + alertTitle: S.current.error, + alertContent: error, + buttonText: S.of(context).ok, + buttonAction: () => Navigator.of(context).pop()); + }); + }); + } Future _onScanQRCode(BuildContext context) async { - final isCameraPermissionGranted = - await PermissionHandler.checkPermission(Permission.camera, context); + final isCameraPermissionGranted = await PermissionHandler.checkPermission(Permission.camera, context); if (!isCameraPermissionGranted) return; bool isPinSet = false; - if (isNewInstall) { + if (widget.isNewInstall) { await Navigator.pushNamed(context, Routes.setupPin, arguments: (PinCodeState setupPinContext, String _) { - setupPinContext.close(); - isPinSet = true; - }); + setupPinContext.close(); + isPinSet = true; + }); } - if (!isNewInstall || isPinSet) { + if (!widget.isNewInstall || isPinSet) { try { + if (isRestoring) { + return; + } + setState(() { + isRestoring = true; + }); final restoreWallet = await WalletRestoreFromQRCode.scanQRCodeForRestoring(context); - final restoreFromQRViewModel = - getIt.get(param1: restoreWallet.type); + final restoreFromQRViewModel = getIt.get(param1: restoreWallet.type); await restoreFromQRViewModel.create(restoreWallet: restoreWallet); if (restoreFromQRViewModel.state is FailureState) { @@ -160,4 +186,4 @@ class RestoreOptionsPage extends BasePage { } } } -} +} \ No newline at end of file diff --git a/lib/src/widgets/alert_with_no_action.dart.dart b/lib/src/widgets/alert_with_no_action.dart.dart new file mode 100644 index 000000000..623656397 --- /dev/null +++ b/lib/src/widgets/alert_with_no_action.dart.dart @@ -0,0 +1,30 @@ +import 'package:flutter/material.dart'; +import 'package:cake_wallet/src/widgets/base_alert_dialog.dart'; + +class AlertWithNoAction extends BaseAlertDialog { + AlertWithNoAction({ + required this.alertTitle, + required this.alertContent, + this.alertBarrierDismissible = true, + Key? key, + }); + + final String alertTitle; + final String alertContent; + final bool alertBarrierDismissible; + + @override + String get titleText => alertTitle; + + @override + String get contentText => alertContent; + + @override + bool get barrierDismissible => alertBarrierDismissible; + + @override + bool get isBottomDividerExists => false; + + @override + Widget actionButtons(BuildContext context) => Container(height: 60); +} diff --git a/lib/src/widgets/base_alert_dialog.dart b/lib/src/widgets/base_alert_dialog.dart index 2e6f1571e..1b521a427 100644 --- a/lib/src/widgets/base_alert_dialog.dart +++ b/lib/src/widgets/base_alert_dialog.dart @@ -17,6 +17,8 @@ class BaseAlertDialog extends StatelessWidget { bool get isDividerExists => false; + bool get isBottomDividerExists => true; + VoidCallback get actionLeft => () {}; VoidCallback get actionRight => () {}; @@ -205,7 +207,7 @@ class BaseAlertDialog extends StatelessWidget { ) ], ), - const HorizontalSectionDivider(), + if (isBottomDividerExists) const HorizontalSectionDivider(), ClipRRect( borderRadius: BorderRadius.all(Radius.circular(30)), child: actionButtons(context)) diff --git a/lib/view_model/restore/restore_from_qr_vm.dart b/lib/view_model/restore/restore_from_qr_vm.dart index bb7d0f8d1..6701b639d 100644 --- a/lib/view_model/restore/restore_from_qr_vm.dart +++ b/lib/view_model/restore/restore_from_qr_vm.dart @@ -56,12 +56,8 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store WalletCredentials getCredentialsFromRestoredWallet( dynamic options, RestoredWallet restoreWallet) { final password = generateWalletPassword(); - String? passphrase; + DerivationInfo? derivationInfo; - if (options != null) { - derivationInfo = options["derivationInfo"] as DerivationInfo?; - passphrase = options["passphrase"] as String?; - } derivationInfo ??= getDefaultCreateDerivation(); switch (restoreWallet.restoreMode) { @@ -119,7 +115,7 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store name: name, mnemonic: restoreWallet.mnemonicSeed ?? '', password: password, - passphrase: passphrase, + passphrase: restoreWallet.passphrase, derivationType: derivationInfo!.derivationType!, derivationPath: derivationInfo.derivationPath!, ); diff --git a/lib/view_model/restore/restore_wallet.dart b/lib/view_model/restore/restore_wallet.dart index d46c48092..2c2a25005 100644 --- a/lib/view_model/restore/restore_wallet.dart +++ b/lib/view_model/restore/restore_wallet.dart @@ -10,6 +10,7 @@ class RestoredWallet { this.spendKey, this.viewKey, this.mnemonicSeed, + this.passphrase, this.txAmount, this.txDescription, this.recipientName, @@ -23,6 +24,7 @@ class RestoredWallet { final String? spendKey; final String? viewKey; final String? mnemonicSeed; + final String? passphrase; final String? txAmount; final String? txDescription; final String? recipientName; @@ -46,11 +48,13 @@ class RestoredWallet { final height = json['height'] as String?; final mnemonic_seed = json['mnemonic_seed'] as String?; final seed = json['seed'] as String? ?? json['hexSeed'] as String?; + final passphrase = json['passphrase'] as String?; return RestoredWallet( restoreMode: json['mode'] as WalletRestoreMode, type: json['type'] as WalletType, address: json['address'] as String?, mnemonicSeed: mnemonic_seed ?? seed, + passphrase: passphrase, height: height != null ? int.parse(height) : 0, ); }