introduce currencyrate to hold fiat rate

This commit is contained in:
Craig Raw 2020-07-10 18:26:56 +02:00
parent c477d31d3d
commit d6218f2a58
5 changed files with 53 additions and 23 deletions

View file

@ -104,7 +104,7 @@ public class AppController implements Initializable {
private static Map<Integer, Double> targetBlockFeeRates;
private static Map<Currency, Double> 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<Currency, Double> 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();
}
}

View file

@ -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;
}
}

View file

@ -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);

View file

@ -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);
}
}

View file

@ -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<Currency, Double> 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<Currency, Double> fiatRate = Map.of(event.getSelectedCurrency(), event.getRate());
setFiatBalance(fiatRate, getWalletForm().getWalletTransactionsEntry().getBalance());
setFiatBalance(event.getCurrencyRate(), getWalletForm().getWalletTransactionsEntry().getBalance());
}
//TODO: Remove