cake_wallet/lib/view_model/dashboard/balance_view_model.dart

177 lines
4.5 KiB
Dart
Raw Normal View History

import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
import 'package:cake_wallet/core/wallet_base.dart';
import 'package:cake_wallet/entities/balance.dart';
2020-09-26 12:19:33 +00:00
import 'package:cake_wallet/entities/crypto_currency.dart';
2021-01-11 17:15:27 +00:00
import 'package:cake_wallet/entities/wallet_type.dart';
import 'package:cake_wallet/generated/i18n.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/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,
2021-01-05 19:49:14 +00:00
@required this.fiatConvertationStore}) {
isReversing = false;
wallet ??= appStore.wallet;
_reaction = reaction((_) => appStore.wallet, _onWalletChange);
final _wallet = wallet;
if (_wallet is MoneroWallet) {
balance = _wallet.balance;
}
if (_wallet is BitcoinWallet) {
balance = _wallet.balance;
}
2021-01-05 19:49:14 +00:00
_onCurrentWalletChangeReaction =
reaction<void>((_) => wallet.balance, (dynamic balance) {
if (balance is Balance) {
this.balance = balance;
}
});
}
final AppStore appStore;
final SettingsStore settingsStore;
2020-09-21 11:50:26 +00:00
final FiatConversionStore fiatConvertationStore;
2021-01-11 17:15:27 +00:00
bool get canReverse => false;
@observable
bool isReversing;
@observable
Balance balance;
@observable
2021-01-11 17:15:27 +00:00
WalletBase<Balance> wallet;
@computed
double get price => fiatConvertationStore.prices[appStore.wallet.currency];
@computed
BalanceDisplayMode get savedDisplayMode => settingsStore.balanceDisplayMode;
@computed
BalanceDisplayMode get displayMode => isReversing
? savedDisplayMode == BalanceDisplayMode.hiddenBalance
? BalanceDisplayMode.displayableBalance
: savedDisplayMode
: savedDisplayMode;
@computed
2021-01-11 17:15:27 +00:00
String get availableBalanceLabel {
if (wallet.type == WalletType.monero) {
return S.current.xmr_available_balance;
}
2021-01-11 17:15:27 +00:00
return S.current.confirmed;
}
@computed
String get additionalBalanceLabel {
if (wallet.type == WalletType.monero) {
return S.current.xmr_full_balance;
}
2021-01-11 17:15:27 +00:00
return S.current.unconfirmed;
}
@computed
2021-01-11 17:15:27 +00:00
String get availableBalance {
final walletBalance = _walletBalance;
if (displayMode == BalanceDisplayMode.hiddenBalance) {
2021-01-11 17:15:27 +00:00
return '---';
}
2021-01-11 17:15:27 +00:00
return walletBalance.formattedAvailableBalance;
}
2021-01-11 17:15:27 +00:00
@computed
String get additionalBalance {
final walletBalance = _walletBalance;
if (displayMode == BalanceDisplayMode.hiddenBalance) {
2021-01-11 17:15:27 +00:00
return '---';
}
2021-01-11 17:15:27 +00:00
return walletBalance.formattedAdditionalBalance;
}
@computed
2021-01-11 17:15:27 +00:00
String get availableFiatBalance {
final walletBalance = _walletBalance;
final fiatCurrency = settingsStore.fiatCurrency;
if (displayMode == BalanceDisplayMode.hiddenBalance) {
2021-01-11 17:15:27 +00:00
return '---';
}
2021-01-11 17:15:27 +00:00
return fiatCurrency.toString() +
' ' +
_getFiatBalance(
price: price,
cryptoAmount: walletBalance.formattedAvailableBalance);
}
@computed
String get additionalFiatBalance {
final walletBalance = _walletBalance;
final fiatCurrency = settingsStore.fiatCurrency;
if (displayMode == BalanceDisplayMode.hiddenBalance) {
2021-01-11 17:15:27 +00:00
return '---';
}
2021-01-11 17:15:27 +00:00
return fiatCurrency.toString() +
' ' +
_getFiatBalance(
price: price,
cryptoAmount: walletBalance.formattedAdditionalBalance);
}
2021-01-11 17:15:27 +00:00
@computed
Balance get _walletBalance => wallet.balance;
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;
@action
2021-01-11 17:15:27 +00:00
void _onWalletChange(WalletBase<Balance> wallet) {
this.wallet = wallet;
2021-01-11 17:15:27 +00:00
balance = wallet.balance;
2021-01-05 19:49:14 +00:00
_onCurrentWalletChangeReaction?.reaction?.dispose();
2021-01-11 17:15:27 +00:00
_onCurrentWalletChangeReaction = reaction<Balance>(
(_) => wallet.balance, (Balance balance) => this.balance = balance);
2021-01-05 19:49:14 +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
}