From 8be96701daa494a956d0f5156dafa1f701ebf580 Mon Sep 17 00:00:00 2001 From: julian-CStack Date: Wed, 12 Apr 2023 17:36:14 -0600 Subject: [PATCH] WIP select firo balance for swap --- .../exchange_step_views/step_4_view.dart | 395 +++++++++++------- 1 file changed, 239 insertions(+), 156 deletions(-) diff --git a/lib/pages/exchange_view/exchange_step_views/step_4_view.dart b/lib/pages/exchange_view/exchange_step_views/step_4_view.dart index 4a86254c4..b7bdf9e46 100644 --- a/lib/pages/exchange_view/exchange_step_views/step_4_view.dart +++ b/lib/pages/exchange_view/exchange_step_views/step_4_view.dart @@ -15,18 +15,22 @@ import 'package:stackwallet/pages/send_view/sub_widgets/building_transaction_dia import 'package:stackwallet/pages/wallet_view/wallet_view.dart'; import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/route_generator.dart'; +import 'package:stackwallet/services/coins/firo/firo_wallet.dart'; import 'package:stackwallet/utilities/amount/amount.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/clipboard_interface.dart'; +import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/widgets/background.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; +import 'package:stackwallet/widgets/desktop/secondary_button.dart'; import 'package:stackwallet/widgets/rounded_container.dart'; import 'package:stackwallet/widgets/rounded_white_container.dart'; import 'package:stackwallet/widgets/stack_dialog.dart'; +import 'package:tuple/tuple.dart'; class Step4View extends ConsumerStatefulWidget { const Step4View({ @@ -120,6 +124,213 @@ class _Step4ViewState extends ConsumerState { } } + Future _showSendFromFiroBalanceSelectSheet(String walletId) async { + final firoWallet = ref + .read(walletsChangeNotifierProvider) + .getManager(walletId) + .wallet as FiroWallet; + final locale = ref.read(localeServiceChangeNotifierProvider).locale; + + return await showModalBottomSheet( + context: context, + backgroundColor: + Theme.of(context).extension()!.backgroundAppBar, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.vertical( + top: Radius.circular( + Constants.size.circularBorderRadius * 3, + ), + ), + ), + builder: (context) { + return Padding( + padding: const EdgeInsets.symmetric( + horizontal: 16, + ), + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + const SizedBox( + height: 32, + ), + Text( + "Select Firo balance", + style: STextStyles.pageTitleH2(context), + ), + const SizedBox( + height: 32, + ), + SecondaryButton( + label: + "${firoWallet.balancePrivate.spendable.localizedStringAsFixed( + locale: locale, + )} (private)", + onPressed: () => Navigator.of(context).pop(false), + ), + const SizedBox( + height: 16, + ), + SecondaryButton( + label: "${firoWallet.balance.spendable.localizedStringAsFixed( + locale: locale, + )} (public)", + onPressed: () => Navigator.of(context).pop(true), + ), + const SizedBox( + height: 32, + ), + ], + ), + ); + }, + ); + } + + Future _confirmSend(Tuple2 tuple) async { + final bool firoPublicSend; + if (tuple.item2 == Coin.firo) { + final result = await _showSendFromFiroBalanceSelectSheet(tuple.item1); + if (result == null) { + return; + } else { + firoPublicSend = result; + } + } else { + firoPublicSend = false; + } + + final manager = + ref.read(walletsChangeNotifierProvider).getManager(tuple.item1); + + final Amount amount = model.sendAmount.toAmount( + fractionDigits: manager.coin.decimals, + ); + final address = model.trade!.payInAddress; + + bool wasCancelled = false; + try { + if (!mounted) return; + + unawaited( + showDialog( + context: context, + useSafeArea: false, + barrierDismissible: false, + builder: (context) { + return BuildingTransactionDialog( + coin: manager.coin, + onCancel: () { + wasCancelled = true; + }, + ); + }, + ), + ); + + final time = Future.delayed( + const Duration( + milliseconds: 2500, + ), + ); + + Future> txDataFuture; + + if (firoPublicSend) { + txDataFuture = (manager.wallet as FiroWallet).prepareSendPublic( + address: address, + amount: amount, + args: { + "feeRate": FeeRateType.average, + // ref.read(feeRateTypeStateProvider) + }, + ); + } else { + txDataFuture = manager.prepareSend( + address: address, + amount: amount, + args: { + "feeRate": FeeRateType.average, + // ref.read(feeRateTypeStateProvider) + }, + ); + } + + final results = await Future.wait([ + txDataFuture, + time, + ]); + + final txData = results.first as Map; + + if (!wasCancelled) { + // pop building dialog + + if (mounted) { + Navigator.of(context).pop(); + } + + txData["note"] = + "${model.trade!.payInCurrency.toUpperCase()}/${model.trade!.payOutCurrency.toUpperCase()} exchange"; + txData["address"] = address; + + if (mounted) { + unawaited( + Navigator.of(context).push( + RouteGenerator.getRoute( + shouldUseMaterialRoute: RouteGenerator.useMaterialPageRoute, + builder: (_) => ConfirmChangeNowSendView( + transactionInfo: txData, + walletId: tuple.item1, + routeOnSuccessName: HomeView.routeName, + trade: model.trade!, + ), + settings: const RouteSettings( + name: ConfirmChangeNowSendView.routeName, + ), + ), + ), + ); + } + } + } catch (e) { + if (mounted && !wasCancelled) { + // pop building dialog + Navigator.of(context).pop(); + + unawaited( + showDialog( + context: context, + useSafeArea: false, + barrierDismissible: true, + builder: (context) { + return StackDialog( + title: "Transaction failed", + message: e.toString(), + rightButton: TextButton( + style: Theme.of(context) + .extension()! + .getSecondaryEnabledButtonStyle(context), + child: Text( + "Ok", + style: STextStyles.button(context).copyWith( + color: Theme.of(context) + .extension()! + .buttonTextSecondary, + ), + ), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ); + }, + ), + ); + } + } + } + @override Widget build(BuildContext context) { final bool isWalletCoin = @@ -244,11 +455,15 @@ class _Step4ViewState extends ConsumerState { text: model.sendAmount.toString()); await clipboard.setData(data); - unawaited(showFloatingFlushBar( - type: FlushBarType.info, - message: "Copied to clipboard", - context: context, - )); + if (mounted) { + unawaited( + showFloatingFlushBar( + type: FlushBarType.info, + message: "Copied to clipboard", + context: context, + ), + ); + } }, child: Row( children: [ @@ -302,11 +517,15 @@ class _Step4ViewState extends ConsumerState { final data = ClipboardData( text: model.trade!.payInAddress); await clipboard.setData(data); - unawaited(showFloatingFlushBar( - type: FlushBarType.info, - message: "Copied to clipboard", - context: context, - )); + if (mounted) { + unawaited( + showFloatingFlushBar( + type: FlushBarType.info, + message: "Copied to clipboard", + context: context, + ), + ); + } }, child: Row( children: [ @@ -365,11 +584,15 @@ class _Step4ViewState extends ConsumerState { final data = ClipboardData( text: model.trade!.tradeId); await clipboard.setData(data); - unawaited(showFloatingFlushBar( - type: FlushBarType.info, - message: "Copied to clipboard", - context: context, - )); + if (mounted) { + unawaited( + showFloatingFlushBar( + type: FlushBarType.info, + message: "Copied to clipboard", + context: context, + ), + ); + } }, child: SvgPicture.asset( Assets.svg.copy, @@ -521,147 +744,7 @@ class _Step4ViewState extends ConsumerState { model.sendTicker.toLowerCase() == tuple.item2.ticker.toLowerCase() ? () async { - final manager = ref - .read( - walletsChangeNotifierProvider) - .getManager(tuple.item1); - - final Amount amount = - model.sendAmount.toAmount( - fractionDigits: - manager.coin.decimals, - ); - final address = - model.trade!.payInAddress; - - try { - bool wasCancelled = false; - - unawaited( - showDialog( - context: context, - useSafeArea: false, - barrierDismissible: false, - builder: (context) { - return BuildingTransactionDialog( - coin: manager.coin, - onCancel: () { - wasCancelled = true; - - Navigator.of(context) - .pop(); - }, - ); - }, - ), - ); - - final time = - Future.delayed( - const Duration( - milliseconds: 2500, - ), - ); - - final txDataFuture = - manager.prepareSend( - address: address, - amount: amount, - args: { - "feeRate": - FeeRateType.average, - // ref.read(feeRateTypeStateProvider) - }, - ); - - final results = - await Future.wait([ - txDataFuture, - time, - ]); - - final txData = results.first - as Map; - - if (!wasCancelled) { - // pop building dialog - - if (mounted) { - Navigator.of(context).pop(); - } - - txData["note"] = - "${model.trade!.payInCurrency.toUpperCase()}/${model.trade!.payOutCurrency.toUpperCase()} exchange"; - txData["address"] = address; - - if (mounted) { - unawaited( - Navigator.of(context) - .push( - RouteGenerator.getRoute( - shouldUseMaterialRoute: - RouteGenerator - .useMaterialPageRoute, - builder: (_) => - ConfirmChangeNowSendView( - transactionInfo: txData, - walletId: tuple.item1, - routeOnSuccessName: - HomeView.routeName, - trade: model.trade!, - ), - settings: - const RouteSettings( - name: - ConfirmChangeNowSendView - .routeName, - ), - ), - )); - } - } - } catch (e) { - // if (mounted) { - // pop building dialog - Navigator.of(context).pop(); - - unawaited(showDialog( - context: context, - useSafeArea: false, - barrierDismissible: true, - builder: (context) { - return StackDialog( - title: "Transaction failed", - message: e.toString(), - rightButton: TextButton( - style: Theme.of(context) - .extension< - StackColors>()! - .getSecondaryEnabledButtonStyle( - context), - child: Text( - "Ok", - style: - STextStyles.button( - context) - .copyWith( - color: Theme.of( - context) - .extension< - StackColors>()! - .buttonTextSecondary, - ), - ), - onPressed: () { - Navigator.of(context) - .pop(); - }, - ), - ); - }, - )); - // } - } + await _confirmSend(tuple); } : () { Navigator.of(context).push(