diff --git a/lib/reactions/fiat_rate_update.dart b/lib/reactions/fiat_rate_update.dart index f9ddbef52..336f04970 100644 --- a/lib/reactions/fiat_rate_update.dart +++ b/lib/reactions/fiat_rate_update.dart @@ -6,6 +6,7 @@ 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:cw_core/wallet_type.dart'; +import 'package:mobx/mobx.dart'; Timer? _timer; @@ -15,7 +16,7 @@ Future startFiatRateUpdate( return; } - _timer = Timer.periodic(Duration(seconds: 30), (_) async { + final _updateFiat = (_) async { try { if (appStore.wallet == null || settingsStore.fiatApiMode == FiatApiMode.disabled) { return; @@ -33,5 +34,20 @@ Future startFiatRateUpdate( } catch (e) { print(e); } + }; + + _timer = Timer.periodic(Duration(seconds: 30), _updateFiat); + // also run immediately: + _updateFiat(null); + + // setup autorun to listen to changes in fiatApiMode + autorun((_) { + // restart the timer if fiatApiMode was re-enabled + if (settingsStore.fiatApiMode != FiatApiMode.disabled) { + _timer = Timer.periodic(Duration(seconds: 30), _updateFiat); + _updateFiat(null); + } else { + _timer?.cancel(); + } }); } diff --git a/lib/view_model/dashboard/balance_view_model.dart b/lib/view_model/dashboard/balance_view_model.dart index 810d8e415..a36ad609c 100644 --- a/lib/view_model/dashboard/balance_view_model.dart +++ b/lib/view_model/dashboard/balance_view_model.dart @@ -66,7 +66,8 @@ abstract class BalanceViewModelBase with Store { final price = fiatConvertationStore.prices[appStore.wallet!.currency]; if (price == null) { - throw Exception('No price for ${appStore.wallet!.currency} (current wallet)'); + // price should update on next fetch: + return 0; } return price;