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<Integer, Double> targetBlockFeeRates;
private static Map<Currency, Double> fiatCurrencyExchangeRate; private static CurrencyRate fiatCurrencyExchangeRate;
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
@ -397,7 +397,7 @@ public class AppController implements Initializable {
return targetBlockFeeRates; return targetBlockFeeRates;
} }
public static Map<Currency, Double> getFiatCurrencyExchangeRate() { public static CurrencyRate getFiatCurrencyExchangeRate() {
return fiatCurrencyExchangeRate; return fiatCurrencyExchangeRate;
} }
@ -879,6 +879,6 @@ public class AppController implements Initializable {
@Subscribe @Subscribe
public void exchangeRatesUpdated(ExchangeRatesUpdatedEvent event) { 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; package com.sparrowwallet.sparrow.control;
import com.sparrowwallet.drongo.protocol.Transaction; import com.sparrowwallet.drongo.protocol.Transaction;
import com.sparrowwallet.sparrow.CurrencyRate;
import javafx.beans.property.*; import javafx.beans.property.*;
import javafx.scene.control.ContextMenu; import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem; import javafx.scene.control.MenuItem;
@ -70,6 +71,10 @@ public class FiatLabel extends CopyableLabel {
this.currencyProperty.set(currency); 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) { public final void set(Currency currency, double btcRate, long value) {
setValue(value); setValue(value);
setBtcRate(btcRate); setBtcRate(btcRate);

View file

@ -1,21 +1,27 @@
package com.sparrowwallet.sparrow.event; package com.sparrowwallet.sparrow.event;
import com.sparrowwallet.sparrow.CurrencyRate;
import java.util.Currency; import java.util.Currency;
public class ExchangeRatesUpdatedEvent { public class ExchangeRatesUpdatedEvent {
private final Currency selectedCurrency; private final Currency currency;
private final Double rate; private final Double btcRate;
public ExchangeRatesUpdatedEvent(Currency selectedCurrency, Double rate) { public ExchangeRatesUpdatedEvent(Currency currency, Double btcRate) {
this.selectedCurrency = selectedCurrency; this.currency = currency;
this.rate = rate; this.btcRate = btcRate;
} }
public Currency getSelectedCurrency() { public Currency getCurrency() {
return selectedCurrency; return currency;
} }
public Double getRate() { public Double getBtcRate() {
return rate; 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.google.common.eventbus.Subscribe;
import com.sparrowwallet.sparrow.AppController; import com.sparrowwallet.sparrow.AppController;
import com.sparrowwallet.sparrow.CurrencyRate;
import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.control.BalanceChart; import com.sparrowwallet.sparrow.control.BalanceChart;
import com.sparrowwallet.sparrow.control.CoinLabel; import com.sparrowwallet.sparrow.control.CoinLabel;
@ -16,8 +17,6 @@ import javafx.scene.control.TreeItem;
import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseEvent;
import java.net.URL; import java.net.URL;
import java.util.Currency;
import java.util.Map;
import java.util.ResourceBundle; import java.util.ResourceBundle;
public class TransactionsController extends WalletFormController implements Initializable { 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) { private void setFiatBalance(CurrencyRate currencyRate, long balance) {
if(fiatCurrencyExchangeRate != null && !fiatCurrencyExchangeRate.isEmpty()) { if(currencyRate != null && currencyRate.isAvailable()) {
Currency currency = fiatCurrencyExchangeRate.keySet().iterator().next(); fiatBalance.set(currencyRate, balance);
Double rate = fiatCurrencyExchangeRate.get(currency);
if(rate != null) {
fiatBalance.set(currency, rate, balance);
}
} }
} }
@ -124,8 +119,7 @@ public class TransactionsController extends WalletFormController implements Init
@Subscribe @Subscribe
public void exchangeRatesUpdated(ExchangeRatesUpdatedEvent event) { public void exchangeRatesUpdated(ExchangeRatesUpdatedEvent event) {
Map<Currency, Double> fiatRate = Map.of(event.getSelectedCurrency(), event.getRate()); setFiatBalance(event.getCurrencyRate(), getWalletForm().getWalletTransactionsEntry().getBalance());
setFiatBalance(fiatRate, getWalletForm().getWalletTransactionsEntry().getBalance());
} }
//TODO: Remove //TODO: Remove