From d370c912bb94bca3f0c4bb1cd726c159959e53bc Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Wed, 16 Dec 2020 20:39:53 +0200 Subject: [PATCH] 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 --- lib/store/settings_store.dart | 5 ++ .../dashboard/balance_view_model.dart | 49 +++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/store/settings_store.dart b/lib/store/settings_store.dart index b4b76bb8d..321bbe23e 100644 --- a/lib/store/settings_store.dart +++ b/lib/store/settings_store.dart @@ -87,6 +87,11 @@ abstract class SettingsStoreBase with Store { (_) => languageCode, (String languageCode) => sharedPreferences.setString( PreferencesKey.currentLanguageCode, languageCode)); + + reaction((_) => balanceDisplayMode, + (BalanceDisplayMode mode) => sharedPreferences.setInt( + PreferencesKey.currentBalanceDisplayModeKey, + mode.serialize())); } static const defaultPinLength = 4; diff --git a/lib/view_model/dashboard/balance_view_model.dart b/lib/view_model/dashboard/balance_view_model.dart index 37f6e5247..ace6a470b 100644 --- a/lib/view_model/dashboard/balance_view_model.dart +++ b/lib/view_model/dashboard/balance_view_model.dart @@ -1,5 +1,7 @@ import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart'; +import 'package:cake_wallet/core/wallet_base.dart'; import 'package:cake_wallet/entities/crypto_currency.dart'; +import 'package:cake_wallet/monero/monero_balance.dart'; import 'package:cake_wallet/monero/monero_wallet.dart'; import 'package:cake_wallet/entities/balance_display_mode.dart'; import 'package:cake_wallet/entities/calculate_fiat_amount.dart'; @@ -19,7 +21,22 @@ abstract class BalanceViewModelBase with Store { @required this.appStore, @required this.settingsStore, @required this.fiatConvertationStore - }) : isReversing = false; + }) { + isReversing = false; + + wallet ??= appStore.wallet; + + _reaction = reaction((_) => appStore.wallet, _onWalletChange); + + final _wallet = wallet; + + if (_wallet is MoneroWallet) { + moneroBalance = _wallet.balance; + + _onMoneroBalanceChangeReaction = reaction((_) => _wallet.balance, + (MoneroBalance balance) => moneroBalance = balance); + } + } final AppStore appStore; final SettingsStore settingsStore; @@ -28,6 +45,12 @@ abstract class BalanceViewModelBase with Store { @observable bool isReversing; + @observable + MoneroBalance moneroBalance; + + @observable + WalletBase wallet; + @computed BalanceDisplayMode get savedDisplayMode => settingsStore.balanceDisplayMode; @@ -86,12 +109,12 @@ abstract class BalanceViewModelBase with Store { @computed WalletBalance get _walletBalance { - final _wallet = appStore.wallet; + final _wallet = wallet; if (_wallet is MoneroWallet) { return WalletBalance( - unlockedBalance: _wallet.balance.formattedUnlockedBalance, - totalBalance: _wallet.balance.formattedFullBalance); + unlockedBalance: moneroBalance.formattedUnlockedBalance, + totalBalance: moneroBalance.formattedFullBalance); } if (_wallet is BitcoinWallet) { @@ -106,6 +129,24 @@ abstract class BalanceViewModelBase with Store { @computed CryptoCurrency get currency => appStore.wallet.currency; + @action + void _onWalletChange(WalletBase wallet) { + this.wallet = wallet; + + if (wallet is MoneroWallet) { + moneroBalance = wallet.balance; + + _onMoneroBalanceChangeReaction?.reaction?.dispose(); + + _onMoneroBalanceChangeReaction = reaction((_) => wallet.balance, + (MoneroBalance balance) => moneroBalance = balance); + } + } + + ReactionDisposer _onMoneroBalanceChangeReaction; + + ReactionDisposer _reaction; + String _getFiatBalance({double price, String cryptoAmount}) { if (cryptoAmount == null) { return '0.00';