cake_wallet/lib/src/start_updating_price.dart

38 lines
1.4 KiB
Dart
Raw Normal View History

2020-01-04 19:31:52 +00:00
import 'dart:async';
2020-01-08 12:26:34 +00:00
import 'package:cake_wallet/src/domain/common/fiat_currency.dart';
2020-01-04 19:31:52 +00:00
import 'package:flutter/foundation.dart';
import 'package:cake_wallet/src/domain/common/crypto_currency.dart';
import 'package:cake_wallet/src/domain/common/fetch_price.dart';
import 'package:cake_wallet/src/stores/price/price_store.dart';
import 'package:cake_wallet/src/stores/settings/settings_store.dart';
bool _startedUpdatingPrice = false;
2020-01-08 12:26:34 +00:00
Future<double> _updatePrice(Map args) async => await fetchPriceFor(
fiat: args['fiat'] as FiatCurrency,
crypto: args['crypto'] as CryptoCurrency);
2020-01-04 19:31:52 +00:00
2020-01-08 12:26:34 +00:00
Future<double> updatePrice(Map args) async => compute(_updatePrice, args);
2020-01-04 19:31:52 +00:00
2020-01-08 12:26:34 +00:00
Future<void> startUpdatingPrice(
{SettingsStore settingsStore, PriceStore priceStore}) async {
2020-01-04 19:31:52 +00:00
if (_startedUpdatingPrice) {
return;
}
const currentCrypto = CryptoCurrency.xmr;
_startedUpdatingPrice = true;
final price = await updatePrice(
2020-01-08 12:26:34 +00:00
<String, dynamic>{'fiat': settingsStore.fiatCurrency, 'crypto': currentCrypto});
2020-01-04 19:31:52 +00:00
priceStore.changePriceForPair(
fiat: settingsStore.fiatCurrency, crypto: currentCrypto, price: price);
Timer.periodic(Duration(seconds: 30), (_) async {
final price = await updatePrice(
2020-01-08 12:26:34 +00:00
<String, dynamic>{'fiat': settingsStore.fiatCurrency, 'crypto': currentCrypto});
2020-01-04 19:31:52 +00:00
priceStore.changePriceForPair(
fiat: settingsStore.fiatCurrency, crypto: currentCrypto, price: price);
});
}