| 1 | import 'package:cake_wallet/bitcoin/bitcoin.dart'; |
| 2 | import 'package:cake_wallet/core/open_crypto_pay/open_cryptopay_service.dart'; |
| 3 | import 'package:cake_wallet/di.dart'; |
| 4 | import 'package:cake_wallet/entities/qr_scanner.dart'; |
| 5 | import 'package:cake_wallet/generated/i18n.dart'; |
| 6 | import 'package:cake_wallet/new-ui/modal_navigator.dart'; |
| 7 | import 'package:cake_wallet/new-ui/pages/send_page.dart'; |
| 8 | import 'package:cake_wallet/new-ui/pages/swap_page.dart'; |
| 9 | import 'package:cake_wallet/reactions/wallet_connect.dart'; |
| 10 | import 'package:cake_wallet/routes.dart'; |
| 11 | import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; |
| 12 | import 'package:cake_wallet/src/widgets/cake_image_widget.dart'; |
| 13 | import 'package:cake_wallet/utils/feature_flag.dart'; |
| 14 | import 'package:cake_wallet/utils/payment_request.dart'; |
| 15 | import 'package:cake_wallet/utils/show_pop_up.dart'; |
| 16 | import 'package:cake_wallet/view_model/send/send_view_model.dart'; |
| 17 | import 'package:cake_wallet/view_model/wallet_address_list/wallet_address_list_view_model.dart'; |
| 18 | import 'package:cw_core/crypto_currency.dart'; |
| 19 | import 'package:cw_core/lnurl.dart'; |
| 20 | import 'package:cw_core/node.dart'; |
| 21 | import 'package:cw_core/unspent_coin_type.dart'; |
| 22 | import 'package:cw_core/wallet_type.dart'; |
| 23 | import 'package:flutter/material.dart'; |
| 24 | import 'package:modal_bottom_sheet/modal_bottom_sheet.dart'; |
| 25 | |
| 26 | import '../../../pages/receive_page.dart'; |
| 27 | import 'coin_action_button.dart'; |
| 28 | |
| 29 | class CoinActionRow extends StatelessWidget { |
| 30 | const CoinActionRow( |
| 31 | {super.key, this.lightningMode = false, this.showSwap = true, required this.walletType}); |
| 32 | |
| 33 | final bool lightningMode; |
| 34 | final bool showSwap; |
| 35 | final WalletType walletType; |
| 36 | |
| 37 | @override |
| 38 | Widget build(BuildContext context) { |
| 39 | return Padding( |
| 40 | padding: const EdgeInsets.symmetric(horizontal: 18.0), |
| 41 | child: Row( |
| 42 | mainAxisSize: MainAxisSize.max, |
| 43 | mainAxisAlignment: MainAxisAlignment.center, |
| 44 | spacing: MediaQuery.of(context).size.width * 0.05, |
| 45 | children: [ |
| 46 | CoinActionButton( |
| 47 | icon: CakeImageWidget( |
| 48 | imageUrl: "assets/new-ui/send.svg", |
| 49 | colorFilter: ColorFilter.mode( |
| 50 | Theme.of(context).colorScheme.primary, |
| 51 | BlendMode.srcIn, |
| 52 | ), |
| 53 | ), |
| 54 | label: S.of(context).send, |
| 55 | action: () { |
| 56 | if (FeatureFlag.hasNewUiExtraPages) { |
| 57 | final sendPage = getIt.get<NewSendPage>( |
| 58 | param1: SendPageParams( |
| 59 | unspentCoinType: |
| 60 | lightningMode ? UnspentCoinType.lightning : UnspentCoinType.nonMweb, |
| 61 | ), |
| 62 | ); |
| 63 | |
| 64 | CupertinoScaffold.showCupertinoModalBottomSheet( |
| 65 | context: context, |
| 66 | isDismissible: true, |
| 67 | barrierColor: Colors.black.withAlpha(60), |
| 68 | builder: (context) { |
| 69 | return Material( |
| 70 | child: ModalNavigator( |
| 71 | rootPage: sendPage, |
| 72 | parentContext: context, |
| 73 | ), |
| 74 | ); |
| 75 | }, |
| 76 | ); |
| 77 | } else { |
| 78 | Map<String, dynamic>? args; |
| 79 | if (lightningMode) args = {'coinTypeToSpendFrom': UnspentCoinType.lightning}; |
| 80 | Navigator.of(context).pushNamed(Routes.send, arguments: args); |
| 81 | } |
| 82 | }, |
| 83 | ), |
| 84 | CoinActionButton( |
| 85 | icon: CakeImageWidget( |
| 86 | imageUrl: "assets/new-ui/receive.svg", |
| 87 | colorFilter: ColorFilter.mode( |
| 88 | Theme.of(context).colorScheme.primary, |
| 89 | BlendMode.srcIn, |
| 90 | ), |
| 91 | ), |
| 92 | label: S.of(context).receive, |
| 93 | action: () async { |
| 94 | if (FeatureFlag.hasNewUiExtraPages) { |
| 95 | final page = getIt.get<NewReceivePage>(param1: lightningMode); |
| 96 | CupertinoScaffold.showCupertinoModalBottomSheet( |
| 97 | context: context, |
| 98 | barrierColor: Colors.black.withAlpha(60), |
| 99 | builder: (context) { |
| 100 | return Material(child: ModalNavigator(parentContext: context, rootPage: page)); |
| 101 | }, |
| 102 | ); |
| 103 | } else { |
| 104 | // ToDo: (Konsti) refactor as part of the derivation PR (I hate myself for it) |
| 105 | if (lightningMode) { |
| 106 | await getIt<WalletAddressListViewModel>().setAddressType( |
| 107 | bitcoin!.getOptionToType(bitcoin!.getBitcoinLightningReceivePageOption())); |
| 108 | } else { |
| 109 | await getIt<WalletAddressListViewModel>().setAddressType( |
| 110 | bitcoin!.getOptionToType(bitcoin!.getBitcoinSegwitPageOption())); |
| 111 | } |
| 112 | Navigator.of(context).pushNamed(Routes.addressPage); |
| 113 | } |
| 114 | }, |
| 115 | ), |
| 116 | if (showSwap) |
| 117 | CoinActionButton( |
| 118 | icon: CakeImageWidget( |
| 119 | imageUrl: "assets/new-ui/exchange.svg", |
| 120 | colorFilter: ColorFilter.mode( |
| 121 | Theme.of(context).colorScheme.primary, |
| 122 | BlendMode.srcIn, |
| 123 | ), |
| 124 | ), |
| 125 | label: S.of(context).swap, |
| 126 | action: () { |
| 127 | final page = |
| 128 | getIt.get<NewSwapPage>(param2: lightningMode ? CryptoCurrency.btcln : null); |
| 129 | if (FeatureFlag.hasNewUiExtraPages) { |
| 130 | CupertinoScaffold.showCupertinoModalBottomSheet( |
| 131 | context: context, |
| 132 | barrierColor: Colors.black.withAlpha(85), |
| 133 | builder: (context) => Material( |
| 134 | child: ModalNavigator( |
| 135 | rootPage: page, |
| 136 | parentContext: context, |
| 137 | )), |
| 138 | ); |
| 139 | } else { |
| 140 | Navigator.of(context).pushNamed(Routes.exchange); |
| 141 | } |
| 142 | }, |
| 143 | ), |
| 144 | CoinActionButton( |
| 145 | icon: CakeImageWidget( |
| 146 | imageUrl: "assets/new-ui/scan.svg", |
| 147 | colorFilter: ColorFilter.mode( |
| 148 | Theme.of(context).colorScheme.primary, |
| 149 | BlendMode.srcIn, |
| 150 | ), |
| 151 | ), |
| 152 | label: S.of(context).scan, |
| 153 | action: () => _onPressedScan(context), |
| 154 | ), |
| 155 | ], |
| 156 | ), |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | Future<void> _onPressedScan(BuildContext context) async { |
| 161 | final code = await presentQRScanner(context, showHelp: true); |
| 162 | |
| 163 | if (code == null || code.isEmpty) return; |
| 164 | |
| 165 | PaymentRequest? req; |
| 166 | String? rawInput; |
| 167 | var unspentCoinType = UnspentCoinType.any; |
| 168 | if (SendViewModelBase.isNonZeroAmountLightningInvoice(code)) { |
| 169 | unspentCoinType = UnspentCoinType.lightning; |
| 170 | final amount = getBolt11Amount(code)?.toString() ?? "0"; |
| 171 | req = PaymentRequest(code, amount, "", "", ""); |
| 172 | } else if (SendViewModelBase.isLnurlInvoice(code)) { |
| 173 | unspentCoinType = UnspentCoinType.lightning; |
| 174 | final amount = (await LNURL.getPayRequestAmount(code))?.toString() ?? "0"; |
| 175 | req = PaymentRequest(code, amount, "", "", ""); |
| 176 | } else if (Uri.tryParse(code)?.scheme == "wc") { |
| 177 | if (!isWalletConnectCompatibleChain(walletType)) { |
| 178 | showPopUp<void>( |
| 179 | context: context, |
| 180 | builder: (context) => AlertWithOneAction( |
| 181 | alertTitle: "WalletConnect", |
| 182 | alertContent: |
| 183 | S.of(context).switchToWCCompatibleWallet(walletConnectCompatibleChainsLabel()), |
| 184 | buttonText: "OK", |
| 185 | buttonAction: Navigator.of(context).pop)); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | Navigator.of(context) |
| 190 | .pushNamed(Routes.walletConnectConnectionsListing, arguments: Uri.parse(code)); |
| 191 | return; |
| 192 | } else if (!OpenCryptoPayService.isOpenCryptoPayQR(code) && |
| 193 | ["http", "https", "tcp"].contains(Uri.tryParse(code)?.scheme)) { |
| 194 | Navigator.of(context).pushNamed(Routes.newNode, |
| 195 | arguments: {"editingNode": Node.fromUri(Uri.parse(code), walletType)}); |
| 196 | return; |
| 197 | } else { |
| 198 | rawInput = code; |
| 199 | } |
| 200 | |
| 201 | final sendPage = getIt.get<NewSendPage>( |
| 202 | param1: SendPageParams( |
| 203 | initialPaymentRequest: req, |
| 204 | initialRawInput: rawInput, |
| 205 | unspentCoinType: unspentCoinType, |
| 206 | ), |
| 207 | ); |
| 208 | |
| 209 | CupertinoScaffold.showCupertinoModalBottomSheet( |
| 210 | context: context, |
| 211 | barrierColor: Colors.black.withAlpha(60), |
| 212 | builder: (context) => Material( |
| 213 | child: ModalNavigator( |
| 214 | rootPage: sendPage, |
| 215 | parentContext: context, |
| 216 | ), |
| 217 | ), |
| 218 | ); |
| 219 | } |
| 220 | } |