mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-02-02 19:26:27 +00:00
refactor the rate fetching flow
This commit is contained in:
parent
1b6d91e059
commit
2f3f677300
13 changed files with 257 additions and 196 deletions
|
@ -65,8 +65,6 @@ Future<double?> _fetchHistoricalPrice(Map<String, dynamic> args) async {
|
|||
'interval_minutes': intervalFromNow.toString()
|
||||
};
|
||||
|
||||
double price = 0.0;
|
||||
|
||||
try {
|
||||
late final Uri uri;
|
||||
if (torOnly) {
|
||||
|
@ -86,9 +84,13 @@ Future<double?> _fetchHistoricalPrice(Map<String, dynamic> args) async {
|
|||
|
||||
final results = data['results'] as Map<String, dynamic>;
|
||||
|
||||
if (results.isNotEmpty) price = results.values.first as double;
|
||||
if (results.isNotEmpty) {
|
||||
return (results.values.first as double) / 100000;
|
||||
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
return price;
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
return null;
|
||||
|
|
|
@ -1198,6 +1198,8 @@ Future<void> setup({
|
|||
),
|
||||
);
|
||||
|
||||
getIt.registerSingleton<Box<TransactionDescription>>(transactionDescriptionBox);
|
||||
|
||||
getIt.registerFactoryParam<ManageNodesPage, bool, void>((bool isPow, _) {
|
||||
if (isPow) {
|
||||
return ManageNodesPage(isPow, powNodeListViewModel: getIt.get<PowNodeListViewModel>());
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:cw_core/hive_type_ids.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
|
@ -9,8 +11,7 @@ class TransactionDescription extends HiveObject {
|
|||
{required this.id,
|
||||
this.recipientAddress,
|
||||
this.transactionNote,
|
||||
this.historicalFiatRaw,
|
||||
this.historicalFiatRate});
|
||||
this.historicalRatesJson});
|
||||
|
||||
static const typeId = TRANSACTION_TYPE_ID;
|
||||
static const boxName = 'TransactionDescriptions';
|
||||
|
@ -26,12 +27,15 @@ class TransactionDescription extends HiveObject {
|
|||
String? transactionNote;
|
||||
|
||||
@HiveField(3)
|
||||
String? historicalFiatRaw;
|
||||
String? historicalRatesJson;
|
||||
|
||||
@HiveField(4)
|
||||
double? historicalFiatRate;
|
||||
|
||||
String get note => transactionNote ?? '';
|
||||
|
||||
String get historicalFiat => historicalFiatRaw ?? '';
|
||||
Map<String, String> get historicalRates =>
|
||||
historicalRatesJson != null ? Map<String, String>.from(jsonDecode(historicalRatesJson!) as Map<dynamic, dynamic>) : {};
|
||||
|
||||
set historicalRates(Map<String, String> value) {
|
||||
historicalRatesJson = jsonEncode(value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import 'package:cake_wallet/reactions/on_current_fiat_api_mode_change.dart';
|
|||
import 'package:cake_wallet/reactions/on_current_node_change.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:cake_wallet/di.dart';
|
||||
import 'package:cake_wallet/entities/preferences_key.dart';
|
||||
|
@ -14,12 +15,14 @@ import 'package:cake_wallet/store/app_store.dart';
|
|||
import 'package:cake_wallet/store/settings_store.dart';
|
||||
import 'package:cake_wallet/store/authentication_store.dart';
|
||||
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
|
||||
import 'package:cake_wallet/entities/transaction_description.dart';
|
||||
|
||||
Future<void> bootstrap(GlobalKey<NavigatorState> navigatorKey) async {
|
||||
final appStore = getIt.get<AppStore>();
|
||||
final authenticationStore = getIt.get<AuthenticationStore>();
|
||||
final settingsStore = getIt.get<SettingsStore>();
|
||||
final fiatConversionStore = getIt.get<FiatConversionStore>();
|
||||
final transactionDescription = getIt.get<Box<TransactionDescription>>();
|
||||
|
||||
final currentWalletName = getIt
|
||||
.get<SharedPreferences>()
|
||||
|
@ -30,9 +33,9 @@ Future<void> bootstrap(GlobalKey<NavigatorState> navigatorKey) async {
|
|||
|
||||
startAuthenticationStateChange(authenticationStore, navigatorKey);
|
||||
startCurrentWalletChangeReaction(
|
||||
appStore, settingsStore, fiatConversionStore);
|
||||
startCurrentFiatChangeReaction(appStore, settingsStore, fiatConversionStore);
|
||||
startCurrentFiatApiModeChangeReaction(appStore, settingsStore, fiatConversionStore);
|
||||
appStore, settingsStore, fiatConversionStore, transactionDescription);
|
||||
startCurrentFiatChangeReaction(appStore, settingsStore, fiatConversionStore, transactionDescription);
|
||||
startCurrentFiatApiModeChangeReaction(appStore, settingsStore, fiatConversionStore, transactionDescription);
|
||||
startOnCurrentNodeChangeReaction(appStore);
|
||||
startFiatRateUpdate(appStore, settingsStore, fiatConversionStore);
|
||||
}
|
||||
|
|
67
lib/reactions/fiat_historical_rate_update.dart
Normal file
67
lib/reactions/fiat_historical_rate_update.dart
Normal file
|
@ -0,0 +1,67 @@
|
|||
import 'dart:async';
|
||||
import 'package:cake_wallet/core/fiat_conversion_service.dart';
|
||||
import 'package:cake_wallet/entities/fiat_api_mode.dart';
|
||||
import 'package:cake_wallet/entities/transaction_description.dart';
|
||||
import 'package:cake_wallet/store/app_store.dart';
|
||||
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
|
||||
import 'package:cake_wallet/store/settings_store.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
Future<void> historicalRateUpdate(
|
||||
AppStore appStore,
|
||||
SettingsStore settingsStore,
|
||||
FiatConversionStore fiatConversionStore,
|
||||
Box<TransactionDescription> transactionDescription) async {
|
||||
final transactions = appStore.wallet!.transactionHistory.transactions.values.toList();
|
||||
|
||||
const int batchSize = 10;
|
||||
const Duration delayBetweenBatches = Duration(milliseconds: 2);
|
||||
|
||||
int nextBatchStart = 0;
|
||||
|
||||
for (int i = 0; i < transactions.length; i += batchSize) {
|
||||
final batch = transactions.skip(i).take(batchSize);
|
||||
|
||||
bool needsProcessing = batch.any((tx) {
|
||||
var description = transactionDescription.get(tx.id);
|
||||
final fiatName = settingsStore.fiatCurrency.toString();
|
||||
return description == null ||
|
||||
description.historicalRates.isEmpty ||
|
||||
!description.historicalRates.containsKey(fiatName);
|
||||
});
|
||||
|
||||
if (needsProcessing) {
|
||||
await Future.wait(batch.map((tx) async {
|
||||
var description = transactionDescription.get(tx.id);
|
||||
final fiatName = settingsStore.fiatCurrency.toString();
|
||||
|
||||
if (description == null ||
|
||||
description.historicalRates.isEmpty ||
|
||||
!description.historicalRates.containsKey(fiatName)) {
|
||||
try {
|
||||
final result = await FiatConversionService.fetchHistoricalPrice(
|
||||
crypto: appStore.wallet!.currency,
|
||||
fiat: settingsStore.fiatCurrency,
|
||||
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly,
|
||||
date: tx.date);
|
||||
|
||||
if (result == null) return;
|
||||
|
||||
description ??= TransactionDescription(id: tx.id);
|
||||
Map<String, String> rates = description.historicalRates;
|
||||
rates[fiatName] = result.toStringAsFixed(4);
|
||||
description.historicalRates = rates;
|
||||
await transactionDescription.put(tx.id, description);
|
||||
} catch (e) {
|
||||
print("Error fetching historical price: $e");
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
nextBatchStart = i + batchSize;
|
||||
if (nextBatchStart < transactions.length) {
|
||||
await Future.delayed(delayBetweenBatches);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,25 +1,34 @@
|
|||
import 'package:cake_wallet/entities/fiat_api_mode.dart';
|
||||
import 'package:cake_wallet/entities/transaction_description.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/core/fiat_conversion_service.dart';
|
||||
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
|
||||
import 'package:cake_wallet/store/settings_store.dart';
|
||||
import 'package:cake_wallet/store/app_store.dart';
|
||||
|
||||
import 'fiat_historical_rate_update.dart';
|
||||
|
||||
ReactionDisposer? _onCurrentFiatCurrencyChangeDisposer;
|
||||
|
||||
void startCurrentFiatApiModeChangeReaction(AppStore appStore,
|
||||
SettingsStore settingsStore, FiatConversionStore fiatConversionStore) {
|
||||
void startCurrentFiatApiModeChangeReaction(AppStore appStore, SettingsStore settingsStore,
|
||||
FiatConversionStore fiatConversionStore, Box<TransactionDescription> transactionDescription) {
|
||||
_onCurrentFiatCurrencyChangeDisposer?.reaction.dispose();
|
||||
_onCurrentFiatCurrencyChangeDisposer = reaction(
|
||||
(_) => settingsStore.fiatApiMode, (FiatApiMode fiatApiMode) async {
|
||||
_onCurrentFiatCurrencyChangeDisposer =
|
||||
reaction((_) => settingsStore.fiatApiMode, (FiatApiMode fiatApiMode) async {
|
||||
if (appStore.wallet == null || fiatApiMode == FiatApiMode.disabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
fiatConversionStore.prices[appStore.wallet!.currency] =
|
||||
await FiatConversionService.fetchPrice(
|
||||
crypto: appStore.wallet!.currency,
|
||||
fiat: settingsStore.fiatCurrency,
|
||||
torOnly: fiatApiMode == FiatApiMode.torOnly);
|
||||
fiatConversionStore.prices[appStore.wallet!.currency] = await FiatConversionService.fetchPrice(
|
||||
crypto: appStore.wallet!.currency,
|
||||
fiat: settingsStore.fiatCurrency,
|
||||
torOnly: fiatApiMode == FiatApiMode.torOnly);
|
||||
|
||||
if (settingsStore.showHistoricalFiatAmount) {
|
||||
await historicalRateUpdate(
|
||||
appStore, settingsStore, fiatConversionStore, transactionDescription);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
import 'package:cake_wallet/entities/fiat_api_mode.dart';
|
||||
import 'package:cake_wallet/entities/transaction_description.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/core/fiat_conversion_service.dart';
|
||||
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
|
||||
|
@ -6,22 +8,31 @@ import 'package:cake_wallet/store/settings_store.dart';
|
|||
import 'package:cake_wallet/store/app_store.dart';
|
||||
import 'package:cake_wallet/entities/fiat_currency.dart';
|
||||
|
||||
import 'fiat_historical_rate_update.dart';
|
||||
|
||||
ReactionDisposer? _onCurrentFiatCurrencyChangeDisposer;
|
||||
|
||||
void startCurrentFiatChangeReaction(AppStore appStore,
|
||||
SettingsStore settingsStore, FiatConversionStore fiatConversionStore) {
|
||||
void startCurrentFiatChangeReaction(AppStore appStore, SettingsStore settingsStore,
|
||||
FiatConversionStore fiatConversionStore, Box<TransactionDescription> transactionDescription) {
|
||||
_onCurrentFiatCurrencyChangeDisposer?.reaction.dispose();
|
||||
_onCurrentFiatCurrencyChangeDisposer = reaction(
|
||||
(_) => settingsStore.fiatCurrency, (FiatCurrency fiatCurrency) async {
|
||||
_onCurrentFiatCurrencyChangeDisposer =
|
||||
reaction((_) => settingsStore.fiatCurrency, (FiatCurrency fiatCurrency) async {
|
||||
if (appStore.wallet == null || settingsStore.fiatApiMode == FiatApiMode.disabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
final cryptoCurrency = appStore.wallet!.currency;
|
||||
fiatConversionStore.prices[cryptoCurrency] =
|
||||
await FiatConversionService.fetchPrice(
|
||||
crypto: cryptoCurrency,
|
||||
fiat: fiatCurrency,
|
||||
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
|
||||
});
|
||||
fiatConversionStore.prices[cryptoCurrency] = await FiatConversionService.fetchPrice(
|
||||
crypto: cryptoCurrency,
|
||||
fiat: fiatCurrency,
|
||||
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
|
||||
|
||||
if (settingsStore.showHistoricalFiatAmount) {
|
||||
await historicalRateUpdate(
|
||||
appStore, settingsStore, fiatConversionStore, transactionDescription);
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'package:cake_wallet/entities/auto_generate_subaddress_status.dart';
|
||||
import 'package:cake_wallet/entities/fiat_api_mode.dart';
|
||||
import 'package:cake_wallet/entities/transaction_description.dart';
|
||||
import 'package:cake_wallet/entities/update_haven_rate.dart';
|
||||
import 'package:cake_wallet/ethereum/ethereum.dart';
|
||||
import 'package:cake_wallet/polygon/polygon.dart';
|
||||
|
@ -9,6 +10,7 @@ import 'package:cw_core/erc20_token.dart';
|
|||
import 'package:cw_core/transaction_history.dart';
|
||||
import 'package:cw_core/balance.dart';
|
||||
import 'package:cw_core/transaction_info.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:cake_wallet/di.dart';
|
||||
|
@ -22,12 +24,15 @@ import 'package:cake_wallet/core/fiat_conversion_service.dart';
|
|||
import 'package:cw_core/wallet_base.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
|
||||
import 'fiat_historical_rate_update.dart';
|
||||
|
||||
ReactionDisposer? _onCurrentWalletChangeReaction;
|
||||
ReactionDisposer? _onCurrentWalletChangeFiatRateUpdateReaction;
|
||||
//ReactionDisposer _onCurrentWalletAddressChangeReaction;
|
||||
|
||||
void startCurrentWalletChangeReaction(
|
||||
AppStore appStore, SettingsStore settingsStore, FiatConversionStore fiatConversionStore) {
|
||||
AppStore appStore, SettingsStore settingsStore, FiatConversionStore fiatConversionStore,
|
||||
Box<TransactionDescription> transactionDescription) {
|
||||
_onCurrentWalletChangeReaction?.reaction.dispose();
|
||||
_onCurrentWalletChangeFiatRateUpdateReaction?.reaction.dispose();
|
||||
//_onCurrentWalletAddressChangeReaction?.reaction?dispose();
|
||||
|
@ -105,6 +110,11 @@ void startCurrentWalletChangeReaction(
|
|||
return;
|
||||
}
|
||||
|
||||
if (settingsStore.showHistoricalFiatAmount) {
|
||||
await historicalRateUpdate(
|
||||
appStore, settingsStore, fiatConversionStore, transactionDescription);
|
||||
}
|
||||
|
||||
fiatConversionStore.prices[wallet.currency] = 0;
|
||||
fiatConversionStore.prices[wallet.currency] = await FiatConversionService.fetchPrice(
|
||||
crypto: wallet.currency,
|
||||
|
@ -133,7 +143,9 @@ void startCurrentWalletChangeReaction(
|
|||
fiat: settingsStore.fiatCurrency,
|
||||
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
|
||||
}.call();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
|
|
|
@ -81,34 +81,26 @@ class TransactionsPage extends StatelessWidget {
|
|||
if (item is DateSectionItem) {
|
||||
return DateSectionRaw(date: item.date);
|
||||
}
|
||||
|
||||
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(
|
||||
onTap: () => Navigator.of(context)
|
||||
.pushNamed(Routes.transactionDetails, arguments: transaction),
|
||||
direction: transaction.direction,
|
||||
formattedDate: DateFormat('HH:mm').format(transaction.date),
|
||||
formattedAmount: item.formattedCryptoAmount,
|
||||
formattedFiatAmount: formattedFiatAmount,
|
||||
isPending: transaction.isPending,
|
||||
title: item.formattedTitle + item.formattedStatus));
|
||||
}
|
||||
if (item is TransactionListItem) {
|
||||
final transaction = item.transaction;
|
||||
|
||||
final historicalFiatValue = dashboardViewModel.getFormattedFiatAmount(transaction);
|
||||
final formattedFiatValue = historicalFiatValue ??
|
||||
item.formattedFiatAmount;
|
||||
|
||||
return Observer(
|
||||
builder: (_) => TransactionRow(
|
||||
onTap: () => Navigator.of(context)
|
||||
.pushNamed(Routes.transactionDetails, arguments: transaction),
|
||||
direction: transaction.direction,
|
||||
formattedDate: DateFormat('HH:mm').format(transaction.date),
|
||||
formattedAmount: item.formattedCryptoAmount,
|
||||
formattedFiatAmount: formattedFiatValue,
|
||||
isPending: transaction.isPending,
|
||||
isHistoricalRate: historicalFiatValue != null && historicalFiatValue.isNotEmpty,
|
||||
title: item.formattedTitle + item.formattedStatus));
|
||||
}
|
||||
|
||||
if (item is AnonpayTransactionListItem) {
|
||||
final transactionInfo = item.transaction;
|
||||
|
|
|
@ -12,7 +12,8 @@ class TransactionRow extends StatelessWidget {
|
|||
required this.formattedFiatAmount,
|
||||
required this.isPending,
|
||||
required this.title,
|
||||
required this.onTap});
|
||||
required this.onTap,
|
||||
this.isHistoricalRate = false});
|
||||
|
||||
final VoidCallback onTap;
|
||||
final TransactionDirection direction;
|
||||
|
@ -20,6 +21,7 @@ class TransactionRow extends StatelessWidget {
|
|||
final String formattedAmount;
|
||||
final String formattedFiatAmount;
|
||||
final bool isPending;
|
||||
final bool isHistoricalRate;
|
||||
final String title;
|
||||
|
||||
@override
|
||||
|
@ -38,48 +40,53 @@ class TransactionRow extends StatelessWidget {
|
|||
width: 36,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: Theme.of(context).extension<TransactionTradeTheme>()!.rowsColor
|
||||
),
|
||||
child: Image.asset(
|
||||
direction == TransactionDirection.incoming
|
||||
? 'assets/images/down_arrow.png'
|
||||
: 'assets/images/up_arrow.png'),
|
||||
color: Theme.of(context).extension<TransactionTradeTheme>()!.rowsColor),
|
||||
child: Image.asset(direction == TransactionDirection.incoming
|
||||
? 'assets/images/down_arrow.png'
|
||||
: 'assets/images/up_arrow.png'),
|
||||
),
|
||||
SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: <Widget>[
|
||||
Text(title,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Theme.of(context).extension<DashboardPageTheme>()!.textColor)),
|
||||
Text(formattedAmount,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Theme.of(context).extension<DashboardPageTheme>()!.textColor))
|
||||
]),
|
||||
SizedBox(height: 5),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: <Widget>[
|
||||
Text(formattedDate,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Theme.of(context).extension<CakeTextTheme>()!.dateSectionRowColor)),
|
||||
Text(formattedFiatAmount,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Theme.of(context).extension<CakeTextTheme>()!.dateSectionRowColor))
|
||||
])
|
||||
],
|
||||
)
|
||||
)
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: <Widget>[
|
||||
Text(title,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Theme.of(context).extension<DashboardPageTheme>()!.textColor)),
|
||||
Text(formattedAmount,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Theme.of(context).extension<DashboardPageTheme>()!.textColor))
|
||||
]),
|
||||
SizedBox(height: 5),
|
||||
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: <Widget>[
|
||||
Text(formattedDate,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color:
|
||||
Theme.of(context).extension<CakeTextTheme>()!.dateSectionRowColor)),
|
||||
Row(
|
||||
children: [
|
||||
Text(formattedFiatAmount,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Theme.of(context)
|
||||
.extension<CakeTextTheme>()!
|
||||
.dateSectionRowColor)),
|
||||
if (isHistoricalRate)
|
||||
Icon(Icons.history,
|
||||
size: 14,
|
||||
color:
|
||||
Theme.of(context).extension<CakeTextTheme>()!.dateSectionRowColor)
|
||||
],
|
||||
),
|
||||
])
|
||||
],
|
||||
))
|
||||
],
|
||||
),
|
||||
));
|
||||
|
|
|
@ -47,6 +47,24 @@ class DisplaySettingsPage extends BasePage {
|
|||
_displaySettingsViewModel.setShowHistoricalFiatAmount(value);
|
||||
},
|
||||
),
|
||||
//if (!isHaven) it does not work correctly
|
||||
if (!_displaySettingsViewModel.disabledFiatApiMode)
|
||||
SettingsPickerCell<FiatCurrency>(
|
||||
title: S.current.settings_currency,
|
||||
searchHintText: S.current.search_currency,
|
||||
items: FiatCurrency.all,
|
||||
selectedItem: _displaySettingsViewModel.fiatCurrency,
|
||||
onItemSelected: (FiatCurrency currency) =>
|
||||
_displaySettingsViewModel.setFiatCurrency(currency),
|
||||
images: FiatCurrency.all
|
||||
.map((e) => Image.asset("assets/images/flags/${e.countryCode}.png"))
|
||||
.toList(),
|
||||
isGridView: true,
|
||||
matchingCriteria: (FiatCurrency currency, String searchText) {
|
||||
return currency.title.toLowerCase().contains(searchText) ||
|
||||
currency.fullName.toLowerCase().contains(searchText);
|
||||
},
|
||||
),
|
||||
SettingsPickerCell<String>(
|
||||
title: S.current.settings_change_language,
|
||||
searchHintText: S.current.search_language,
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
import 'package:cake_wallet/core/fiat_conversion_service.dart';
|
||||
import 'package:cake_wallet/entities/exchange_api_mode.dart';
|
||||
import 'package:cake_wallet/entities/fiat_api_mode.dart';
|
||||
|
@ -79,7 +78,7 @@ abstract class DashboardViewModelBase with Store {
|
|||
required this.settingsStore,
|
||||
required this.yatStore,
|
||||
required this.ordersStore,
|
||||
required this.transactionDescriptionBox,
|
||||
required this.transactionDescriptionBox,
|
||||
required this.anonpayTransactionsStore,
|
||||
required this.sharedPreferences,
|
||||
required this.keyService})
|
||||
|
@ -160,31 +159,6 @@ abstract class DashboardViewModelBase with Store {
|
|||
|
||||
final _wallet = wallet;
|
||||
|
||||
|
||||
reaction((_) => settingsStore.fiatCurrency,
|
||||
(FiatCurrency fiatCurrency) {
|
||||
_wallet.transactionHistory.transactions.values.forEach((tx) {
|
||||
_getHistoricalFiatRate(tx);
|
||||
});
|
||||
});
|
||||
|
||||
reaction((_) => settingsStore.fiatApiMode,
|
||||
(FiatApiMode fiatApiMode) {
|
||||
_wallet.transactionHistory.transactions.values.forEach((tx) {
|
||||
_getHistoricalFiatRate(tx);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
reaction((_) => settingsStore.showHistoricalFiatAmount,
|
||||
(bool showHistoricalFiatAmount) {
|
||||
if (showHistoricalFiatAmount) {
|
||||
_wallet.transactionHistory.transactions.values.forEach((tx) {
|
||||
_getHistoricalFiatRate(tx);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (_wallet.type == WalletType.monero) {
|
||||
subname = monero!.getCurrentAccount(_wallet).label;
|
||||
|
||||
|
@ -204,15 +178,12 @@ abstract class DashboardViewModelBase with Store {
|
|||
final sortedTransactions = [..._accountTransactions];
|
||||
sortedTransactions.sort((a, b) => a.date.compareTo(b.date));
|
||||
|
||||
|
||||
transactions = ObservableList.of(sortedTransactions.map((transaction) {
|
||||
_getHistoricalFiatRate(transaction);
|
||||
return TransactionListItem(
|
||||
transaction: transaction,
|
||||
balanceViewModel: balanceViewModel,
|
||||
settingsStore: appStore.settingsStore);
|
||||
}));
|
||||
|
||||
} else {
|
||||
final sortedTransactions = [...wallet.transactionHistory.transactions.values];
|
||||
sortedTransactions.sort((a, b) => a.date.compareTo(b.date));
|
||||
|
@ -222,7 +193,7 @@ abstract class DashboardViewModelBase with Store {
|
|||
transaction: transaction,
|
||||
balanceViewModel: balanceViewModel,
|
||||
settingsStore: appStore.settingsStore);
|
||||
} ));
|
||||
}));
|
||||
}
|
||||
|
||||
// TODO: nano sub-account generation is disabled:
|
||||
|
@ -243,15 +214,12 @@ abstract class DashboardViewModelBase with Store {
|
|||
return false;
|
||||
}
|
||||
|
||||
_getHistoricalFiatRate(transaction);
|
||||
|
||||
final wallet = _wallet;
|
||||
if (wallet.type == WalletType.monero) {
|
||||
return monero!.getTransactionInfoAccountId(transaction) ==
|
||||
monero!.getCurrentAccount(wallet).id;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
@ -515,7 +483,6 @@ abstract class DashboardViewModelBase with Store {
|
|||
|
||||
@action
|
||||
void _onMoneroTransactionsUpdate(WalletBase wallet) {
|
||||
|
||||
transactions.clear();
|
||||
|
||||
final _accountTransactions = monero!
|
||||
|
@ -538,52 +505,6 @@ abstract class DashboardViewModelBase with Store {
|
|||
hasSellAction = !isHaven;
|
||||
}
|
||||
|
||||
Future<void> _getHistoricalFiatRate(TransactionInfo transactionInfo) async {
|
||||
if (FiatApiMode.disabled == settingsStore.fiatApiMode
|
||||
|| !settingsStore.showHistoricalFiatAmount) return;
|
||||
final description = getTransactionDescription(transactionInfo);
|
||||
|
||||
|
||||
if (description.historicalFiat != settingsStore.fiatCurrency.toString()
|
||||
|| description.historicalFiatRate == null) {
|
||||
if (description.key == 0) description.delete();
|
||||
description.historicalFiatRate = null;
|
||||
transactionDescriptionBox.put(description.id, description);
|
||||
final fiat = settingsStore.fiatCurrency;
|
||||
|
||||
final historicalFiatRate = await FiatConversionService.fetchHistoricalPrice(
|
||||
crypto: wallet.currency,
|
||||
fiat: fiat,
|
||||
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly,
|
||||
date: transactionInfo.date);
|
||||
var formattedFiatAmount = 0.0;
|
||||
switch (wallet.type) {
|
||||
case WalletType.bitcoin:
|
||||
case WalletType.litecoin:
|
||||
formattedFiatAmount = bitcoinAmountToDouble(amount: transactionInfo.amount);
|
||||
break;
|
||||
case WalletType.monero:
|
||||
case WalletType.haven:
|
||||
formattedFiatAmount = moneroAmountToDouble(amount: transactionInfo.amount);
|
||||
break;
|
||||
default:
|
||||
formattedFiatAmount;
|
||||
}
|
||||
description.historicalFiatRaw = settingsStore.fiatCurrency.toString();
|
||||
|
||||
if (historicalFiatRate != null) {
|
||||
final historicalFiatAmountFormatted = formattedFiatAmount * historicalFiatRate;
|
||||
if (description.key == 0) description.delete();
|
||||
description.historicalFiatRate = historicalFiatAmountFormatted;
|
||||
transactionDescriptionBox.put(description.id, description);
|
||||
} else {
|
||||
if (description.key == 0) description.delete();
|
||||
description.historicalFiatRate = null;
|
||||
transactionDescriptionBox.put(description.id, description);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TransactionDescription getTransactionDescription(TransactionInfo transactionInfo) =>
|
||||
transactionDescriptionBox.values.firstWhere((val) => val.id == transactionInfo.id,
|
||||
orElse: () => TransactionDescription(id: transactionInfo.id));
|
||||
|
@ -629,6 +550,25 @@ abstract class DashboardViewModelBase with Store {
|
|||
return affectedWallets;
|
||||
}
|
||||
|
||||
String? getFormattedFiatAmount(TransactionInfo transaction) {
|
||||
final description = getTransactionDescription(transaction);
|
||||
if (settingsStore.fiatApiMode == FiatApiMode.disabled) return '';
|
||||
|
||||
if (settingsStore.showHistoricalFiatAmount) {
|
||||
final fiatName = settingsStore.fiatCurrency.toString();
|
||||
final fiatRate = double.tryParse(description.historicalRates[fiatName] ?? '');
|
||||
final formattedHistoricalRate = (fiatRate != null && fiatRate < 0.01)
|
||||
? '$fiatName < 0.01'
|
||||
: fiatRate != null
|
||||
? '$fiatName ${fiatRate.toStringAsFixed(2)}'
|
||||
: '';
|
||||
|
||||
return formattedHistoricalRate;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<ServicesResponse> getServicesStatus() async {
|
||||
try {
|
||||
final res = await http.get(Uri.parse("https://service-api.cakewallet.com/v1/active-notices"));
|
||||
|
|
|
@ -102,6 +102,16 @@ abstract class TransactionDetailsViewModelBase with Store {
|
|||
}
|
||||
}
|
||||
|
||||
final description = dashboardViewModel.getTransactionDescription(transactionInfo);
|
||||
|
||||
|
||||
final fiatHistoricalRate = dashboardViewModel.getFormattedFiatAmount(transactionInfo);
|
||||
if (fiatHistoricalRate != null && fiatHistoricalRate.isNotEmpty) {
|
||||
final formattedFiatValue = fiatHistoricalRate.split(' ').reversed.join(' ');
|
||||
items.add(StandartListItem(
|
||||
title: 'S.current.historical_fiat_amount', value: formattedFiatValue));
|
||||
}
|
||||
|
||||
final type = wallet.type;
|
||||
|
||||
items.add(BlockExplorerListItem(
|
||||
|
@ -113,8 +123,6 @@ abstract class TransactionDetailsViewModelBase with Store {
|
|||
} catch (e) {}
|
||||
}));
|
||||
|
||||
final description = dashboardViewModel.getTransactionDescription(transactionInfo);
|
||||
|
||||
items.add(TextFieldListItem(
|
||||
title: S.current.note_tap_to_change,
|
||||
value: description.note,
|
||||
|
@ -128,20 +136,6 @@ abstract class TransactionDetailsViewModelBase with Store {
|
|||
}
|
||||
}));
|
||||
|
||||
if (settingsStore.showHistoricalFiatAmount &&
|
||||
description.historicalFiatRate != null &&
|
||||
description.historicalFiatRate! > 0 && settingsStore.fiatApiMode != FiatApiMode.disabled) {
|
||||
final index =
|
||||
items.indexWhere((element) => element.title == S.current.transaction_details_fee);
|
||||
|
||||
items.insert(
|
||||
index + 1,
|
||||
StandartListItem(
|
||||
title: 'S.current.historical_fiat_amount',
|
||||
value: description.historicalFiatRate!.toStringAsFixed(2) +
|
||||
' ' +
|
||||
settingsStore.fiatCurrency.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
final TransactionInfo transactionInfo;
|
||||
|
|
Loading…
Reference in a new issue