CW-441 xmr price issue (#1005)

* refactor fiat_rate_update * remove commented line

Matthew Fosse committed Jul 26, 2023 at 21:08 UTC b28f85350a659912ac22d9f35d44171ebb169488
2 files changed +19 -2
lib/reactions/fiat_rate_update.dart
+17 -1
@@ -6,6 +6,7 @@ import 'package:cake_wallet/store/app_store.dart';
6 import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
7 import 'package:cake_wallet/store/settings_store.dart';
8 import 'package:cw_core/wallet_type.dart';
9 +import 'package:mobx/mobx.dart';
10
11 Timer? _timer;
12
@@ -15,7 +16,7 @@ Future<void> startFiatRateUpdate(
16 return;
17 }
18
18 - _timer = Timer.periodic(Duration(seconds: 30), (_) async {
19 + final _updateFiat = (_) async {
20 try {
21 if (appStore.wallet == null || settingsStore.fiatApiMode == FiatApiMode.disabled) {
22 return;
@@ -33,5 +34,20 @@ Future<void> startFiatRateUpdate(
34 } catch (e) {
35 print(e);
36 }
37 + };
38 +
39 + _timer = Timer.periodic(Duration(seconds: 30), _updateFiat);
40 + // also run immediately:
41 + _updateFiat(null);
42 +
43 + // setup autorun to listen to changes in fiatApiMode
44 + autorun((_) {
45 + // restart the timer if fiatApiMode was re-enabled
46 + if (settingsStore.fiatApiMode != FiatApiMode.disabled) {
47 + _timer = Timer.periodic(Duration(seconds: 30), _updateFiat);
48 + _updateFiat(null);
49 + } else {
50 + _timer?.cancel();
51 + }
52 });
53 }
lib/view_model/dashboard/balance_view_model.dart
+2 -1
@@ -66,7 +66,8 @@ abstract class BalanceViewModelBase with Store {
66 final price = fiatConvertationStore.prices[appStore.wallet!.currency];
67
68 if (price == null) {
69 - throw Exception('No price for ${appStore.wallet!.currency} (current wallet)');
69 + // price should update on next fetch:
70 + return 0;
71 }
72
73 return price;