diff --git a/lib/di.dart b/lib/di.dart index 99188a2c8..e8f878327 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -12,7 +12,6 @@ import 'package:cake_wallet/src/screens/wallet_keys/wallet_keys_page.dart'; import 'package:cake_wallet/store/contact_list_store.dart'; import 'package:cake_wallet/store/node_list_store.dart'; import 'package:cake_wallet/store/settings_store.dart'; -import 'package:cake_wallet/src/stores/price/price_store.dart'; import 'package:cake_wallet/core/auth_service.dart'; import 'package:cake_wallet/core/key_service.dart'; import 'package:cake_wallet/monero/monero_wallet.dart'; @@ -57,6 +56,7 @@ import 'package:cake_wallet/store/authentication_store.dart'; import 'package:cake_wallet/store/dashboard/trades_store.dart'; import 'package:cake_wallet/store/dashboard/trade_filter_store.dart'; import 'package:cake_wallet/store/dashboard/transaction_filter_store.dart'; +import 'package:cake_wallet/store/dashboard/fiat_convertation_store.dart'; final getIt = GetIt.instance; @@ -83,8 +83,7 @@ Future setup( {Box walletInfoSource, Box nodeSource, Box contactSource, - Box tradesSource, - PriceStore priceStore}) async { + Box tradesSource}) async { getIt.registerSingletonAsync( () => SharedPreferences.getInstance()); @@ -110,6 +109,7 @@ Future setup( getIt.registerSingleton( TradeFilterStore(wallet: getIt.get().wallet)); getIt.registerSingleton(TransactionFilterStore()); + getIt.registerSingleton(FiatConvertationStore()); getIt.registerFactory( () => KeyService(getIt.get())); @@ -145,7 +145,7 @@ Future setup( () => BalanceViewModel( wallet: getIt.get().wallet, settingsStore: getIt.get(), - priceStore: priceStore)); + fiatConvertationStore: getIt.get())); getIt.registerFactory( () => DashboardViewModel( diff --git a/lib/main.dart b/lib/main.dart index ce3739970..d1be6866d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -47,6 +47,7 @@ import 'package:cake_wallet/src/domain/common/wallet_type.dart'; import 'package:cake_wallet/src/domain/common/template.dart'; import 'package:cake_wallet/src/domain/exchange/exchange_template.dart'; import 'package:cake_wallet/src/domain/services/wallet_service.dart'; +import 'package:cake_wallet/src/domain/services/fiat_convertation_service.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/src/domain/common/language.dart'; import 'package:cake_wallet/src/stores/seed_language/seed_language_store.dart'; @@ -87,6 +88,7 @@ void main() async { final sharedPreferences = await SharedPreferences.getInstance(); final walletService = WalletService(); + final fiatConvertationService = FiatConvertationService(); final walletListService = WalletListService( secureStorage: secureStorage, walletInfoSource: walletInfoSource, @@ -124,7 +126,7 @@ void main() async { walletInfoSource: walletInfoSource, contactSource: contacts, tradesSource: trades, - priceStore: priceStore, + fiatConvertationService: fiatConvertationService, initialMigrationVersion: 3); setReactions( @@ -166,7 +168,7 @@ Future initialSetup( @required Box walletInfoSource, @required Box contactSource, @required Box tradesSource, - @required PriceStore priceStore, + @required FiatConvertationService fiatConvertationService, int initialMigrationVersion = 3}) async { await defaultSettingsMigration( version: initialMigrationVersion, @@ -176,9 +178,8 @@ Future initialSetup( walletInfoSource: walletInfoSource, nodeSource: nodes, contactSource: contactSource, - tradesSource: tradesSource, - priceStore: priceStore); - await bootstrap(); + tradesSource: tradesSource); + await bootstrap(fiatConvertationService: fiatConvertationService); monero_wallet.onStartup(); } diff --git a/lib/reactions/bootstrap.dart b/lib/reactions/bootstrap.dart index 928fb41ba..b190697c5 100644 --- a/lib/reactions/bootstrap.dart +++ b/lib/reactions/bootstrap.dart @@ -10,10 +10,14 @@ import 'package:cake_wallet/monero/monero_wallet_service.dart'; import 'package:cake_wallet/core/wallet_base.dart'; import 'package:cake_wallet/core/wallet_service.dart'; 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/src/domain/common/wallet_type.dart'; import 'package:cake_wallet/src/domain/common/secret_store_key.dart'; import 'package:cake_wallet/src/domain/common/encrypt.dart'; +import 'package:cake_wallet/src/domain/services/fiat_convertation_service.dart'; +import 'package:cake_wallet/src/domain/common/fiat_currency.dart'; +import 'package:cake_wallet/store/dashboard/fiat_convertation_store.dart'; // FIXME: move me Future loadCurrentWallet() async { @@ -44,9 +48,12 @@ Future loadCurrentWallet() async { ReactionDisposer _initialAuthReaction; ReactionDisposer _onCurrentWalletChangeReaction; ReactionDisposer _onWalletSyncStatusChangeReaction; +ReactionDisposer _onCurrentFiatCurrencyChangeDisposer; -Future bootstrap() async { +Future bootstrap({FiatConvertationService fiatConvertationService}) async { final authenticationStore = getIt.get(); + final settingsStore = getIt.get(); + final fiatConvertationStore = getIt.get(); if (authenticationStore.state == AuthenticationState.uninitialized) { authenticationStore.state = @@ -81,5 +88,30 @@ Future bootstrap() async { .setInt('current_wallet_type', serializeToInt(wallet.type)); await wallet.connectToNode(node: null); + + final cryptoCurrency = wallet.currency; + final fiatCurrency = settingsStore.fiatCurrency; + + final price = await fiatConvertationService.getPrice( + crypto: cryptoCurrency, + fiat: fiatCurrency + ); + + fiatConvertationStore.setPrice(price); + }); + + // + + _onCurrentFiatCurrencyChangeDisposer ??= + reaction((_) => settingsStore.fiatCurrency, + (FiatCurrency fiatCurrency) async { + final cryptoCurrency = getIt.get().wallet.currency; + + final price = await fiatConvertationService.getPrice( + crypto: cryptoCurrency, + fiat: fiatCurrency + ); + + fiatConvertationStore.setPrice(price); }); } diff --git a/lib/src/domain/services/fiat_convertation_service.dart b/lib/src/domain/services/fiat_convertation_service.dart new file mode 100644 index 000000000..ad81b35e5 --- /dev/null +++ b/lib/src/domain/services/fiat_convertation_service.dart @@ -0,0 +1,10 @@ +import 'dart:async'; +import 'package:cake_wallet/src/domain/common/fiat_currency.dart'; +import 'package:cake_wallet/src/domain/common/crypto_currency.dart'; +import 'package:cake_wallet/src/domain/common/fetch_price.dart'; + +class FiatConvertationService { + Future getPrice({CryptoCurrency crypto, FiatCurrency fiat}) async { + return await fetchPriceFor(crypto: crypto, fiat: fiat); + } +} \ No newline at end of file diff --git a/lib/store/dashboard/fiat_convertation_store.dart b/lib/store/dashboard/fiat_convertation_store.dart new file mode 100644 index 000000000..1b1a59b57 --- /dev/null +++ b/lib/store/dashboard/fiat_convertation_store.dart @@ -0,0 +1,19 @@ +import 'package:mobx/mobx.dart'; + +part 'fiat_convertation_store.g.dart'; + +class FiatConvertationStore = FiatConvertationStoreBase with _$FiatConvertationStore; + +abstract class FiatConvertationStoreBase with Store { + FiatConvertationStoreBase() { + setPrice(0.0); + } + + @observable + double price; + + @action + void setPrice(double price) { + this.price = price; + } +} \ No newline at end of file diff --git a/lib/view_model/dashboard/balance_view_model.dart b/lib/view_model/dashboard/balance_view_model.dart index afb8eae62..62f7a44ef 100644 --- a/lib/view_model/dashboard/balance_view_model.dart +++ b/lib/view_model/dashboard/balance_view_model.dart @@ -3,10 +3,9 @@ import 'package:cake_wallet/core/wallet_base.dart'; import 'package:cake_wallet/monero/monero_wallet.dart'; import 'package:cake_wallet/src/domain/common/balance_display_mode.dart'; import 'package:cake_wallet/src/domain/common/calculate_fiat_amount.dart'; -import 'package:cake_wallet/src/domain/common/crypto_currency.dart'; import 'package:cake_wallet/view_model/dashboard/wallet_balance.dart'; import 'package:cake_wallet/store/settings_store.dart'; -import 'package:cake_wallet/src/stores/price/price_store.dart'; +import 'package:cake_wallet/store/dashboard/fiat_convertation_store.dart'; import 'package:flutter/cupertino.dart'; import 'package:mobx/mobx.dart'; @@ -18,12 +17,12 @@ abstract class BalanceViewModelBase with Store { BalanceViewModelBase({ @required this.wallet, @required this.settingsStore, - @required this.priceStore + @required this.fiatConvertationStore }); final WalletBase wallet; final SettingsStore settingsStore; - final PriceStore priceStore; + final FiatConvertationStore fiatConvertationStore; WalletBalance _getWalletBalance() { final _wallet = wallet; @@ -50,22 +49,7 @@ abstract class BalanceViewModelBase with Store { } @computed - double get price { - String symbol; - final _wallet = wallet; - - if (_wallet is MoneroWallet) { - symbol = PriceStoreBase.generateSymbolForPair( - fiat: settingsStore.fiatCurrency, crypto: CryptoCurrency.xmr); - } - - if (_wallet is BitcoinWallet) { - symbol = PriceStoreBase.generateSymbolForPair( - fiat: settingsStore.fiatCurrency, crypto: CryptoCurrency.btc); - } - - return priceStore.prices[symbol]; - } + double get price => fiatConvertationStore.price; @computed String get cryptoBalance {