Add "About Bitsquare" screen, add invert price icon

This commit is contained in:
Manfred Karrer 2016-04-08 01:00:11 +02:00
parent 3f5e1296b1
commit 625749534c
13 changed files with 263 additions and 79 deletions

View file

@ -8,6 +8,7 @@ import io.bitsquare.app.Log;
import io.bitsquare.btc.pricefeed.providers.BitcoinAveragePriceProvider;
import io.bitsquare.btc.pricefeed.providers.PoloniexPriceProvider;
import io.bitsquare.btc.pricefeed.providers.PriceProvider;
import io.bitsquare.common.Timer;
import io.bitsquare.common.UserThread;
import io.bitsquare.common.handlers.FaultHandler;
import io.bitsquare.locale.CurrencyUtil;
@ -24,6 +25,9 @@ import java.util.function.Consumer;
public class PriceFeed {
private static final Logger log = LoggerFactory.getLogger(PriceFeed.class);
private static final long MIN_PERIOD_BETWEEN_CALLS = 5000;
///////////////////////////////////////////////////////////////////////////////////////////
// Enum
///////////////////////////////////////////////////////////////////////////////////////////
@ -40,9 +44,8 @@ public class PriceFeed {
}
}
private static final long PERIOD_FIAT_SEC = 60;
private static final long PERIOD_FIAT_SEC = 2 * 60;
private static final long PERIOD_ALL_FIAT_SEC = 60 * 5;
private static final long PERIOD_CRYPTO_SEC = 60;
private static final long PERIOD_ALL_CRYPTO_SEC = 60 * 5;
private final Map<String, MarketPrice> cache = new HashMap<>();
@ -55,6 +58,11 @@ public class PriceFeed {
private final StringProperty currencyCodeProperty = new SimpleStringProperty();
private final ObjectProperty<Type> typeProperty = new SimpleObjectProperty<>();
private final IntegerProperty currenciesUpdateFlag = new SimpleIntegerProperty(0);
private long bitcoinAveragePriceProviderLastCallAllTs;
private long poloniexPriceProviderLastCallAllTs;
private long bitcoinAveragePriceProviderLastCallTs;
private Timer cryptoCurrenciesTime;
private Timer fiatCurrenciesTime;
///////////////////////////////////////////////////////////////////////////////////////////
@ -76,19 +84,18 @@ public class PriceFeed {
requestAllPrices(fiatPriceProvider, () -> {
applyPrice();
UserThread.runPeriodically(() -> requestPrice(fiatPriceProvider), PERIOD_FIAT_SEC);
if (fiatCurrenciesTime == null)
fiatCurrenciesTime = UserThread.runPeriodically(() -> requestPrice(fiatPriceProvider), PERIOD_FIAT_SEC);
});
requestAllPrices(cryptoCurrenciesPriceProvider, () -> {
applyPrice();
UserThread.runPeriodically(() -> requestAllPrices(cryptoCurrenciesPriceProvider, this::applyPrice),
PERIOD_CRYPTO_SEC);
if (cryptoCurrenciesTime == null)
cryptoCurrenciesTime = UserThread.runPeriodically(() -> requestAllPrices(cryptoCurrenciesPriceProvider, this::applyPrice),
PERIOD_ALL_CRYPTO_SEC);
});
UserThread.runPeriodically(() -> requestAllPrices(fiatPriceProvider, this::applyPrice), PERIOD_ALL_FIAT_SEC);
UserThread.runPeriodically(() -> requestAllPrices(cryptoCurrenciesPriceProvider, this::applyPrice), PERIOD_ALL_CRYPTO_SEC);
requestAllPrices(cryptoCurrenciesPriceProvider, this::applyPrice);
}
@Nullable
@ -110,14 +117,18 @@ public class PriceFeed {
}
public void setCurrencyCode(String currencyCode) {
this.currencyCode = currencyCode;
currencyCodeProperty.set(currencyCode);
applyPrice();
if (this.currencyCode != currencyCode) {
this.currencyCode = currencyCode;
currencyCodeProperty.set(currencyCode);
applyPrice();
if (CurrencyUtil.isFiatCurrency(currencyCode))
requestPrice(fiatPriceProvider);
else
requestPrice(cryptoCurrenciesPriceProvider);
if (CurrencyUtil.isFiatCurrency(currencyCode)) {
requestPrice(fiatPriceProvider);
} else {
// Poloniex does not support calls for one currency just for all which is quite a bit of data
requestAllPrices(cryptoCurrenciesPriceProvider, this::applyPrice);
}
}
}
@ -154,7 +165,6 @@ public class PriceFeed {
if (priceConsumer != null && currencyCode != null && type != null) {
if (cache.containsKey(currencyCode)) {
MarketPrice marketPrice = cache.get(currencyCode);
//log.debug("applyPrice type=" + type);
if (marketPrice != null)
priceConsumer.accept(marketPrice.getPrice(type));
} else {
@ -168,39 +178,66 @@ public class PriceFeed {
private void requestPrice(PriceProvider provider) {
Log.traceCall();
GetPriceRequest getPriceRequest = new GetPriceRequest();
SettableFuture<MarketPrice> future = getPriceRequest.requestPrice(currencyCode, provider);
Futures.addCallback(future, new FutureCallback<MarketPrice>() {
public void onSuccess(MarketPrice marketPrice) {
UserThread.execute(() -> {
cache.put(marketPrice.currencyCode, marketPrice);
//log.debug("marketPrice updated " + marketPrice);
priceConsumer.accept(marketPrice.getPrice(type));
});
}
long now = System.currentTimeMillis();
boolean allowed = false;
if (now - bitcoinAveragePriceProviderLastCallTs > MIN_PERIOD_BETWEEN_CALLS) {
bitcoinAveragePriceProviderLastCallTs = now;
allowed = true;
}
public void onFailure(@NotNull Throwable throwable) {
log.debug("Could not load marketPrice\n" + throwable.getMessage());
}
});
if (allowed) {
GetPriceRequest getPriceRequest = new GetPriceRequest();
SettableFuture<MarketPrice> future = getPriceRequest.requestPrice(currencyCode, provider);
Futures.addCallback(future, new FutureCallback<MarketPrice>() {
public void onSuccess(MarketPrice marketPrice) {
UserThread.execute(() -> {
cache.put(marketPrice.currencyCode, marketPrice);
priceConsumer.accept(marketPrice.getPrice(type));
});
}
public void onFailure(@NotNull Throwable throwable) {
log.debug("Could not load marketPrice\n" + throwable.getMessage());
}
});
} else {
log.debug("Ignore request. Too many attempt to call the API provider " + provider);
}
}
private void requestAllPrices(PriceProvider provider, @Nullable Runnable resultHandler) {
Log.traceCall();
GetPriceRequest getPriceRequest = new GetPriceRequest();
SettableFuture<Map<String, MarketPrice>> future = getPriceRequest.requestAllPrices(provider);
Futures.addCallback(future, new FutureCallback<Map<String, MarketPrice>>() {
public void onSuccess(Map<String, MarketPrice> marketPriceMap) {
UserThread.execute(() -> {
cache.putAll(marketPriceMap);
if (resultHandler != null)
resultHandler.run();
});
long now = System.currentTimeMillis();
boolean allowed = false;
if (provider instanceof BitcoinAveragePriceProvider) {
if (now - bitcoinAveragePriceProviderLastCallAllTs > MIN_PERIOD_BETWEEN_CALLS) {
bitcoinAveragePriceProviderLastCallAllTs = now;
allowed = true;
}
} else if (provider instanceof PoloniexPriceProvider) {
if (now - poloniexPriceProviderLastCallAllTs > MIN_PERIOD_BETWEEN_CALLS) {
poloniexPriceProviderLastCallAllTs = now;
allowed = true;
}
}
if (allowed) {
GetPriceRequest getPriceRequest = new GetPriceRequest();
SettableFuture<Map<String, MarketPrice>> future = getPriceRequest.requestAllPrices(provider);
Futures.addCallback(future, new FutureCallback<Map<String, MarketPrice>>() {
public void onSuccess(Map<String, MarketPrice> marketPriceMap) {
UserThread.execute(() -> {
cache.putAll(marketPriceMap);
if (resultHandler != null)
resultHandler.run();
});
}
public void onFailure(@NotNull Throwable throwable) {
UserThread.execute(() -> faultHandler.handleFault("Could not load marketPrices", throwable));
}
});
public void onFailure(@NotNull Throwable throwable) {
UserThread.execute(() -> faultHandler.handleFault("Could not load marketPrices", throwable));
}
});
} else {
log.debug("Ignore request. Too many attempt to call the API provider " + provider);
}
}
}