diff --git a/lib/data/models/bottom_nav_cubit.dart b/lib/data/models/bottom_nav_cubit.dart index d2bb37df4cd26f4474ae890aa14a69d42a993bc2..d4801d53b63fe2d9c9a3eb704d488e41db53022a 100644 --- a/lib/data/models/bottom_nav_cubit.dart +++ b/lib/data/models/bottom_nav_cubit.dart @@ -9,6 +9,8 @@ class BottomNavCubit extends HydratedCubit<int> { void updateIndex(int index) => emit(index); + int get currentIndex => state; + void getFirstScreen() => emit(0); void getSecondScreen() => emit(1); diff --git a/lib/shared_prefs_helper.dart b/lib/shared_prefs_helper.dart index 8c220250b092b3ec963de3a15932cf91ec27eb06..97c88ebbfbe4e655574a5c86c424e115533d0f1f 100644 --- a/lib/shared_prefs_helper.dart +++ b/lib/shared_prefs_helper.dart @@ -97,6 +97,7 @@ class SharedPreferencesHelper with ChangeNotifier { if (isG1nkgoCard()) { return CesiumWallet.fromSeed(seedFromString(card.seed)); } else { + // This should have the wallet loaded return cesiumVolatileCards[extractPublicKey(card.pubKey)]!; } } else { diff --git a/lib/ui/pay_helper.dart b/lib/ui/pay_helper.dart index d6904df606317323565f8a3b73487d3e46cfb514..f060996764463635726fcc98832690d2eee7b049 100644 --- a/lib/ui/pay_helper.dart +++ b/lib/ui/pay_helper.dart @@ -22,6 +22,7 @@ import 'contacts_cache.dart'; import 'logger.dart'; import 'ui_helpers.dart'; import 'widgets/connectivity_widget_wrapper_wrapper.dart'; +import 'widgets/fifth_screen/import_dialog.dart'; Future<void> payWithRetry( {required BuildContext context, @@ -35,96 +36,115 @@ Future<void> payWithRetry( logger('Trying to pay state with useMempool: $useMempool'); assert(amount > 0); - - final MultiWalletTransactionCubit txCubit = - context.read<MultiWalletTransactionCubit>(); - final PaymentCubit paymentCubit = context.read<PaymentCubit>(); - final AppCubit appCubit = context.read<AppCubit>(); - paymentCubit.sending(); - final String fromPubKey = SharedPreferencesHelper().getPubKey(); - final String contactPubKey = to.pubKey; - final bool? confirmed = await _confirmSend(context, amount.toString(), - humanizeContact(fromPubKey, to, true), isRetry, appCubit.currency); - final Contact fromContact = await ContactsCache().getContact(fromPubKey); - final double convertedAmount = toG1(amount, isG1, currentUd); - - if (confirmed == null || !confirmed) { - paymentCubit.sentFailed(); + bool hasPass = false; + if (!SharedPreferencesHelper().isG1nkgoCard() && + !SharedPreferencesHelper().hasVolatile()) { + hasPass = await showImportCesiumWalletDialog( + context, + SharedPreferencesHelper().getPubKey(), + context.read<BottomNavCubit>().currentIndex) ?? + false; } else { - final Transaction tx = Transaction( - type: TransactionType.pending, - from: fromContact, - to: to, - amount: -toCG1(convertedAmount).toDouble(), - comment: comment, - time: DateTime.now()); - final bool isConnected = await ConnectivityWidgetWrapperWrapper.isConnected; - logger('isConnected: $isConnected'); - if (isConnected != null && !isConnected && !isRetry) { - paymentCubit.sent(); - if (!context.mounted) { - return; - } - showTooltip(context, tr('payment_waiting_internet_title'), - tr('payment_waiting_internet_desc_beta')); - final Transaction pending = - tx.copyWith(type: TransactionType.waitingNetwork); - txCubit.addPendingTransaction(pending); - context.read<BottomNavCubit>().updateIndex(3); - return; - } else { - final PayResult result = await pay( - to: contactPubKey, comment: comment, amount: convertedAmount); - final Transaction pending = tx.copyWith( - debugInfo: - 'Node used: ${result.node != null ? result.node!.url : 'unknown'}'); - if (result.message == 'success') { - paymentCubit.sent(); - // ignore: use_build_context_synchronously - if (!context.mounted) { - return; - } - showTooltip( - context, tr('payment_successful'), tr('payment_successful_desc')); + hasPass = true; + } + if (hasPass) { + if (context.mounted) { + final MultiWalletTransactionCubit txCubit = + context.read<MultiWalletTransactionCubit>(); + final PaymentCubit paymentCubit = context.read<PaymentCubit>(); + final AppCubit appCubit = context.read<AppCubit>(); + paymentCubit.sending(); + final String fromPubKey = SharedPreferencesHelper().getPubKey(); + final String contactPubKey = to.pubKey; + final bool? confirmed = await _confirmSend(context, amount.toString(), + humanizeContact(fromPubKey, to, true), isRetry, appCubit.currency); + final Contact fromContact = await ContactsCache().getContact(fromPubKey); + final double convertedAmount = toG1(amount, isG1, currentUd); - if (!isRetry) { - // Add here the transaction to the pending list (so we can check it the tx is confirmed) + if (confirmed == null || !confirmed) { + paymentCubit.sentFailed(); + } else { + final Transaction tx = Transaction( + type: TransactionType.pending, + from: fromContact, + to: to, + amount: -toCG1(convertedAmount).toDouble(), + comment: comment, + time: DateTime.now()); + final bool isConnected = + await ConnectivityWidgetWrapperWrapper.isConnected; + logger('isConnected: $isConnected'); + if (isConnected != null && !isConnected && !isRetry) { + paymentCubit.sent(); + if (!context.mounted) { + return; + } + showTooltip(context, tr('payment_waiting_internet_title'), + tr('payment_waiting_internet_desc_beta')); + final Transaction pending = + tx.copyWith(type: TransactionType.waitingNetwork); txCubit.addPendingTransaction(pending); + context.read<BottomNavCubit>().updateIndex(3); + return; } else { - // Update the previously failed tx with an update time and type pending - txCubit.updatePendingTransaction(pending); - } - } else { - /* this retry didn't work + final PayResult result = await pay( + to: contactPubKey, comment: comment, amount: convertedAmount); + final Transaction pending = tx.copyWith( + debugInfo: + 'Node used: ${result.node != null ? result.node!.url : 'unknown'}'); + if (result.message == 'success') { + paymentCubit.sent(); + // ignore: use_build_context_synchronously + if (!context.mounted) { + return; + } + showTooltip(context, tr('payment_successful'), + tr('payment_successful_desc')); + + if (!isRetry) { + // Add here the transaction to the pending list (so we can check it the tx is confirmed) + txCubit.addPendingTransaction(pending); + } else { + // Update the previously failed tx with an update time and type pending + txCubit.updatePendingTransaction(pending); + } + } else { + /* this retry didn't work if (!useMempool) { throw RetryException(); } */ - paymentCubit.pendingPayment(); - if (!context.mounted) { - return; - } - final bool failedWithBalance = - result.message == 'insufficient balance' && - weHaveBalance(context, amount); - showPayError( - context, - failedWithBalance - ? tr('payment_error_retry') - : tr('payment_error_desc', namedArgs: <String, String>{ - // We try to translate the error, like "insufficient balance" - 'error': tr(result.message) - })); - if (!isRetry) { - txCubit.insertPendingTransaction( - pending.copyWith(type: TransactionType.failed)); - context.read<BottomNavCubit>().updateIndex(3); - } else { - // Update the previously failed tx with an update time and type pending - txCubit.updatePendingTransaction( - pending.copyWith(type: TransactionType.failed)); + paymentCubit.pendingPayment(); + if (!context.mounted) { + return; + } + final bool failedWithBalance = + result.message == 'insufficient balance' && + weHaveBalance(context, amount); + showPayError( + context, + failedWithBalance + ? tr('payment_error_retry') + : tr('payment_error_desc', namedArgs: <String, String>{ + // We try to translate the error, like "insufficient balance" + 'error': tr(result.message) + })); + if (!isRetry) { + txCubit.insertPendingTransaction( + pending.copyWith(type: TransactionType.failed)); + context.read<BottomNavCubit>().updateIndex(3); + } else { + // Update the previously failed tx with an update time and type pending + txCubit.updatePendingTransaction( + pending.copyWith(type: TransactionType.failed)); + } + } } } } + } else { + if (context.mounted) { + showPayError(context, tr('payment_error_no_pass')); + } } } diff --git a/lib/ui/screens/fifth_screen.dart b/lib/ui/screens/fifth_screen.dart index 305ff48061a046abd2a44efa9d0d48e35db52f76..e6e405884ebd432db563706084331666375ab780 100644 --- a/lib/ui/screens/fifth_screen.dart +++ b/lib/ui/screens/fifth_screen.dart @@ -195,7 +195,7 @@ class _FifthScreenState extends State<FifthScreen> { title: 'import_key', icon: Icons.upload, onTap: () => - showSelectImportMethodDialog(context)), + showSelectImportMethodDialog(context, 0)), ]), SwitchListTile( title: Text(tr('expert_mode')), diff --git a/lib/ui/widgets/cesium_auth_dialog.dart b/lib/ui/widgets/cesium_auth_dialog.dart index 6312824c0a66756de133f021ef8193976139da71..0f0ce521c30c2d43a074e93cc18c468df093b773 100644 --- a/lib/ui/widgets/cesium_auth_dialog.dart +++ b/lib/ui/widgets/cesium_auth_dialog.dart @@ -16,9 +16,11 @@ import '../ui_helpers.dart'; import 'form_error_widget.dart'; class CesiumAddDialog extends StatefulWidget { - const CesiumAddDialog({super.key, required this.publicKey}); + const CesiumAddDialog( + {super.key, required this.publicKey, required this.returnTo}); final String publicKey; + final int returnTo; @override State<CesiumAddDialog> createState() => _CesiumAddDialogState(); @@ -137,7 +139,12 @@ class _CesiumAddDialogState extends State<CesiumAddDialog> { SharedPreferencesHelper().selectCurrentWallet(card); } SharedPreferencesHelper().addCesiumVolatileCard(wallet); - context.read<BottomNavCubit>().updateIndex(0); + if (context.read<BottomNavCubit>().currentIndex != + widget.returnTo) { + context + .read<BottomNavCubit>() + .updateIndex(widget.returnTo); + } _feedbackNotifier.value = ''; Navigator.of(context).pop(true); } diff --git a/lib/ui/widgets/fifth_screen/import_dialog.dart b/lib/ui/widgets/fifth_screen/import_dialog.dart index 946b1298c83b3017ec2cdbff4b5318dcff43ba34..3e7a152e1e340782f75aea028cd2484e9e177dd7 100644 --- a/lib/ui/widgets/fifth_screen/import_dialog.dart +++ b/lib/ui/widgets/fifth_screen/import_dialog.dart @@ -246,7 +246,8 @@ class _ImportDialogState extends State<ImportDialog> { } } -Future<void> showSelectImportMethodDialog(BuildContext context) async { +Future<void> showSelectImportMethodDialog( + BuildContext context, int returnTo) async { final String? method = await showDialog<String>( context: context, builder: (BuildContext context) => const SelectImportMethodDialog(), @@ -266,7 +267,7 @@ Future<void> showSelectImportMethodDialog(BuildContext context) async { if (validateKey(wallet)) { // It's a pubkey, let's think is a cesium wallet if (!SharedPreferencesHelper().has(wallet)) { - showImportCesiumWalletDialog(context, wallet); + showImportCesiumWalletDialog(context, wallet, returnTo); } else { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(tr('wallet_already_imported')))); @@ -286,12 +287,12 @@ Future<void> showSelectImportMethodDialog(BuildContext context) async { } Future<bool?> showImportCesiumWalletDialog( - BuildContext context, String wallet) { + BuildContext context, String wallet, int returnTo) { return showDialog<bool>( context: context, barrierDismissible: false, builder: (BuildContext dialogContext) { - return CesiumAddDialog(publicKey: wallet); + return CesiumAddDialog(publicKey: wallet, returnTo: returnTo); }, ); } diff --git a/lib/ui/widgets/first_screen/card_stack.dart b/lib/ui/widgets/first_screen/card_stack.dart index 62481252e813346d15db85976208ab9afbaac122..c087fdecd622878d700aff9bc22f853da0424404 100644 --- a/lib/ui/widgets/first_screen/card_stack.dart +++ b/lib/ui/widgets/first_screen/card_stack.dart @@ -68,7 +68,7 @@ class _CardStackState extends State<CardStack> { borderRadius: BorderRadius.circular(20), ), */ onPressed: () { - showSelectImportMethodDialog(context); + showSelectImportMethodDialog(context, 0); }, child: const Icon(Icons.add), ), diff --git a/lib/ui/widgets/first_screen/pay_form.dart b/lib/ui/widgets/first_screen/pay_form.dart index 0a72ad453ba4b09f88405e336af55c75446a34dc..5b6cf8b840045b72c5faf4467be9374f44f5cdd1 100644 --- a/lib/ui/widgets/first_screen/pay_form.dart +++ b/lib/ui/widgets/first_screen/pay_form.dart @@ -7,12 +7,10 @@ import '../../../data/models/multi_wallet_transaction_cubit.dart'; import '../../../data/models/payment_cubit.dart'; import '../../../data/models/payment_state.dart'; import '../../../g1/currency.dart'; -import '../../../shared_prefs_helper.dart'; import '../../logger.dart'; import '../../pay_helper.dart'; import '../../tutorial_keys.dart'; import '../../ui_helpers.dart'; -import '../fifth_screen/import_dialog.dart'; import '../form_error_widget.dart'; import 'g1_textfield.dart'; @@ -108,24 +106,11 @@ class _PayFormState extends State<PayForm> { color: sentColor, ), onPressed: () async { - bool hasPass = false; - if (!SharedPreferencesHelper().isG1nkgoCard() && - !SharedPreferencesHelper().hasVolatile()) { - hasPass = await showImportCesiumWalletDialog( - context, - SharedPreferencesHelper().getPubKey()) ?? - false; - } else { - hasPass = true; - } - if (hasPass) { - if (mounted) { - final Future<void> Function()? func = - _onPressed( - state, context, currency, currentUd); - if (func != null) { - func(); - } + if (mounted) { + final Future<void> Function()? func = _onPressed( + state, context, currency, currentUd); + if (func != null) { + func(); } } },