2020-12-24 20:57:32 +00:00
|
|
|
import 'package:cake_wallet/bitcoin/bitcoin_balance.dart';
|
2020-07-23 12:20:52 +00:00
|
|
|
import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
|
2020-12-16 18:39:53 +00:00
|
|
|
import 'package:cake_wallet/core/wallet_base.dart';
|
2020-12-24 20:57:32 +00:00
|
|
|
import 'package:cake_wallet/entities/balance.dart';
|
2020-09-26 12:19:33 +00:00
|
|
|
import 'package:cake_wallet/entities/crypto_currency.dart';
|
2020-12-16 18:39:53 +00:00
|
|
|
import 'package:cake_wallet/monero/monero_balance.dart';
|
2020-07-23 12:20:52 +00:00
|
|
|
import 'package:cake_wallet/monero/monero_wallet.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/entities/balance_display_mode.dart';
|
|
|
|
import 'package:cake_wallet/entities/calculate_fiat_amount.dart';
|
2020-09-24 09:16:13 +00:00
|
|
|
import 'package:cake_wallet/store/app_store.dart';
|
2020-07-23 12:20:52 +00:00
|
|
|
import 'package:cake_wallet/view_model/dashboard/wallet_balance.dart';
|
|
|
|
import 'package:cake_wallet/store/settings_store.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
|
2020-07-23 12:20:52 +00:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
|
|
|
|
part 'balance_view_model.g.dart';
|
|
|
|
|
|
|
|
class BalanceViewModel = BalanceViewModelBase with _$BalanceViewModel;
|
|
|
|
|
|
|
|
abstract class BalanceViewModelBase with Store {
|
2020-12-15 16:29:10 +00:00
|
|
|
BalanceViewModelBase(
|
|
|
|
{@required this.appStore,
|
|
|
|
@required this.settingsStore,
|
2021-01-05 19:49:14 +00:00
|
|
|
@required this.fiatConvertationStore}) {
|
2020-12-16 18:39:53 +00:00
|
|
|
isReversing = false;
|
|
|
|
|
|
|
|
wallet ??= appStore.wallet;
|
|
|
|
|
|
|
|
_reaction = reaction((_) => appStore.wallet, _onWalletChange);
|
|
|
|
|
|
|
|
final _wallet = wallet;
|
|
|
|
|
|
|
|
if (_wallet is MoneroWallet) {
|
2020-12-24 20:57:32 +00:00
|
|
|
balance = _wallet.balance;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_wallet is BitcoinWallet) {
|
|
|
|
balance = _wallet.balance;
|
2020-12-16 18:39:53 +00:00
|
|
|
}
|
2021-01-05 19:49:14 +00:00
|
|
|
|
|
|
|
_onCurrentWalletChangeReaction =
|
|
|
|
reaction<void>((_) => wallet.balance, (dynamic balance) {
|
|
|
|
if (balance is Balance) {
|
|
|
|
this.balance = balance;
|
|
|
|
}
|
|
|
|
});
|
2020-12-16 18:39:53 +00:00
|
|
|
}
|
2020-07-23 12:20:52 +00:00
|
|
|
|
2020-09-24 09:16:13 +00:00
|
|
|
final AppStore appStore;
|
2020-07-23 12:20:52 +00:00
|
|
|
final SettingsStore settingsStore;
|
2020-09-21 11:50:26 +00:00
|
|
|
final FiatConversionStore fiatConvertationStore;
|
2020-07-23 12:20:52 +00:00
|
|
|
|
2020-12-15 16:29:10 +00:00
|
|
|
bool get canReverse =>
|
|
|
|
(appStore.wallet.balance.availableModes as List).length > 1;
|
|
|
|
|
2020-09-29 08:45:35 +00:00
|
|
|
@observable
|
|
|
|
bool isReversing;
|
|
|
|
|
2020-12-16 18:39:53 +00:00
|
|
|
@observable
|
2020-12-24 20:57:32 +00:00
|
|
|
Balance balance;
|
2020-12-16 18:39:53 +00:00
|
|
|
|
|
|
|
@observable
|
|
|
|
WalletBase wallet;
|
|
|
|
|
2020-09-29 08:45:35 +00:00
|
|
|
@computed
|
2020-12-15 16:29:10 +00:00
|
|
|
double get price => fiatConvertationStore.prices[appStore.wallet.currency];
|
2020-09-29 08:45:35 +00:00
|
|
|
|
|
|
|
@computed
|
2020-12-15 16:29:10 +00:00
|
|
|
BalanceDisplayMode get savedDisplayMode => settingsStore.balanceDisplayMode;
|
2020-09-29 08:45:35 +00:00
|
|
|
|
2020-07-23 12:20:52 +00:00
|
|
|
@computed
|
2020-12-15 16:29:10 +00:00
|
|
|
BalanceDisplayMode get displayMode => isReversing
|
|
|
|
? (savedDisplayMode == BalanceDisplayMode.availableBalance
|
|
|
|
? BalanceDisplayMode.fullBalance
|
|
|
|
: BalanceDisplayMode.availableBalance)
|
|
|
|
: savedDisplayMode;
|
2020-07-23 12:20:52 +00:00
|
|
|
|
|
|
|
@computed
|
|
|
|
String get cryptoBalance {
|
2020-09-24 09:16:13 +00:00
|
|
|
final walletBalance = _walletBalance;
|
2020-12-24 20:57:32 +00:00
|
|
|
var _balance = '---';
|
2020-07-23 12:20:52 +00:00
|
|
|
|
|
|
|
if (displayMode == BalanceDisplayMode.availableBalance) {
|
2020-12-24 20:57:32 +00:00
|
|
|
_balance = walletBalance.unlockedBalance ?? '0.0';
|
2020-07-23 12:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (displayMode == BalanceDisplayMode.fullBalance) {
|
2020-12-24 20:57:32 +00:00
|
|
|
_balance = walletBalance.totalBalance ?? '0.0';
|
2020-07-23 12:20:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 20:57:32 +00:00
|
|
|
return _balance;
|
2020-07-23 12:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@computed
|
|
|
|
String get fiatBalance {
|
2020-09-24 09:16:13 +00:00
|
|
|
final walletBalance = _walletBalance;
|
2020-07-23 12:20:52 +00:00
|
|
|
final fiatCurrency = settingsStore.fiatCurrency;
|
2020-12-24 20:57:32 +00:00
|
|
|
var _balance = '---';
|
2020-07-23 12:20:52 +00:00
|
|
|
|
2020-12-15 16:29:10 +00:00
|
|
|
final totalBalance =
|
|
|
|
_getFiatBalance(price: price, cryptoAmount: walletBalance.totalBalance);
|
2020-07-23 12:20:52 +00:00
|
|
|
|
|
|
|
final unlockedBalance = _getFiatBalance(
|
2020-12-15 16:29:10 +00:00
|
|
|
price: price, cryptoAmount: walletBalance.unlockedBalance);
|
2020-07-23 12:20:52 +00:00
|
|
|
|
|
|
|
if (displayMode == BalanceDisplayMode.availableBalance) {
|
2020-12-24 20:57:32 +00:00
|
|
|
_balance = fiatCurrency.toString() + ' ' + unlockedBalance ?? '0.00';
|
2020-07-23 12:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (displayMode == BalanceDisplayMode.fullBalance) {
|
2020-12-24 20:57:32 +00:00
|
|
|
_balance = fiatCurrency.toString() + ' ' + totalBalance ?? '0.00';
|
2020-07-23 12:20:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 20:57:32 +00:00
|
|
|
return _balance;
|
2020-07-23 12:20:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 09:16:13 +00:00
|
|
|
@computed
|
|
|
|
WalletBalance get _walletBalance {
|
2020-12-24 20:57:32 +00:00
|
|
|
final _balance = balance;
|
2020-09-24 09:16:13 +00:00
|
|
|
|
2020-12-24 20:57:32 +00:00
|
|
|
if (_balance is MoneroBalance) {
|
2021-01-05 19:49:14 +00:00
|
|
|
return WalletBalance(
|
2020-12-24 20:57:32 +00:00
|
|
|
unlockedBalance: _balance.formattedUnlockedBalance,
|
|
|
|
totalBalance: _balance.formattedFullBalance);
|
2020-09-24 09:16:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 20:57:32 +00:00
|
|
|
if (_balance is BitcoinBalance) {
|
2020-09-24 09:16:13 +00:00
|
|
|
return WalletBalance(
|
2020-12-24 20:57:32 +00:00
|
|
|
unlockedBalance: _balance.availableBalanceFormatted,
|
|
|
|
totalBalance: _balance.totalFormatted);
|
2020-09-24 09:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-26 12:19:33 +00:00
|
|
|
@computed
|
|
|
|
CryptoCurrency get currency => appStore.wallet.currency;
|
|
|
|
|
2021-01-05 19:49:14 +00:00
|
|
|
ReactionDisposer _onCurrentWalletChangeReaction;
|
|
|
|
ReactionDisposer _reaction;
|
|
|
|
|
2020-12-16 18:39:53 +00:00
|
|
|
@action
|
|
|
|
void _onWalletChange(WalletBase wallet) {
|
|
|
|
this.wallet = wallet;
|
|
|
|
|
|
|
|
if (wallet is MoneroWallet) {
|
2020-12-24 20:57:32 +00:00
|
|
|
balance = wallet.balance;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wallet is BitcoinWallet) {
|
|
|
|
balance = wallet.balance;
|
2020-12-16 18:39:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 19:49:14 +00:00
|
|
|
_onCurrentWalletChangeReaction?.reaction?.dispose();
|
|
|
|
_onCurrentWalletChangeReaction =
|
|
|
|
reaction<void>((_) => wallet.balance, (dynamic balance) {
|
|
|
|
if (balance is Balance) {
|
|
|
|
this.balance = balance;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-12-16 18:39:53 +00:00
|
|
|
|
2020-09-24 09:16:13 +00:00
|
|
|
String _getFiatBalance({double price, String cryptoAmount}) {
|
|
|
|
if (cryptoAmount == null) {
|
|
|
|
return '0.00';
|
|
|
|
}
|
|
|
|
|
|
|
|
return calculateFiatAmount(price: price, cryptoAmount: cryptoAmount);
|
|
|
|
}
|
2021-01-05 19:49:14 +00:00
|
|
|
}
|