From 56d872a243ca6776f40bf6a01cbb5aa1c0c3f9cd Mon Sep 17 00:00:00 2001 From: Serhii Date: Wed, 24 May 2023 14:18:29 +0300 Subject: [PATCH] add the historical rate to the transaction screen --- lib/di.dart | 1 + .../dashboard/widgets/transactions_page.dart | 19 ++++++++++++---- .../dashboard/dashboard_view_model.dart | 22 ++++++++++++++----- .../transaction_details_view_model.dart | 10 +++++---- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/lib/di.dart b/lib/di.dart index 30009261f..195dfd92e 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -754,6 +754,7 @@ Future setup({ return TransactionDetailsViewModel( transactionInfo: transactionInfo, transactionDescriptionBox: _transactionDescriptionBox, + dashboardViewModel: getIt.get(), wallet: wallet, settingsStore: getIt.get()); }); diff --git a/lib/src/screens/dashboard/widgets/transactions_page.dart b/lib/src/screens/dashboard/widgets/transactions_page.dart index 2efb38e89..0d70d0e47 100644 --- a/lib/src/screens/dashboard/widgets/transactions_page.dart +++ b/lib/src/screens/dashboard/widgets/transactions_page.dart @@ -1,3 +1,4 @@ +import 'package:cake_wallet/entities/fiat_api_mode.dart'; import 'package:cake_wallet/src/screens/dashboard/widgets/anonpay_transaction_row.dart'; import 'package:cake_wallet/src/screens/dashboard/widgets/order_row.dart'; import 'package:cake_wallet/utils/responsive_layout_util.dart'; @@ -48,6 +49,19 @@ class TransactionsPage extends StatelessWidget { if (item is TransactionListItem) { final transaction = item.transaction; + final description = + dashboardViewModel.getTransactionDescription(transaction); + final formattedFiatAmount = + dashboardViewModel.settingsStore.fiatApiMode == FiatApiMode.disabled + ? '' + : (dashboardViewModel.settingsStore.showHistoricalFiatAmount + ? (description.historicalFiatRate == null || + description.historicalFiatRate == 0.0 + ? '' + : dashboardViewModel.settingsStore.fiatCurrency.toString() + + ' ' + + description.historicalFiatRate!.toStringAsFixed(2)) + : item.formattedFiatAmount); return Observer( builder: (_) => TransactionRow( @@ -56,10 +70,7 @@ class TransactionsPage extends StatelessWidget { direction: transaction.direction, formattedDate: DateFormat('HH:mm').format(transaction.date), formattedAmount: item.formattedCryptoAmount, - formattedFiatAmount: - dashboardViewModel.balanceViewModel.isFiatDisabled - ? '' - : item.formattedFiatAmount, + formattedFiatAmount: formattedFiatAmount, isPending: transaction.isPending, title: item.formattedTitle + item.formattedStatus)); } diff --git a/lib/view_model/dashboard/dashboard_view_model.dart b/lib/view_model/dashboard/dashboard_view_model.dart index 51bcc2be1..29c711805 100644 --- a/lib/view_model/dashboard/dashboard_view_model.dart +++ b/lib/view_model/dashboard/dashboard_view_model.dart @@ -130,6 +130,13 @@ abstract class DashboardViewModelBase with Store { }); }); + reaction((_) => settingsStore.fiatApiMode, + (FiatApiMode fiatApiMode) { + _wallet.transactionHistory.transactions.values.forEach((tx) { + _getHistoricalFiatRate(tx); + }); + }); + if (_wallet.type == WalletType.monero) { subname = monero!.getCurrentAccount(_wallet).label; @@ -165,7 +172,7 @@ abstract class DashboardViewModelBase with Store { } reaction((_) => appStore.wallet, _onWalletChange); - + connectMapToListWithTransform( appStore.wallet!.transactionHistory.transactions, transactions, @@ -237,7 +244,7 @@ abstract class DashboardViewModelBase with Store { @computed BalanceDisplayMode get balanceDisplayMode => appStore.settingsStore.balanceDisplayMode; - + @computed bool get shouldShowMarketPlaceInDashboard { return appStore.settingsStore.shouldShowMarketPlaceInDashboard; @@ -252,7 +259,7 @@ abstract class DashboardViewModelBase with Store { List get orders => ordersStore.orders .where((item) => item.order.walletId == wallet.id) .toList(); - + @computed List get anonpayTransactons => anonpayTransactionsStore.transactions .where((item) => item.transaction.walletId == wallet.id) @@ -441,9 +448,8 @@ abstract class DashboardViewModelBase with Store { } Future _getHistoricalFiatRate(TransactionInfo transactionInfo) async { - final description = transactionDescriptionBox.values.firstWhere( - (val) => val.id == transactionInfo.id, - orElse: () => TransactionDescription(id: transactionInfo.id)); + if (FiatApiMode.disabled == settingsStore.fiatApiMode) return; + final description = getTransactionDescription(transactionInfo); if (description.historicalFiat != settingsStore.fiatCurrency.toString() || description.historicalFiatRate == null) { @@ -484,4 +490,8 @@ abstract class DashboardViewModelBase with Store { } } } + + TransactionDescription getTransactionDescription(TransactionInfo transactionInfo) => + transactionDescriptionBox.values.firstWhere((val) => val.id == transactionInfo.id, + orElse: () => TransactionDescription(id: transactionInfo.id)); } diff --git a/lib/view_model/transaction_details_view_model.dart b/lib/view_model/transaction_details_view_model.dart index 81447c952..d1f2edd35 100644 --- a/lib/view_model/transaction_details_view_model.dart +++ b/lib/view_model/transaction_details_view_model.dart @@ -19,6 +19,8 @@ import 'package:url_launcher/url_launcher.dart'; import 'package:cake_wallet/monero/monero.dart'; import 'package:cake_wallet/core/fiat_conversion_service.dart'; +import 'dashboard/dashboard_view_model.dart'; + part 'transaction_details_view_model.g.dart'; class TransactionDetailsViewModel = TransactionDetailsViewModelBase @@ -28,6 +30,7 @@ abstract class TransactionDetailsViewModelBase with Store { TransactionDetailsViewModelBase( {required this.transactionInfo, required this.transactionDescriptionBox, + required this.dashboardViewModel, required this.wallet, required this.settingsStore}) : items = ObservableList(), @@ -130,9 +133,7 @@ abstract class TransactionDetailsViewModelBase with Store { } catch (e) {} })); - final description = transactionDescriptionBox.values.firstWhere( - (val) => val.id == transactionInfo.id, - orElse: () => TransactionDescription(id: transactionInfo.id)); + final description = dashboardViewModel.getTransactionDescription(transactionInfo); items.add(TextFieldListItem( title: S.current.note_tap_to_change, @@ -149,7 +150,7 @@ abstract class TransactionDetailsViewModelBase with Store { if (settingsStore.showHistoricalFiatAmount && description.historicalFiatRate != null && - description.historicalFiatRate! > 0) { + description.historicalFiatRate! > 0 && settingsStore.fiatApiMode != FiatApiMode.disabled) { final index = items.indexWhere((element) => element.title == S.current.transaction_details_fee); @@ -165,6 +166,7 @@ abstract class TransactionDetailsViewModelBase with Store { final TransactionInfo transactionInfo; final Box transactionDescriptionBox; + final DashboardViewModel dashboardViewModel; final SettingsStore settingsStore; final WalletBase wallet;