From 5e31cdb7ac7639849235ef0b069be624616afa60 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Sat, 26 Mar 2022 14:15:00 +0200 Subject: [PATCH] update utxo fiat balances as rates change --- .../sparrow/wallet/UtxosController.java | 21 +++++++++++++++++-- .../sparrow/wallet/WalletUtxosEntry.java | 8 +++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/UtxosController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/UtxosController.java index 02c5dcbc..bac139a7 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/UtxosController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/UtxosController.java @@ -18,6 +18,7 @@ import com.sparrowwallet.sparrow.event.*; import com.sparrowwallet.sparrow.glyphfont.FontAwesome5; import com.sparrowwallet.sparrow.io.Config; import com.sparrowwallet.sparrow.io.Storage; +import com.sparrowwallet.sparrow.net.ExchangeSource; import com.sparrowwallet.sparrow.whirlpool.Whirlpool; import com.sparrowwallet.sparrow.whirlpool.WhirlpoolDialog; import com.sparrowwallet.sparrow.whirlpool.WhirlpoolServices; @@ -186,8 +187,8 @@ public class UtxosController extends WalletFormController implements Initializab } private void updateFields(WalletUtxosEntry walletUtxosEntry) { - balance.setValue(walletUtxosEntry.getChildren().stream().mapToLong(Entry::getValue).sum()); - mempoolBalance.setValue(walletUtxosEntry.getChildren().stream().filter(entry -> ((UtxoEntry)entry).getHashIndex().getHeight() <= 0).mapToLong(Entry::getValue).sum()); + balance.setValue(walletUtxosEntry.getBalance()); + mempoolBalance.setValue(walletUtxosEntry.getMempoolBalance()); utxoCount.setText(walletUtxosEntry.getChildren() != null ? Integer.toString(walletUtxosEntry.getChildren().size()) : "0"); } @@ -612,4 +613,20 @@ public class UtxosController extends WalletFormController implements Initializab selectEntry(utxosTable, utxosTable.getRoot(), event.getEntry()); } } + + @Subscribe + public void fiatCurrencySelected(FiatCurrencySelectedEvent event) { + if(event.getExchangeSource() == ExchangeSource.NONE) { + fiatBalance.setCurrency(null); + fiatBalance.setBtcRate(0.0); + fiatMempoolBalance.setCurrency(null); + fiatMempoolBalance.setBtcRate(0.0); + } + } + + @Subscribe + public void exchangeRatesUpdated(ExchangeRatesUpdatedEvent event) { + setFiatBalance(fiatBalance, event.getCurrencyRate(), getWalletForm().getWalletUtxosEntry().getBalance()); + setFiatBalance(fiatMempoolBalance, event.getCurrencyRate(), getWalletForm().getWalletUtxosEntry().getMempoolBalance()); + } } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletUtxosEntry.java b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletUtxosEntry.java index 6555a6ea..f4ad539a 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletUtxosEntry.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletUtxosEntry.java @@ -76,4 +76,12 @@ public class WalletUtxosEntry extends Entry { calculateDuplicates(); updateMixProgress(); } + + public long getBalance() { + return getChildren().stream().mapToLong(Entry::getValue).sum(); + } + + public long getMempoolBalance() { + return getChildren().stream().filter(entry -> ((UtxoEntry)entry).getHashIndex().getHeight() <= 0).mapToLong(Entry::getValue).sum(); + } }