diff --git a/cw_monero/lib/monero_wallet.dart b/cw_monero/lib/monero_wallet.dart index 86475c2e2..1edaa83d5 100644 --- a/cw_monero/lib/monero_wallet.dart +++ b/cw_monero/lib/monero_wallet.dart @@ -218,7 +218,7 @@ abstract class MoneroWalletBase extends WalletBase( (bool isNewInstall, _) => RestoreOptionsPage(isNewInstall: isNewInstall)); + getIt.registerFactoryParam( + (sweepingWalletPageData, _) => + SweepingWalletPage(sweepingWalletPageData: sweepingWalletPageData), + ); + getIt.registerFactory(() => RestoreFromBackupViewModel(getIt.get())); getIt.registerFactory(() => RestoreFromBackupPage(getIt.get())); diff --git a/lib/router.dart b/lib/router.dart index 39dd75628..b37b24473 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -199,7 +199,10 @@ Route createRoute(RouteSettings settings) { case Routes.sweepingWalletPage: return CupertinoPageRoute( - builder: (_) => getIt.get()); + builder: (_) => getIt.get( + param1: settings.arguments as SweepingWalletPageData, + ), + ); case Routes.dashboard: return CupertinoPageRoute( diff --git a/lib/src/screens/restore/restore_options_page.dart b/lib/src/screens/restore/restore_options_page.dart index 24ab1fd77..e2a818c00 100644 --- a/lib/src/screens/restore/restore_options_page.dart +++ b/lib/src/screens/restore/restore_options_page.dart @@ -1,13 +1,12 @@ import 'package:cake_wallet/core/execution_state.dart'; import 'package:cake_wallet/di.dart'; import 'package:cake_wallet/src/screens/pin_code/pin_code_widget.dart'; +import 'package:cake_wallet/src/screens/restore/sweeping_wallet_page.dart'; import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; -import 'package:cake_wallet/utils/language_list.dart'; +import 'package:cake_wallet/utils/responsive_layout_util.dart'; import 'package:cake_wallet/utils/show_pop_up.dart'; import 'package:cake_wallet/view_model/restore/restore_from_qr_vm.dart'; import 'package:cake_wallet/view_model/restore/wallet_restore_from_qr_code.dart'; -import 'package:cake_wallet/utils/responsive_layout_util.dart'; -import 'package:cake_wallet/wallet_type_utils.dart'; import 'package:flutter/material.dart'; import 'package:cake_wallet/routes.dart'; import 'package:flutter/cupertino.dart'; @@ -37,8 +36,13 @@ class RestoreOptionsPage extends BasePage { child: Column( children: [ RestoreButton( - onPressed: () => Navigator.pushNamed(context, Routes.restoreWalletFromSeedKeys, - arguments: isNewInstall), + onPressed: () { + Navigator.pushNamed( + context, + Routes.restoreWalletFromSeedKeys, + arguments: isNewInstall, + ); + }, image: imageSeedKeys, title: S.of(context).restore_title_from_seed_keys, description: S.of(context).restore_description_from_seed_keys), @@ -49,7 +53,9 @@ class RestoreOptionsPage extends BasePage { onPressed: () => Navigator.pushNamed(context, Routes.restoreFromBackup), image: imageBackup, title: S.of(context).restore_title_from_backup, - description: S.of(context).restore_description_from_backup), + description: + S.of(context).restore_description_from_backup, + ), ), Padding( padding: EdgeInsets.only(top: 24), @@ -61,24 +67,46 @@ class RestoreOptionsPage extends BasePage { arguments: (PinCodeState setupPinContext, String _) { setupPinContext.close(); isPinSet = true; - }); + }, + ); } - if (!isNewInstall || isPinSet) { + + if (!isNewInstall || isPinSet) { try { - final restoreWallet = - await WalletRestoreFromQRCode.scanQRCodeForRestoring(context); + final restoreWallet = await WalletRestoreFromQRCode + .scanQRCodeForRestoring(context); final restoreFromQRViewModel = - getIt.get(param1: restoreWallet.type); + getIt.get( + param1: restoreWallet.type, + ); - await restoreFromQRViewModel.create(restoreWallet: restoreWallet); + if (restoreWallet.txId != null && + restoreWallet.txId!.isNotEmpty) { + Navigator.pushNamed( + context, + Routes.sweepingWalletPage, + arguments: SweepingWalletPageData( + restorationFromQRVM: restoreFromQRViewModel, + restoredWallet: restoreWallet, + ), + ); + } else { + await restoreFromQRViewModel.create( + restoreWallet: restoreWallet, + ); - if (restoreFromQRViewModel.state is FailureState) { - final errorState = restoreFromQRViewModel.state as FailureState; - _onWalletCreateFailure(context, - 'Create wallet state: ${errorState.error}'); + if (restoreFromQRViewModel.state + is FailureState) { + final errorState = restoreFromQRViewModel.state + as FailureState; + + _onWalletCreateFailure( + context, + 'Create wallet state: ${errorState.error}', + ); + } } - } catch (e) { _onWalletCreateFailure(context, e.toString()); } @@ -94,15 +122,19 @@ 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()); - }); + void _onWalletCreateFailure(BuildContext context, String error) async { + await showPopUp( + context: context, + builder: (BuildContext context) { + return AlertWithOneAction( + alertTitle: S.current.error, + alertContent: error, + buttonText: S.of(context).ok, + buttonAction: () => Navigator.of(context).pop(), + ); + }, + ); + + Navigator.pop(context); } } diff --git a/lib/src/screens/restore/sweeping_wallet_page.dart b/lib/src/screens/restore/sweeping_wallet_page.dart index 96887b955..e801a8a4a 100644 --- a/lib/src/screens/restore/sweeping_wallet_page.dart +++ b/lib/src/screens/restore/sweeping_wallet_page.dart @@ -1,18 +1,23 @@ +import 'package:cake_wallet/core/execution_state.dart'; +import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; import 'package:cake_wallet/themes/theme_base.dart'; +import 'package:cake_wallet/utils/show_pop_up.dart'; +import 'package:cake_wallet/view_model/restore/restore_from_qr_vm.dart'; +import 'package:cake_wallet/view_model/restore/restore_wallet.dart'; import 'package:flutter/material.dart'; import 'package:cake_wallet/src/screens/base_page.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:flutter/scheduler.dart'; class SweepingWalletPage extends BasePage { - SweepingWalletPage(); + SweepingWalletPage({required this.sweepingWalletPageData}); + + final SweepingWalletPageData sweepingWalletPageData; static const aspectRatioImage = 1.25; final welcomeImageLight = Image.asset('assets/images/welcome_light.png'); final welcomeImageDark = Image.asset('assets/images/welcome.png'); - - @override Widget build(BuildContext context) { return Scaffold( @@ -23,23 +28,31 @@ class SweepingWalletPage extends BasePage { @override Widget body(BuildContext context) { - final welcomeImage = currentTheme.type == ThemeType.dark ? welcomeImageDark : welcomeImageLight; + final welcomeImage = currentTheme.type == ThemeType.dark + ? welcomeImageDark + : welcomeImageLight; return SweepingWalletWidget( - aspectRatioImage: aspectRatioImage, welcomeImage: welcomeImage, + restoredWallet: sweepingWalletPageData.restoredWallet, + aspectRatioImage: aspectRatioImage, + restoreFromQRViewModel: sweepingWalletPageData.restorationFromQRVM, ); } } class SweepingWalletWidget extends StatefulWidget { const SweepingWalletWidget({ - required this.aspectRatioImage, required this.welcomeImage, + required this.restoredWallet, + required this.aspectRatioImage, + required this.restoreFromQRViewModel, }); - final double aspectRatioImage; final Image welcomeImage; + final double aspectRatioImage; + final RestoredWallet restoredWallet; + final WalletRestorationFromQRVM restoreFromQRViewModel; @override State createState() => _SweepingWalletWidgetState(); @@ -49,11 +62,42 @@ class _SweepingWalletWidgetState extends State { @override void initState() { SchedulerBinding.instance.addPostFrameCallback((_) async { - + await _initializeRestoreFromQR(); }); super.initState(); } + Future _initializeRestoreFromQR() async { + try { + await widget.restoreFromQRViewModel + .createFlowForSweepAll(restoreWallet: widget.restoredWallet); + + if (widget.restoreFromQRViewModel.state is FailureState) { + final errorState = widget.restoreFromQRViewModel.state as FailureState; + _onWalletCreateFailure( + context, 'Create wallet state: ${errorState.error}'); + } + } catch (e) { + _onWalletCreateFailure(context, e.toString()); + } + } + + void _onWalletCreateFailure(BuildContext context, String error) async { + await showPopUp( + context: context, + builder: (BuildContext context) { + return AlertWithOneAction( + alertTitle: S.current.error, + alertContent: error, + buttonText: S.of(context).ok, + buttonAction: () => Navigator.of(context).pop(), + ); + }, + ); + + Navigator.pop(context); + } + @override Widget build(BuildContext context) { return WillPopScope( @@ -61,15 +105,20 @@ class _SweepingWalletWidgetState extends State { child: Container( padding: EdgeInsets.only(top: 64, bottom: 24, left: 24, right: 24), child: Column( - mainAxisAlignment: MainAxisAlignment.spaceBetween, + mainAxisAlignment: MainAxisAlignment.center, children: [ Flexible( - flex: 2, - child: AspectRatio( - aspectRatio: widget.aspectRatioImage, - child: FittedBox(child: widget.welcomeImage, fit: BoxFit.fill))), + flex: 4, + child: AspectRatio( + aspectRatio: widget.aspectRatioImage, + child: FittedBox( + child: widget.welcomeImage, + fit: BoxFit.fill, + ), + ), + ), Flexible( - flex: 3, + flex: 2, child: Column( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ @@ -83,7 +132,7 @@ class _SweepingWalletWidgetState extends State { fontSize: 18, fontWeight: FontWeight.w500, color: Theme.of(context) - .accentTextTheme! + .accentTextTheme .displayMedium! .color, ), @@ -98,7 +147,7 @@ class _SweepingWalletWidgetState extends State { fontSize: 36, fontWeight: FontWeight.bold, color: Theme.of(context) - .primaryTextTheme! + .primaryTextTheme .titleLarge! .color!, ), @@ -113,7 +162,7 @@ class _SweepingWalletWidgetState extends State { fontSize: 16, fontWeight: FontWeight.w500, color: Theme.of(context) - .accentTextTheme! + .accentTextTheme .displayMedium! .color, ), @@ -129,4 +178,12 @@ class _SweepingWalletWidgetState extends State { } } +class SweepingWalletPageData { + final WalletRestorationFromQRVM restorationFromQRVM; + final RestoredWallet restoredWallet; + SweepingWalletPageData({ + required this.restorationFromQRVM, + required this.restoredWallet, + }); +} diff --git a/lib/view_model/wallet_creation_vm.dart b/lib/view_model/wallet_creation_vm.dart index 58b3c05a9..847f584be 100644 --- a/lib/view_model/wallet_creation_vm.dart +++ b/lib/view_model/wallet_creation_vm.dart @@ -68,18 +68,7 @@ abstract class WalletCreationVMBase with Store { bool typeExists(WalletType type) => walletCreationService.typeExists(type); Future create({dynamic options, RestoredWallet? restoreWallet}) async { - // if (restoreWallet != null && - // restoreWallet.restoreMode == WalletRestoreMode.txids) { - await _createFlowForSweepAll(options, restoreWallet); - // } - - // await _createTransactionFlowNormally(options, restoreWallet); - } - - Future _createTransactionFlowNormally( - dynamic options, - RestoredWallet? restoreWallet, - ) async { + print('Inside normal create function'); try { final restoredWallet = await _createNewWalletWithoutSwitching( options: options, @@ -104,10 +93,11 @@ abstract class WalletCreationVMBase with Store { } } - Future _createFlowForSweepAll( + Future createFlowForSweepAll({ dynamic options, RestoredWallet? restoreWallet, - ) async { + }) async { + print('Inside sweep all create function'); state = IsExecutingState(); final type = restoreWallet?.type ?? this.type; @@ -195,7 +185,7 @@ abstract class WalletCreationVMBase with Store { name: name, type: type, //TODO(David): Ask Omar about this, was previous isRecovery - isRecovery: restoreWallet != null ? true : false, + isRecovery: restoreWallet != null, restoreHeight: credentials.height ?? 0, date: DateTime.now(), path: path, @@ -311,7 +301,8 @@ abstract class WalletCreationVMBase with Store { Future _createTransaction(WalletBase wallet, Object credentials) async { try { print('about to enter wallet create transaction function'); - pendingTransaction = await wallet.createTransaction(credentials); + pendingTransaction = + await wallet.createTransactionForSweepAll(credentials); } catch (e) { state = FailureState(e.toString()); }