cake_wallet/lib/view_model/dashboard/balance_view_model.dart

157 lines
4.2 KiB
Dart
Raw Normal View History

import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
import 'package:cake_wallet/core/wallet_base.dart';
2020-09-26 12:19:33 +00:00
import 'package:cake_wallet/entities/crypto_currency.dart';
import 'package:cake_wallet/monero/monero_balance.dart';
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';
import 'package:cake_wallet/store/app_store.dart';
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';
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 {
BalanceViewModelBase({
@required this.appStore,
@required this.settingsStore,
@required this.fiatConvertationStore
}) {
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;
2020-09-21 11:50:26 +00:00
final FiatConversionStore fiatConvertationStore;
@observable
bool isReversing;
@observable
MoneroBalance moneroBalance;
@observable
WalletBase wallet;
@computed
BalanceDisplayMode get savedDisplayMode => settingsStore.balanceDisplayMode;
@computed
BalanceDisplayMode get displayMode => isReversing
? (savedDisplayMode == BalanceDisplayMode.availableBalance
? BalanceDisplayMode.fullBalance
: BalanceDisplayMode.availableBalance)
: savedDisplayMode;
@computed
double get price => fiatConvertationStore.price;
@computed
String get cryptoBalance {
final walletBalance = _walletBalance;
var balance = '---';
if (displayMode == BalanceDisplayMode.availableBalance) {
balance = walletBalance.unlockedBalance ?? '0.0';
}
if (displayMode == BalanceDisplayMode.fullBalance) {
balance = walletBalance.totalBalance ?? '0.0';
}
return balance;
}
@computed
String get fiatBalance {
final walletBalance = _walletBalance;
final fiatCurrency = settingsStore.fiatCurrency;
var balance = '---';
final totalBalance = _getFiatBalance(
price: price,
cryptoAmount: walletBalance.totalBalance
);
final unlockedBalance = _getFiatBalance(
price: price,
cryptoAmount: walletBalance.unlockedBalance
);
if (displayMode == BalanceDisplayMode.availableBalance) {
balance = fiatCurrency.toString() + ' ' + unlockedBalance ?? '0.00';
}
if (displayMode == BalanceDisplayMode.fullBalance) {
balance = fiatCurrency.toString() + ' ' + totalBalance ?? '0.00';
}
return balance;
}
@computed
WalletBalance get _walletBalance {
final _wallet = wallet;
if (_wallet is MoneroWallet) {
return WalletBalance(
unlockedBalance: moneroBalance.formattedUnlockedBalance,
totalBalance: moneroBalance.formattedFullBalance);
}
if (_wallet is BitcoinWallet) {
return WalletBalance(
unlockedBalance: _wallet.balance.totalFormatted,
totalBalance: _wallet.balance.totalFormatted);
}
return null;
}
2020-09-26 12:19:33 +00:00
@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';
}
return calculateFiatAmount(price: price, cryptoAmount: cryptoAmount);
}
}