cake_wallet/lib/view_model/dashboard/balance_view_model.dart

168 lines
4.4 KiB
Dart
Raw Normal View History

2021-12-24 12:37:24 +00:00
import 'package:cw_core/transaction_history.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/balance.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/transaction_info.dart';
import 'package:cw_core/wallet_type.dart';
2021-01-11 17:15:27 +00:00
import 'package:cake_wallet/generated/i18n.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;
2021-12-24 12:37:24 +00:00
balance = wallet.balance;
2021-01-05 19:49:14 +00:00
reaction((_) => appStore.wallet, _onWalletChange);
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
WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>
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;
@action
void _onWalletChange(
WalletBase<Balance, TransactionHistoryBase<TransactionInfo>,
TransactionInfo>
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
}