From d370c912bb94bca3f0c4bb1cd726c159959e53bc Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Wed, 16 Dec 2020 20:39:53 +0200 Subject: [PATCH 1/3] 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'; From b1e0bf0edd56ce364eec325cdd30c75fe71c449f Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Fri, 18 Dec 2020 21:42:00 +0200 Subject: [PATCH 2/3] CAKE-208 | merged 4.1.0 branch into current and resolved problems --- lib/store/settings_store.dart | 10 +++++----- lib/view_model/dashboard/balance_view_model.dart | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/store/settings_store.dart b/lib/store/settings_store.dart index f4c80b883..cc8f8acce 100644 --- a/lib/store/settings_store.dart +++ b/lib/store/settings_store.dart @@ -85,14 +85,14 @@ abstract class SettingsStoreBase with Store { (String languageCode) => sharedPreferences.setString( PreferencesKey.currentLanguageCode, languageCode)); + reaction((_) => balanceDisplayMode, + (BalanceDisplayMode mode) => sharedPreferences.setInt( + PreferencesKey.currentBalanceDisplayModeKey, + mode.serialize())); + this .nodes .observe((change) => _saveCurrentNode(change.newValue, change.key)); - - 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 66d719771..a7bc575bd 100644 --- a/lib/view_model/dashboard/balance_view_model.dart +++ b/lib/view_model/dashboard/balance_view_model.dart @@ -20,8 +20,8 @@ abstract class BalanceViewModelBase with Store { BalanceViewModelBase( {@required this.appStore, @required this.settingsStore, - @required this.fiatConvertationStore}){ - + @required this.fiatConvertationStore + }){ isReversing = false; wallet ??= appStore.wallet; From d93c706997031ac9b18a3be4ccc0c00bb77e5664 Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Thu, 24 Dec 2020 22:57:32 +0200 Subject: [PATCH 3/3] CAKE-208 | changed MoneroBalance to Balance property and handled BitcoinBalance case in the balance_view_model.dart --- .../dashboard/balance_view_model.dart | 60 ++++++++++++------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/lib/view_model/dashboard/balance_view_model.dart b/lib/view_model/dashboard/balance_view_model.dart index a7bc575bd..0c4272343 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_balance.dart'; import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart'; import 'package:cake_wallet/core/wallet_base.dart'; +import 'package:cake_wallet/entities/balance.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'; @@ -31,10 +33,17 @@ abstract class BalanceViewModelBase with Store { final _wallet = wallet; if (_wallet is MoneroWallet) { - moneroBalance = _wallet.balance; + balance = _wallet.balance; _onMoneroBalanceChangeReaction = reaction((_) => _wallet.balance, - (MoneroBalance balance) => moneroBalance = balance); + (MoneroBalance moneroBalance) => balance = moneroBalance); + } + + if (_wallet is BitcoinWallet) { + balance = _wallet.balance; + + _onBitcoinBalanceChangeReaction = reaction((_) => _wallet.balance, + (BitcoinBalance bitcoinBalance) => balance = bitcoinBalance); } } @@ -49,7 +58,7 @@ abstract class BalanceViewModelBase with Store { bool isReversing; @observable - MoneroBalance moneroBalance; + Balance balance; @observable WalletBase wallet; @@ -70,24 +79,24 @@ abstract class BalanceViewModelBase with Store { @computed String get cryptoBalance { final walletBalance = _walletBalance; - var balance = '---'; + var _balance = '---'; if (displayMode == BalanceDisplayMode.availableBalance) { - balance = walletBalance.unlockedBalance ?? '0.0'; + _balance = walletBalance.unlockedBalance ?? '0.0'; } if (displayMode == BalanceDisplayMode.fullBalance) { - balance = walletBalance.totalBalance ?? '0.0'; + _balance = walletBalance.totalBalance ?? '0.0'; } - return balance; + return _balance; } @computed String get fiatBalance { final walletBalance = _walletBalance; final fiatCurrency = settingsStore.fiatCurrency; - var balance = '---'; + var _balance = '---'; final totalBalance = _getFiatBalance(price: price, cryptoAmount: walletBalance.totalBalance); @@ -96,30 +105,30 @@ abstract class BalanceViewModelBase with Store { price: price, cryptoAmount: walletBalance.unlockedBalance); if (displayMode == BalanceDisplayMode.availableBalance) { - balance = fiatCurrency.toString() + ' ' + unlockedBalance ?? '0.00'; + _balance = fiatCurrency.toString() + ' ' + unlockedBalance ?? '0.00'; } if (displayMode == BalanceDisplayMode.fullBalance) { - balance = fiatCurrency.toString() + ' ' + totalBalance ?? '0.00'; + _balance = fiatCurrency.toString() + ' ' + totalBalance ?? '0.00'; } - return balance; + return _balance; } @computed WalletBalance get _walletBalance { - final _wallet = wallet; + final _balance = balance; - if (_wallet is MoneroWallet) { + if (_balance is MoneroBalance) { return WalletBalance( - unlockedBalance: moneroBalance.formattedUnlockedBalance, - totalBalance: moneroBalance.formattedFullBalance); + unlockedBalance: _balance.formattedUnlockedBalance, + totalBalance: _balance.formattedFullBalance); } - if (_wallet is BitcoinWallet) { + if (_balance is BitcoinBalance) { return WalletBalance( - unlockedBalance: _wallet.balance.availableBalanceFormatted, - totalBalance: _wallet.balance.totalFormatted); + unlockedBalance: _balance.availableBalanceFormatted, + totalBalance: _balance.totalFormatted); } return null; @@ -133,17 +142,26 @@ abstract class BalanceViewModelBase with Store { this.wallet = wallet; if (wallet is MoneroWallet) { - moneroBalance = wallet.balance; + balance = wallet.balance; _onMoneroBalanceChangeReaction?.reaction?.dispose(); _onMoneroBalanceChangeReaction = reaction((_) => wallet.balance, - (MoneroBalance balance) => moneroBalance = balance); + (MoneroBalance moneroBalance) => balance = moneroBalance); + } + + if (wallet is BitcoinWallet) { + balance = wallet.balance; + + _onBitcoinBalanceChangeReaction?.reaction?.dispose(); + + _onBitcoinBalanceChangeReaction = reaction((_) => wallet.balance, + (BitcoinBalance bitcoinBalance) => balance = bitcoinBalance); } } ReactionDisposer _onMoneroBalanceChangeReaction; - + ReactionDisposer _onBitcoinBalanceChangeReaction; ReactionDisposer _reaction; String _getFiatBalance({double price, String cryptoAmount}) {