mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-11-13 09:04:09 +01:00
@@ -15,6 +15,21 @@ import 'package:mobx/mobx.dart';
|
||||
|
||||
part 'balance_view_model.g.dart';
|
||||
|
||||
class BalanceRecord {
|
||||
const BalanceRecord({this.availableBalance,
|
||||
this.additionalBalance,
|
||||
this.fiatAvailableBalance,
|
||||
this.fiatAdditionalBalance,
|
||||
this.asset,
|
||||
this.formattedAssetTitle});
|
||||
final String fiatAdditionalBalance;
|
||||
final String fiatAvailableBalance;
|
||||
final String additionalBalance;
|
||||
final String availableBalance;
|
||||
final CryptoCurrency asset;
|
||||
final String formattedAssetTitle;
|
||||
}
|
||||
|
||||
class BalanceViewModel = BalanceViewModelBase with _$BalanceViewModel;
|
||||
|
||||
abstract class BalanceViewModelBase with Store {
|
||||
@@ -24,16 +39,7 @@ abstract class BalanceViewModelBase with Store {
|
||||
@required this.fiatConvertationStore}) {
|
||||
isReversing = false;
|
||||
wallet ??= appStore.wallet;
|
||||
balance = wallet.balance;
|
||||
|
||||
reaction((_) => appStore.wallet, _onWalletChange);
|
||||
|
||||
_onCurrentWalletChangeReaction =
|
||||
reaction<void>((_) => wallet.balance, (dynamic balance) {
|
||||
if (balance is Balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
final AppStore appStore;
|
||||
@@ -45,9 +51,6 @@ abstract class BalanceViewModelBase with Store {
|
||||
@observable
|
||||
bool isReversing;
|
||||
|
||||
@observable
|
||||
Balance balance;
|
||||
|
||||
@observable
|
||||
WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>
|
||||
wallet;
|
||||
@@ -58,47 +61,56 @@ abstract class BalanceViewModelBase with Store {
|
||||
@computed
|
||||
BalanceDisplayMode get savedDisplayMode => settingsStore.balanceDisplayMode;
|
||||
|
||||
@computed
|
||||
@computed
|
||||
String get asset {
|
||||
|
||||
switch(appStore.wallet.currency){
|
||||
case CryptoCurrency.btc:
|
||||
return 'Bitcoin Assets';
|
||||
case CryptoCurrency.xmr:
|
||||
return 'Monero Assets';
|
||||
case CryptoCurrency.ltc:
|
||||
return 'Litecoin Assets';
|
||||
final typeFormatted = walletTypeToString(appStore.wallet.type);
|
||||
|
||||
switch(wallet.type) {
|
||||
case WalletType.haven:
|
||||
return '$typeFormatted Assets';
|
||||
default:
|
||||
return '';
|
||||
return typeFormatted;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@computed
|
||||
BalanceDisplayMode get displayMode => isReversing
|
||||
? savedDisplayMode == BalanceDisplayMode.hiddenBalance
|
||||
? BalanceDisplayMode.displayableBalance
|
||||
: savedDisplayMode
|
||||
: savedDisplayMode;
|
||||
BalanceDisplayMode get displayMode {
|
||||
if (isReversing) {
|
||||
if (savedDisplayMode == BalanceDisplayMode.hiddenBalance) {
|
||||
return BalanceDisplayMode.displayableBalance;
|
||||
} else {
|
||||
return BalanceDisplayMode.hiddenBalance;
|
||||
}
|
||||
}
|
||||
|
||||
return savedDisplayMode;
|
||||
}
|
||||
|
||||
@computed
|
||||
String get availableBalanceLabel {
|
||||
if (wallet.type == WalletType.monero) {
|
||||
return S.current.xmr_available_balance;
|
||||
switch(wallet.type) {
|
||||
case WalletType.monero:
|
||||
case WalletType.haven:
|
||||
return S.current.xmr_available_balance;
|
||||
default:
|
||||
return S.current.confirmed;
|
||||
}
|
||||
|
||||
return S.current.confirmed;
|
||||
}
|
||||
|
||||
@computed
|
||||
String get additionalBalanceLabel {
|
||||
if (wallet.type == WalletType.monero) {
|
||||
return S.current.xmr_full_balance;
|
||||
switch(wallet.type) {
|
||||
case WalletType.monero:
|
||||
case WalletType.haven:
|
||||
return S.current.xmr_full_balance;
|
||||
default:
|
||||
return S.current.unconfirmed;
|
||||
}
|
||||
|
||||
return S.current.unconfirmed;
|
||||
}
|
||||
|
||||
@computed
|
||||
bool get hasMultiBalance => appStore.wallet.type == WalletType.haven;
|
||||
|
||||
@computed
|
||||
String get availableBalance {
|
||||
final walletBalance = _walletBalance;
|
||||
@@ -152,7 +164,73 @@ abstract class BalanceViewModelBase with Store {
|
||||
}
|
||||
|
||||
@computed
|
||||
Balance get _walletBalance => wallet.balance;
|
||||
Map<CryptoCurrency, BalanceRecord> get balances {
|
||||
return wallet.balance.map((key, value) {
|
||||
if (displayMode == BalanceDisplayMode.hiddenBalance) {
|
||||
return MapEntry(key, BalanceRecord(
|
||||
availableBalance: '---',
|
||||
additionalBalance: '---',
|
||||
fiatAdditionalBalance: '---',
|
||||
fiatAvailableBalance: '---',
|
||||
asset: key,
|
||||
formattedAssetTitle: _formatterAsset(key)));
|
||||
}
|
||||
final fiatCurrency = settingsStore.fiatCurrency;
|
||||
final additionalFiatBalance = fiatCurrency.toString()
|
||||
+ ' '
|
||||
+ _getFiatBalance(
|
||||
price: fiatConvertationStore.prices[key],
|
||||
cryptoAmount: value.formattedAdditionalBalance);
|
||||
|
||||
final availableFiatBalance = fiatCurrency.toString()
|
||||
+ ' '
|
||||
+ _getFiatBalance(
|
||||
price: fiatConvertationStore.prices[key],
|
||||
cryptoAmount: value.formattedAvailableBalance);
|
||||
|
||||
return MapEntry(key, BalanceRecord(
|
||||
availableBalance: value.formattedAvailableBalance,
|
||||
additionalBalance: value.formattedAdditionalBalance,
|
||||
fiatAdditionalBalance: additionalFiatBalance,
|
||||
fiatAvailableBalance: availableFiatBalance,
|
||||
asset: key,
|
||||
formattedAssetTitle: _formatterAsset(key)));
|
||||
});
|
||||
}
|
||||
|
||||
@computed
|
||||
List<BalanceRecord> get formattedBalances {
|
||||
final balance = balances.values.toList();
|
||||
|
||||
balance.sort((BalanceRecord a, BalanceRecord b) {
|
||||
if (b.asset == CryptoCurrency.xhv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (b.asset == CryptoCurrency.xusd) {
|
||||
if (a.asset == CryptoCurrency.xhv) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (b.asset == CryptoCurrency.xbtc) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (b.asset == CryptoCurrency.xeur) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
});
|
||||
|
||||
return balance;
|
||||
}
|
||||
|
||||
@computed
|
||||
Balance get _walletBalance => wallet.balance[wallet.currency];
|
||||
|
||||
@computed
|
||||
CryptoCurrency get currency => appStore.wallet.currency;
|
||||
@@ -164,11 +242,8 @@ abstract class BalanceViewModelBase with Store {
|
||||
WalletBase<Balance, TransactionHistoryBase<TransactionInfo>,
|
||||
TransactionInfo>
|
||||
wallet) {
|
||||
this.wallet = wallet;
|
||||
balance = wallet.balance;
|
||||
this.wallet = wallet;
|
||||
_onCurrentWalletChangeReaction?.reaction?.dispose();
|
||||
_onCurrentWalletChangeReaction = reaction<Balance>(
|
||||
(_) => wallet.balance, (Balance balance) => this.balance = balance);
|
||||
}
|
||||
|
||||
String _getFiatBalance({double price, String cryptoAmount}) {
|
||||
@@ -178,5 +253,20 @@ abstract class BalanceViewModelBase with Store {
|
||||
|
||||
return calculateFiatAmount(price: price, cryptoAmount: cryptoAmount);
|
||||
}
|
||||
|
||||
String _formatterAsset(CryptoCurrency asset) {
|
||||
switch (wallet.type) {
|
||||
case WalletType.haven:
|
||||
final assetStringified = asset.toString();
|
||||
|
||||
if (asset != CryptoCurrency.xhv && assetStringified[0].toUpperCase() == 'X') {
|
||||
return assetStringified.replaceFirst('X', 'x');
|
||||
}
|
||||
|
||||
return asset.toString();
|
||||
default:
|
||||
return asset.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:cake_wallet/wallet_type_utils.dart';
|
||||
import 'package:cw_core/transaction_history.dart';
|
||||
import 'package:cw_core/balance.dart';
|
||||
import 'package:cake_wallet/buy/order.dart';
|
||||
@@ -78,6 +79,8 @@ abstract class DashboardViewModelBase with Store {
|
||||
isShowFirstYatIntroduction = false;
|
||||
isShowSecondYatIntroduction = false;
|
||||
isShowThirdYatIntroduction = false;
|
||||
updateActions();
|
||||
|
||||
final _wallet = wallet;
|
||||
|
||||
if (_wallet.type == WalletType.monero) {
|
||||
@@ -229,6 +232,24 @@ abstract class DashboardViewModelBase with Store {
|
||||
void furtherShowYatPopup(bool shouldShow) =>
|
||||
settingsStore.shouldShowYatPopup = shouldShow;
|
||||
|
||||
@observable
|
||||
bool isEnabledExchangeAction;
|
||||
|
||||
@observable
|
||||
bool hasExchangeAction;
|
||||
|
||||
@observable
|
||||
bool isEnabledBuyAction;
|
||||
|
||||
@observable
|
||||
bool hasBuyAction;
|
||||
|
||||
@observable
|
||||
bool isEnabledSellAction;
|
||||
|
||||
@observable
|
||||
bool hasSellAction;
|
||||
|
||||
ReactionDisposer _onMoneroAccountChangeReaction;
|
||||
|
||||
ReactionDisposer _onMoneroBalanceChangeReaction;
|
||||
@@ -251,6 +272,7 @@ abstract class DashboardViewModelBase with Store {
|
||||
name = wallet.name;
|
||||
isOutdatedElectrumWallet =
|
||||
wallet.type == WalletType.bitcoin && wallet.seed.split(' ').length < 24;
|
||||
updateActions();
|
||||
|
||||
if (wallet.type == WalletType.monero) {
|
||||
subname = monero.getCurrentAccount(wallet)?.label;
|
||||
@@ -313,4 +335,16 @@ abstract class DashboardViewModelBase with Store {
|
||||
balanceViewModel: balanceViewModel,
|
||||
settingsStore: appStore.settingsStore)));
|
||||
}
|
||||
|
||||
void updateActions() {
|
||||
isEnabledExchangeAction = wallet.type != WalletType.haven;
|
||||
hasExchangeAction = !isHaven;
|
||||
isEnabledBuyAction = wallet.type != WalletType.haven
|
||||
&& wallet.type != WalletType.monero;
|
||||
hasBuyAction = !isMoneroOnly && !isHaven;
|
||||
isEnabledSellAction = wallet.type != WalletType.haven
|
||||
&& wallet.type != WalletType.monero
|
||||
&& wallet.type != WalletType.litecoin;
|
||||
hasSellAction = !isMoneroOnly && !isHaven;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
import 'package:cake_wallet/entities/balance_display_mode.dart';
|
||||
import 'package:cake_wallet/entities/fiat_currency.dart';
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
import 'package:cw_core/transaction_info.dart';
|
||||
import 'package:cake_wallet/store/settings_store.dart';
|
||||
import 'package:cake_wallet/utils/mobx.dart';
|
||||
import 'package:cake_wallet/view_model/dashboard/action_list_item.dart';
|
||||
import 'package:cake_wallet/monero/monero.dart';
|
||||
import 'package:cake_wallet/haven/haven.dart';
|
||||
import 'package:cake_wallet/bitcoin/bitcoin.dart';
|
||||
import 'package:cake_wallet/entities/calculate_fiat_amount_raw.dart';
|
||||
import 'package:cake_wallet/view_model/dashboard/balance_view_model.dart';
|
||||
import 'package:cw_core/keyable.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
import 'package:cw_haven/haven_transaction_info.dart';
|
||||
|
||||
class TransactionListItem extends ActionListItem with Keyable {
|
||||
TransactionListItem(
|
||||
@@ -35,21 +38,33 @@ class TransactionListItem extends ActionListItem with Keyable {
|
||||
}
|
||||
|
||||
String get formattedFiatAmount {
|
||||
if (balanceViewModel.wallet.type == WalletType.monero) {
|
||||
final amount = calculateFiatAmountRaw(
|
||||
var amount = '';
|
||||
|
||||
switch(balanceViewModel.wallet.type) {
|
||||
case WalletType.monero:
|
||||
amount = calculateFiatAmountRaw(
|
||||
cryptoAmount: monero.formatterMoneroAmountToDouble(amount: transaction.amount),
|
||||
price: price);
|
||||
transaction.changeFiatAmount(amount);
|
||||
}
|
||||
|
||||
if (balanceViewModel.wallet.type == WalletType.bitcoin
|
||||
|| balanceViewModel.wallet.type == WalletType.litecoin) {
|
||||
final amount = calculateFiatAmountRaw(
|
||||
break;
|
||||
case WalletType.bitcoin:
|
||||
case WalletType.litecoin:
|
||||
amount = calculateFiatAmountRaw(
|
||||
cryptoAmount: bitcoin.formatterBitcoinAmountToDouble(amount: transaction.amount),
|
||||
price: price);
|
||||
transaction.changeFiatAmount(amount);
|
||||
break;
|
||||
case WalletType.haven:
|
||||
final tx = transaction as HavenTransactionInfo;
|
||||
final asset = CryptoCurrency.fromString(tx.assetType);
|
||||
final price = balanceViewModel.fiatConvertationStore.prices[asset];
|
||||
amount = calculateFiatAmountRaw(
|
||||
cryptoAmount: haven.formatterMoneroAmountToDouble(amount: transaction.amount),
|
||||
price: price);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
transaction.changeFiatAmount(amount);
|
||||
return displayMode == BalanceDisplayMode.hiddenBalance
|
||||
? '---'
|
||||
: fiatCurrency.title + ' ' + transaction.fiatAmount();
|
||||
|
||||
Reference in New Issue
Block a user