From 3fd8430e83da363b4a64ef4a87f51541b32baac2 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Wed, 6 Mar 2024 22:10:40 -0800 Subject: [PATCH] fixes --- lib/di.dart | 19 +++-- .../receive/lightning_invoice_page.dart | 14 ++-- .../receive/lightning_receive_page.dart | 20 +++-- .../send/lightning_send_confirm_page.dart | 19 ++--- lib/src/screens/send/lightning_send_page.dart | 5 +- .../lightning_invoice_page_view_model.dart | 6 +- lib/view_model/lightning_send_view_model.dart | 43 +++++++++++ lib/view_model/lightning_view_model.dart | 75 ++++++++++--------- 8 files changed, 128 insertions(+), 73 deletions(-) create mode 100644 lib/view_model/lightning_send_view_model.dart diff --git a/lib/di.dart b/lib/di.dart index f47410cb6..7f2404422 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -14,6 +14,7 @@ import 'package:cake_wallet/core/yat_service.dart'; import 'package:cake_wallet/entities/background_tasks.dart'; import 'package:cake_wallet/entities/exchange_api_mode.dart'; import 'package:cake_wallet/entities/parse_address_from_domain.dart'; +import 'package:cake_wallet/view_model/LightningSendViewModel.dart'; import 'package:cw_core/receive_page_option.dart'; import 'package:cake_wallet/ethereum/ethereum.dart'; import 'package:cake_wallet/lightning/lightning.dart'; @@ -1205,10 +1206,16 @@ Future setup({ getIt.registerFactory(() => NFTViewModel(appStore, getIt.get())); getIt.registerFactory(() => TorPage(getIt.get())); - getIt.registerFactory(() => LightningViewModel( - settingsStore: getIt.get(), - fiatConversionStore: getIt.get(), - )); + getIt.registerFactory( + () => LightningViewModel(), + ); + + getIt.registerFactory( + () => LightningSendViewModel( + settingsStore: getIt.get(), + fiatConversionStore: getIt.get(), + ), + ); getIt.registerFactoryParam((_, __) { return LightningInvoicePageViewModel( @@ -1246,14 +1253,14 @@ Future setup({ () => CryptoCurrency.btcln, ), authService: getIt.get(), - lightningViewModel: getIt.get(), + lightningSendViewModel: getIt.get(), ); }); getIt.registerFactoryParam((LNInvoice invoice, _) { return LightningSendConfirmPage( invoice: invoice, - lightningViewModel: getIt.get(), + lightningSendViewModel: getIt.get(), ); }); diff --git a/lib/src/screens/receive/lightning_invoice_page.dart b/lib/src/screens/receive/lightning_invoice_page.dart index 784afec5f..b739d70ad 100644 --- a/lib/src/screens/receive/lightning_invoice_page.dart +++ b/lib/src/screens/receive/lightning_invoice_page.dart @@ -169,18 +169,16 @@ class LightningInvoicePage extends BasePage { Container(child: Center(child: CircularProgressIndicator()))); } late String finalText; - int balance = (snapshot.data as List)[2]; - if (balance == 0) { - int min = (snapshot.data as List)[0]; + InvoiceSoftLimitsResult limits = + snapshot.data as InvoiceSoftLimitsResult; + if (limits.balance == 0) { finalText = S .of(context) - .lightning_invoice_min(lightning!.satsToLightningString(min)); + .lightning_invoice_min(lightning!.satsToLightningString(limits.min)); } else { - int min = (snapshot.data as List)[0]; - int max = (snapshot.data as List)[1]; finalText = S.of(context).lightning_invoice_min_max( - lightning!.satsToLightningString(min), - lightning!.satsToLightningString(max), + lightning!.satsToLightningString(limits.min), + lightning!.satsToLightningString(limits.max), ); } diff --git a/lib/src/screens/receive/lightning_receive_page.dart b/lib/src/screens/receive/lightning_receive_page.dart index 0af4bbca1..59f1f66f7 100644 --- a/lib/src/screens/receive/lightning_receive_page.dart +++ b/lib/src/screens/receive/lightning_receive_page.dart @@ -73,7 +73,7 @@ class LightningReceiveOnchainPage extends BasePage { if (snapshot.data == null) { return CircularProgressIndicator(); } - String data = (snapshot.data as List)[0]; + ReceiveOnchainResult results = snapshot.data as ReceiveOnchainResult; return Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ @@ -99,7 +99,7 @@ class LightningReceiveOnchainPage extends BasePage { () async { await Navigator.pushNamed(context, Routes.fullscreenQR, arguments: QrViewData( - data: data, + data: results.bitcoinAddress, heroTag: heroTag, )); }, @@ -127,7 +127,7 @@ class LightningReceiveOnchainPage extends BasePage { color: Colors.white, ), ), - child: QrImage(data: data)), + child: QrImage(data: results.bitcoinAddress)), ), ), ), @@ -144,7 +144,7 @@ class LightningReceiveOnchainPage extends BasePage { builder: (context) => Observer( builder: (context) => GestureDetector( onTap: () { - Clipboard.setData(ClipboardData(text: data)); + Clipboard.setData(ClipboardData(text: results.bitcoinAddress)); showBar(context, S.of(context).copied_to_clipboard); }, child: Row( @@ -153,7 +153,7 @@ class LightningReceiveOnchainPage extends BasePage { children: [ Expanded( child: Text( - data, + results.bitcoinAddress, textAlign: TextAlign.center, style: TextStyle( fontSize: 15, @@ -207,15 +207,13 @@ class LightningReceiveOnchainPage extends BasePage { return Expanded( child: Container(child: Center(child: CircularProgressIndicator()))); } - int min = int.parse((snapshot.data as List)[1]); - int max = int.parse((snapshot.data as List)[2]); - int fee = int.parse((snapshot.data as List)[3]); + ReceiveOnchainResult results = snapshot.data as ReceiveOnchainResult; return Expanded( child: Text( S.of(context).lightning_receive_limits( - lightning!.satsToLightningString(min), - lightning!.satsToLightningString(max), - lightning!.satsToLightningString(fee), + lightning!.satsToLightningString(results.minAllowedDeposit), + lightning!.satsToLightningString(results.maxAllowedDeposit), + lightning!.satsToLightningString(results.fee), ), maxLines: 10, style: TextStyle( diff --git a/lib/src/screens/send/lightning_send_confirm_page.dart b/lib/src/screens/send/lightning_send_confirm_page.dart index 5259ee640..242ac57b9 100644 --- a/lib/src/screens/send/lightning_send_confirm_page.dart +++ b/lib/src/screens/send/lightning_send_confirm_page.dart @@ -11,6 +11,7 @@ import 'package:cake_wallet/themes/extensions/keyboard_theme.dart'; import 'package:cake_wallet/themes/theme_base.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/LightningSendViewModel.dart'; import 'package:cake_wallet/view_model/lightning_view_model.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:flutter/material.dart'; @@ -22,13 +23,13 @@ import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart'; import 'package:cake_wallet/generated/i18n.dart'; class LightningSendConfirmPage extends BasePage { - LightningSendConfirmPage({required this.invoice, required this.lightningViewModel}) + LightningSendConfirmPage({required this.invoice, required this.lightningSendViewModel}) : _formKey = GlobalKey() { initialSatAmount = ((invoice.amountMsat ?? 0) ~/ 1000); _amountController = TextEditingController(); _fiatAmountController = TextEditingController(); _amountController.text = initialSatAmount.toString(); - _fiatAmountController.text = lightningViewModel.formattedFiatAmount(initialSatAmount); + _fiatAmountController.text = lightningSendViewModel.formattedFiatAmount(initialSatAmount); } final GlobalKey _formKey; @@ -39,7 +40,7 @@ class LightningSendConfirmPage extends BasePage { late TextEditingController _amountController; late TextEditingController _fiatAmountController; final FocusNode _depositAmountFocus = FocusNode(); - final LightningViewModel lightningViewModel; + final LightningSendViewModel lightningSendViewModel; bool _effectsInstalled = false; @@ -194,7 +195,7 @@ class LightningSendConfirmPage extends BasePage { prefixIcon: Padding( padding: EdgeInsets.only(top: 9), child: Text( - lightningViewModel.fiat.title + ':', + lightningSendViewModel.fiat.title + ':', style: TextStyle( fontSize: 16, fontWeight: FontWeight.w600, @@ -252,7 +253,7 @@ class LightningSendConfirmPage extends BasePage { final sdk = await BreezSDK(); late BZG.SendPaymentRequest req; - lightningViewModel.setLoading(true); + lightningSendViewModel.setLoading(true); if (invoice.amountMsat == null) { req = BZG.SendPaymentRequest( @@ -265,7 +266,7 @@ class LightningSendConfirmPage extends BasePage { await sdk.sendPayment(req: req); - lightningViewModel.setLoading(false); + lightningSendViewModel.setLoading(false); showPopUp( context: context, @@ -280,7 +281,7 @@ class LightningSendConfirmPage extends BasePage { }); }); } catch (e) { - lightningViewModel.setLoading(false); + lightningSendViewModel.setLoading(false); showPopUp( context: context, builder: (BuildContext context) { @@ -294,7 +295,7 @@ class LightningSendConfirmPage extends BasePage { }, color: Theme.of(context).primaryColor, textColor: Colors.white, - isLoading: lightningViewModel.loading, + isLoading: lightningSendViewModel.loading, ), ], ); @@ -317,7 +318,7 @@ class LightningSendConfirmPage extends BasePage { _amountController.addListener(() { final amount = _amountController.text; - _fiatAmountController.text = lightningViewModel.formattedFiatAmount(int.parse(amount)); + _fiatAmountController.text = lightningSendViewModel.formattedFiatAmount(int.parse(amount)); }); _effectsInstalled = true; diff --git a/lib/src/screens/send/lightning_send_page.dart b/lib/src/screens/send/lightning_send_page.dart index 7485f9991..994d90440 100644 --- a/lib/src/screens/send/lightning_send_page.dart +++ b/lib/src/screens/send/lightning_send_page.dart @@ -11,6 +11,7 @@ import 'package:cake_wallet/themes/theme_base.dart'; import 'package:cake_wallet/utils/payment_request.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/LightningSendViewModel.dart'; import 'package:cake_wallet/view_model/lightning_view_model.dart'; import 'package:cake_wallet/view_model/send/output.dart'; import 'package:cw_core/crypto_currency.dart'; @@ -27,12 +28,12 @@ class LightningSendPage extends BasePage { LightningSendPage({ required this.output, required this.authService, - required this.lightningViewModel, + required this.lightningSendViewModel, }) : _formKey = GlobalKey(); final Output output; final AuthService authService; - final LightningViewModel lightningViewModel; + final LightningSendViewModel lightningSendViewModel; final GlobalKey _formKey; final controller = PageController(initialPage: 0); diff --git a/lib/view_model/lightning_invoice_page_view_model.dart b/lib/view_model/lightning_invoice_page_view_model.dart index 399654a9f..577182f12 100644 --- a/lib/view_model/lightning_invoice_page_view_model.dart +++ b/lib/view_model/lightning_invoice_page_view_model.dart @@ -109,9 +109,9 @@ abstract class LightningInvoicePageViewModelBase with Store { } Future _fetchLimits() async { - List limits = await lightningViewModel.invoiceSoftLimitsSats(); - minimum = limits[0].toDouble(); - maximum = limits[1].toDouble(); + final limits = await lightningViewModel.invoiceSoftLimitsSats(); + minimum = limits.min.toDouble(); + maximum = limits.max.toDouble(); } @action diff --git a/lib/view_model/lightning_send_view_model.dart b/lib/view_model/lightning_send_view_model.dart new file mode 100644 index 000000000..d4a35a65e --- /dev/null +++ b/lib/view_model/lightning_send_view_model.dart @@ -0,0 +1,43 @@ +import 'dart:async'; +import 'package:breez_sdk/breez_sdk.dart'; +import 'package:breez_sdk/bridge_generated.dart' as BZG; +import 'package:cake_wallet/entities/calculate_fiat_amount_raw.dart'; +import 'package:cake_wallet/entities/fiat_currency.dart'; +import 'package:cake_wallet/lightning/lightning.dart'; +import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart'; +import 'package:cake_wallet/store/settings_store.dart'; +import 'package:cw_core/crypto_currency.dart'; +import 'package:mobx/mobx.dart'; + +part 'lightning_send_view_model.g.dart'; + +class LightningSendViewModel = LightningSendViewModelBase with _$LightningSendViewModel; + +abstract class LightningSendViewModelBase with Store { + LightningSendViewModelBase({ + required this.settingsStore, + required this.fiatConversionStore, + }) {} + + final SettingsStore settingsStore; + final FiatConversionStore fiatConversionStore; + + @observable + bool loading = false; + + @action + void setLoading(bool value) { + loading = value; + } + + FiatCurrency get fiat => settingsStore.fiatCurrency; + + String formattedFiatAmount(int sats) { + String amount = calculateFiatAmountRaw( + cryptoAmount: lightning!.formatterLightningAmountToDouble(amount: sats), + price: fiatConversionStore.prices[CryptoCurrency.btcln], + ); + return amount; + } + +} diff --git a/lib/view_model/lightning_view_model.dart b/lib/view_model/lightning_view_model.dart index a3100b872..8d47755e0 100644 --- a/lib/view_model/lightning_view_model.dart +++ b/lib/view_model/lightning_view_model.dart @@ -14,33 +14,9 @@ part 'lightning_view_model.g.dart'; class LightningViewModel = LightningViewModelBase with _$LightningViewModel; abstract class LightningViewModelBase with Store { - LightningViewModelBase({ - required this.settingsStore, - required this.fiatConversionStore, - }) {} + LightningViewModelBase() {} - final SettingsStore settingsStore; - final FiatConversionStore fiatConversionStore; - - @observable - bool loading = false; - - @action - void setLoading(bool value) { - loading = value; - } - - FiatCurrency get fiat => settingsStore.fiatCurrency; - - String formattedFiatAmount(int sats) { - String amount = calculateFiatAmountRaw( - cryptoAmount: lightning!.formatterLightningAmountToDouble(amount: sats), - price: fiatConversionStore.prices[CryptoCurrency.btcln], - ); - return amount; - } - - Future> receiveOnchain() async { + Future receiveOnchain() async { final sdk = await BreezSDK(); BZG.ReceiveOnchainRequest req = const BZG.ReceiveOnchainRequest(); @@ -49,12 +25,12 @@ abstract class LightningViewModelBase with Store { print("Maximum amount allowed to deposit in sats: ${swapInfo.maxAllowedDeposit}"); int fee = swapInfo.channelOpeningFees?.minMsat ?? 2000; fee = fee ~/ 1000; - return [ - swapInfo.bitcoinAddress, - swapInfo.minAllowedDeposit.toString(), - swapInfo.maxAllowedDeposit.toString(), - fee.toString(), - ]; + return ReceiveOnchainResult( + bitcoinAddress: swapInfo.bitcoinAddress, + minAllowedDeposit: swapInfo.minAllowedDeposit, + maxAllowedDeposit: swapInfo.maxAllowedDeposit, + fee: fee, + ); } Future createInvoice({required String amountSats, String? description}) async { @@ -67,7 +43,7 @@ abstract class LightningViewModelBase with Store { return res.lnInvoice.bolt11; } - Future> invoiceSoftLimitsSats() async { + Future invoiceSoftLimitsSats() async { final sdk = await BreezSDK(); BZG.ReceivePaymentRequest? req = null; req = BZG.ReceivePaymentRequest( @@ -88,7 +64,11 @@ abstract class LightningViewModelBase with Store { min = 0; } } catch (_) {} - return [min, max, balance]; + return InvoiceSoftLimitsResult( + min: min, + max: max, + balance: balance, + ); } Future getBalanceSats() async { @@ -101,3 +81,30 @@ abstract class LightningViewModelBase with Store { } } } + + +class ReceiveOnchainResult { + final String bitcoinAddress; + final int minAllowedDeposit; + final int maxAllowedDeposit; + final int fee; + + ReceiveOnchainResult({ + required this.bitcoinAddress, + required this.minAllowedDeposit, + required this.maxAllowedDeposit, + required this.fee, + }); +} + +class InvoiceSoftLimitsResult { + final int min; + final int max; + final int balance; + + InvoiceSoftLimitsResult({ + required this.min, + required this.max, + required this.balance, + }); +} \ No newline at end of file