diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index 789c7a20..b7bfc2fa 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -104,7 +104,7 @@ public class AppController implements Initializable { private static Map targetBlockFeeRates; - private static Map fiatCurrencyExchangeRate; + private static CurrencyRate fiatCurrencyExchangeRate; @Override public void initialize(URL location, ResourceBundle resources) { @@ -397,7 +397,7 @@ public class AppController implements Initializable { return targetBlockFeeRates; } - public static Map getFiatCurrencyExchangeRate() { + public static CurrencyRate getFiatCurrencyExchangeRate() { return fiatCurrencyExchangeRate; } @@ -879,6 +879,6 @@ public class AppController implements Initializable { @Subscribe public void exchangeRatesUpdated(ExchangeRatesUpdatedEvent event) { - fiatCurrencyExchangeRate = Map.of(event.getSelectedCurrency(), event.getRate()); + fiatCurrencyExchangeRate = event.getCurrencyRate(); } } \ No newline at end of file diff --git a/src/main/java/com/sparrowwallet/sparrow/CurrencyRate.java b/src/main/java/com/sparrowwallet/sparrow/CurrencyRate.java new file mode 100644 index 00000000..96ad63cd --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/CurrencyRate.java @@ -0,0 +1,25 @@ +package com.sparrowwallet.sparrow; + +import java.util.Currency; + +public class CurrencyRate { + private final Currency currency; + private final Double btcRate; + + public CurrencyRate(Currency currency, Double btcRate) { + this.currency = currency; + this.btcRate = btcRate; + } + + public Currency getCurrency() { + return currency; + } + + public boolean isAvailable() { + return btcRate != null && btcRate > 0.0; + } + + public Double getBtcRate() { + return btcRate; + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/control/FiatLabel.java b/src/main/java/com/sparrowwallet/sparrow/control/FiatLabel.java index 8f5971c1..dc070815 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/FiatLabel.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/FiatLabel.java @@ -1,6 +1,7 @@ package com.sparrowwallet.sparrow.control; import com.sparrowwallet.drongo.protocol.Transaction; +import com.sparrowwallet.sparrow.CurrencyRate; import javafx.beans.property.*; import javafx.scene.control.ContextMenu; import javafx.scene.control.MenuItem; @@ -70,6 +71,10 @@ public class FiatLabel extends CopyableLabel { this.currencyProperty.set(currency); } + public final void set(CurrencyRate currencyRate, long value) { + set(currencyRate.getCurrency(), currencyRate.getBtcRate(), value); + } + public final void set(Currency currency, double btcRate, long value) { setValue(value); setBtcRate(btcRate); diff --git a/src/main/java/com/sparrowwallet/sparrow/event/ExchangeRatesUpdatedEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/ExchangeRatesUpdatedEvent.java index 33174dc5..a6005efb 100644 --- a/src/main/java/com/sparrowwallet/sparrow/event/ExchangeRatesUpdatedEvent.java +++ b/src/main/java/com/sparrowwallet/sparrow/event/ExchangeRatesUpdatedEvent.java @@ -1,21 +1,27 @@ package com.sparrowwallet.sparrow.event; +import com.sparrowwallet.sparrow.CurrencyRate; + import java.util.Currency; public class ExchangeRatesUpdatedEvent { - private final Currency selectedCurrency; - private final Double rate; + private final Currency currency; + private final Double btcRate; - public ExchangeRatesUpdatedEvent(Currency selectedCurrency, Double rate) { - this.selectedCurrency = selectedCurrency; - this.rate = rate; + public ExchangeRatesUpdatedEvent(Currency currency, Double btcRate) { + this.currency = currency; + this.btcRate = btcRate; } - public Currency getSelectedCurrency() { - return selectedCurrency; + public Currency getCurrency() { + return currency; } - public Double getRate() { - return rate; + public Double getBtcRate() { + return btcRate; + } + + public CurrencyRate getCurrencyRate() { + return new CurrencyRate(currency, btcRate); } } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionsController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionsController.java index f705f1c9..9fec9c1a 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/TransactionsController.java @@ -2,6 +2,7 @@ package com.sparrowwallet.sparrow.wallet; import com.google.common.eventbus.Subscribe; import com.sparrowwallet.sparrow.AppController; +import com.sparrowwallet.sparrow.CurrencyRate; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.control.BalanceChart; import com.sparrowwallet.sparrow.control.CoinLabel; @@ -16,8 +17,6 @@ import javafx.scene.control.TreeItem; import javafx.scene.input.MouseEvent; import java.net.URL; -import java.util.Currency; -import java.util.Map; import java.util.ResourceBundle; public class TransactionsController extends WalletFormController implements Initializable { @@ -61,13 +60,9 @@ public class TransactionsController extends WalletFormController implements Init }); } - private void setFiatBalance(Map fiatCurrencyExchangeRate, long balance) { - if(fiatCurrencyExchangeRate != null && !fiatCurrencyExchangeRate.isEmpty()) { - Currency currency = fiatCurrencyExchangeRate.keySet().iterator().next(); - Double rate = fiatCurrencyExchangeRate.get(currency); - if(rate != null) { - fiatBalance.set(currency, rate, balance); - } + private void setFiatBalance(CurrencyRate currencyRate, long balance) { + if(currencyRate != null && currencyRate.isAvailable()) { + fiatBalance.set(currencyRate, balance); } } @@ -124,8 +119,7 @@ public class TransactionsController extends WalletFormController implements Init @Subscribe public void exchangeRatesUpdated(ExchangeRatesUpdatedEvent event) { - Map fiatRate = Map.of(event.getSelectedCurrency(), event.getRate()); - setFiatBalance(fiatRate, getWalletForm().getWalletTransactionsEntry().getBalance()); + setFiatBalance(event.getCurrencyRate(), getWalletForm().getWalletTransactionsEntry().getBalance()); } //TODO: Remove