add the historical rate to the transaction screen

This commit is contained in:
Serhii 2023-05-24 14:18:29 +03:00
parent c4ebcadb57
commit 56d872a243
4 changed files with 38 additions and 14 deletions

View file

@ -754,6 +754,7 @@ Future setup({
return TransactionDetailsViewModel(
transactionInfo: transactionInfo,
transactionDescriptionBox: _transactionDescriptionBox,
dashboardViewModel: getIt.get<DashboardViewModel>(),
wallet: wallet,
settingsStore: getIt.get<SettingsStore>());
});

View file

@ -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));
}

View file

@ -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<OrderListItem> get orders => ordersStore.orders
.where((item) => item.order.walletId == wallet.id)
.toList();
@computed
List<AnonpayTransactionListItem> get anonpayTransactons => anonpayTransactionsStore.transactions
.where((item) => item.transaction.walletId == wallet.id)
@ -441,9 +448,8 @@ abstract class DashboardViewModelBase with Store {
}
Future<void> _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));
}

View file

@ -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<TransactionDetailsListItem>(),
@ -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<TransactionDescription> transactionDescriptionBox;
final DashboardViewModel dashboardViewModel;
final SettingsStore settingsStore;
final WalletBase wallet;