CAKE-208 | added current balance display mode saving to shared preferences (settings_store.dart); added moneroBalance property, reactions and _onWalletChange() method to balance_view_model.dart

OleksandrSobol committed Dec 16, 2020 at 20:39 UTC d370c912bb94bca3f0c4bb1cd726c159959e53bc
2 files changed +50 -4
lib/store/settings_store.dart
+5
@@ -87,6 +87,11 @@ abstract class SettingsStoreBase with Store {
87 (_) => languageCode,
88 (String languageCode) => sharedPreferences.setString(
89 PreferencesKey.currentLanguageCode, languageCode));
90 +
91 + reaction((_) => balanceDisplayMode,
92 + (BalanceDisplayMode mode) => sharedPreferences.setInt(
93 + PreferencesKey.currentBalanceDisplayModeKey,
94 + mode.serialize()));
95 }
96
97 static const defaultPinLength = 4;
lib/view_model/dashboard/balance_view_model.dart
+45 -4
@@ -1,5 +1,7 @@
1 import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
2 +import 'package:cake_wallet/core/wallet_base.dart';
3 import 'package:cake_wallet/entities/crypto_currency.dart';
4 +import 'package:cake_wallet/monero/monero_balance.dart';
5 import 'package:cake_wallet/monero/monero_wallet.dart';
6 import 'package:cake_wallet/entities/balance_display_mode.dart';
7 import 'package:cake_wallet/entities/calculate_fiat_amount.dart';
@@ -19,7 +21,22 @@ abstract class BalanceViewModelBase with Store {
21 @required this.appStore,
22 @required this.settingsStore,
23 @required this.fiatConvertationStore
22 - }) : isReversing = false;
24 + }) {
25 + isReversing = false;
26 +
27 + wallet ??= appStore.wallet;
28 +
29 + _reaction = reaction((_) => appStore.wallet, _onWalletChange);
30 +
31 + final _wallet = wallet;
32 +
33 + if (_wallet is MoneroWallet) {
34 + moneroBalance = _wallet.balance;
35 +
36 + _onMoneroBalanceChangeReaction = reaction((_) => _wallet.balance,
37 + (MoneroBalance balance) => moneroBalance = balance);
38 + }
39 + }
40
41 final AppStore appStore;
42 final SettingsStore settingsStore;
@@ -28,6 +45,12 @@ abstract class BalanceViewModelBase with Store {
45 @observable
46 bool isReversing;
47
48 + @observable
49 + MoneroBalance moneroBalance;
50 +
51 + @observable
52 + WalletBase wallet;
53 +
54 @computed
55 BalanceDisplayMode get savedDisplayMode => settingsStore.balanceDisplayMode;
56
@@ -86,12 +109,12 @@ abstract class BalanceViewModelBase with Store {
109
110 @computed
111 WalletBalance get _walletBalance {
89 - final _wallet = appStore.wallet;
112 + final _wallet = wallet;
113
114 if (_wallet is MoneroWallet) {
115 return WalletBalance(
93 - unlockedBalance: _wallet.balance.formattedUnlockedBalance,
94 - totalBalance: _wallet.balance.formattedFullBalance);
116 + unlockedBalance: moneroBalance.formattedUnlockedBalance,
117 + totalBalance: moneroBalance.formattedFullBalance);
118 }
119
120 if (_wallet is BitcoinWallet) {
@@ -106,6 +129,24 @@ abstract class BalanceViewModelBase with Store {
129 @computed
130 CryptoCurrency get currency => appStore.wallet.currency;
131
132 + @action
133 + void _onWalletChange(WalletBase wallet) {
134 + this.wallet = wallet;
135 +
136 + if (wallet is MoneroWallet) {
137 + moneroBalance = wallet.balance;
138 +
139 + _onMoneroBalanceChangeReaction?.reaction?.dispose();
140 +
141 + _onMoneroBalanceChangeReaction = reaction((_) => wallet.balance,
142 + (MoneroBalance balance) => moneroBalance = balance);
143 + }
144 + }
145 +
146 + ReactionDisposer _onMoneroBalanceChangeReaction;
147 +
148 + ReactionDisposer _reaction;
149 +
150 String _getFiatBalance({double price, String cryptoAmount}) {
151 if (cryptoAmount == null) {
152 return '0.00';