/* * This file is part of Stack Wallet. * * Copyright (c) 2023 Cypher Stack * All Rights Reserved. * The code is distributed under GPLv3 license, see LICENSE file for details. * Generated by Cypher Stack on 2023-05-26 * */ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/svg.dart'; import '../../../exceptions/wallet/insufficient_balance_exception.dart'; import '../../../models/paynym/paynym_account_lite.dart'; import '../../../notifications/show_flush_bar.dart'; import '../../../pages_desktop_specific/my_stack_view/paynym/desktop_paynym_send_dialog.dart'; import '../../../providers/global/locale_provider.dart'; import '../../../providers/global/wallets_provider.dart'; import '../../../themes/stack_colors.dart'; import '../../../utilities/assets.dart'; import '../../../utilities/text_styles.dart'; import '../../../wallets/isar/providers/wallet_info_provider.dart'; import '../../../wallets/models/tx_data.dart'; import '../../../wallets/wallet/wallet_mixin_interfaces/paynym_interface.dart'; import '../../../widgets/custom_buttons/blue_text_button.dart'; import '../../../widgets/custom_buttons/paynym_follow_toggle_button.dart'; import '../../../widgets/desktop/desktop_dialog.dart'; import '../../../widgets/desktop/primary_button.dart'; import '../../../widgets/loading_indicator.dart'; import '../../../widgets/qr.dart'; import '../../../widgets/rounded_container.dart'; import '../../../widgets/rounded_white_container.dart'; import '../../send_view/confirm_transaction_view.dart'; import '../dialogs/confirm_paynym_connect_dialog.dart'; import 'paynym_bot.dart'; class DesktopPaynymDetails extends ConsumerStatefulWidget { const DesktopPaynymDetails({ super.key, required this.walletId, required this.accountLite, }); final String walletId; final PaynymAccountLite accountLite; @override ConsumerState createState() => _PaynymDetailsPopupState(); } class _PaynymDetailsPopupState extends ConsumerState { bool _showInsufficientFundsInfo = false; Future _onConnectPressed() async { bool canPop = false; unawaited( showDialog( context: context, builder: (context) => WillPopScope( onWillPop: () async => canPop, child: const LoadingIndicator( width: 200, ), ), ), ); final wallet = ref.read(pWallets).getWallet(widget.walletId) as PaynymInterface; if (await wallet.hasConnected(widget.accountLite.code)) { canPop = true; Navigator.of(context, rootNavigator: true).pop(); // TODO show info popup return; } final rates = await ref.read(pWallets).getWallet(widget.walletId).fees; TxData preparedTx; try { preparedTx = await wallet.prepareNotificationTx( selectedTxFeeRate: rates.medium, targetPaymentCodeString: widget.accountLite.code, ); } on InsufficientBalanceException catch (e) { if (mounted) { canPop = true; Navigator.of(context, rootNavigator: true).pop(); } setState(() { _showInsufficientFundsInfo = true; }); return; } if (mounted) { // We have enough balance and prepared tx should be good to go. canPop = true; // close loading Navigator.of(context, rootNavigator: true).pop(); // show info pop up await showDialog( context: context, builder: (context) => ConfirmPaynymConnectDialog( nymName: widget.accountLite.nymName, locale: ref.read(localeServiceChangeNotifierProvider).locale, onConfirmPressed: () { Navigator.of(context, rootNavigator: true).pop(); unawaited( showDialog( context: context, builder: (context) => DesktopDialog( maxHeight: MediaQuery.of(context).size.height - 64, maxWidth: 580, child: ConfirmTransactionView( walletId: widget.walletId, isPaynymNotificationTransaction: true, txData: preparedTx, onSuccess: () { // do nothing extra }, onSuccessInsteadOfRouteOnSuccess: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.of(context, rootNavigator: true).pop(); unawaited( showFloatingFlushBar( type: FlushBarType.success, message: "Connection initiated to ${widget.accountLite.nymName}", iconAsset: Assets.svg.copy, context: context, ), ); }, ), ), ), ); }, amount: preparedTx.amount! + preparedTx.fee!, coin: ref.read(pWalletCoin(widget.walletId)), ), ); } } Future _onSend() async { await showDialog( context: context, builder: (context) => DesktopPaynymSendDialog( walletId: widget.walletId, accountLite: widget.accountLite, ), ); } @override Widget build(BuildContext context) { final wallet = ref.watch(pWallets).getWallet(widget.walletId); final paynymWallet = wallet as PaynymInterface; return RoundedWhiteContainer( padding: const EdgeInsets.all(0), child: Column( children: [ Padding( padding: const EdgeInsets.all(24), child: Column( mainAxisSize: MainAxisSize.min, children: [ Row( children: [ PayNymBot( paymentCodeString: widget.accountLite.code, size: 36, ), const SizedBox( width: 12, ), Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( widget.accountLite.nymName, style: STextStyles.desktopTextSmall(context), ), FutureBuilder( future: paynymWallet .hasConnected(widget.accountLite.code), builder: (context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done && snapshot.data == true) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const SizedBox( height: 2, ), Text( "Connected", style: STextStyles.desktopTextSmall(context) .copyWith( color: Theme.of(context) .extension()! .accentColorGreen, ), ), ], ); } else { return Container(); } }, ), ], ), ], ), const SizedBox( height: 20, ), Row( children: [ Expanded( child: FutureBuilder( future: paynymWallet.hasConnected(widget.accountLite.code), builder: (context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) { if (snapshot.data!) { return PrimaryButton( label: "Send", buttonHeight: ButtonHeight.s, icon: SvgPicture.asset( Assets.svg.circleArrowUpRight, width: 16, height: 16, color: Theme.of(context) .extension()! .buttonTextPrimary, ), iconSpacing: 6, onPressed: _onSend, ); } else { return PrimaryButton( label: "Connect", buttonHeight: ButtonHeight.s, icon: SvgPicture.asset( Assets.svg.circlePlusFilled, width: 16, height: 16, color: Theme.of(context) .extension()! .buttonTextPrimary, ), iconSpacing: 6, onPressed: _onConnectPressed, ); } } else { return const SizedBox( height: 100, child: LoadingIndicator(), ); } }, ), ), const SizedBox( width: 20, ), kDisableFollowing ? const Spacer() : Expanded( child: PaynymFollowToggleButton( walletId: widget.walletId, paymentCodeStringToFollow: widget.accountLite.code, style: PaynymFollowToggleButtonStyle.detailsDesktop, ), ), ], ), if (_showInsufficientFundsInfo) Column( mainAxisSize: MainAxisSize.min, children: [ const SizedBox( height: 24, ), RoundedContainer( color: Theme.of(context) .extension()! .warningBackground, child: Text( "Adding a PayNym to your contacts requires a one-time " "transaction fee for creating the record on the " "blockchain. Please deposit more " "${ref.watch(pWalletCoin(widget.walletId)).ticker} " "into your wallet and try again.", style: STextStyles.desktopTextExtraExtraSmall(context) .copyWith( color: Theme.of(context) .extension()! .warningForeground, ), ), ), ], ), ], ), ), Container( color: Theme.of(context).extension()!.backgroundAppBar, height: 1, ), Padding( padding: const EdgeInsets.all(24), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "PayNym address", style: STextStyles.desktopTextExtraExtraSmall(context), ), const SizedBox( height: 8, ), Row( children: [ Expanded( child: ConstrainedBox( constraints: const BoxConstraints(minHeight: 100), child: Text( widget.accountLite.code, style: STextStyles.desktopTextExtraExtraSmall(context) .copyWith( color: Theme.of(context) .extension()! .textDark, ), ), ), ), const SizedBox( width: 20, ), QR( padding: const EdgeInsets.all(0), size: 100, data: widget.accountLite.code, ), ], ), const SizedBox( height: 8, ), CustomTextButton( text: "Copy", onTap: () async { await Clipboard.setData( ClipboardData( text: widget.accountLite.code, ), ); unawaited( showFloatingFlushBar( type: FlushBarType.info, message: "Copied to clipboard", iconAsset: Assets.svg.copy, context: context, ), ); }, ), ], ), ), ], ), ); } }