Make buildUsdPricesPerTickUnit async using CompletableFuture

This commit is contained in:
chimp1984 2021-11-01 21:13:36 +01:00
parent deeb912f9b
commit ea66a510dc
No known key found for this signature in database
GPG Key ID: 9801B4EC591F90E3
2 changed files with 21 additions and 7 deletions

View File

@ -33,13 +33,14 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
public class ChartCalculations {
static final ZoneId ZONE_ID = ZoneId.systemDefault();
static void buildUsdPricesPerTickUnit(Map<TradesChartsViewModel.TickUnit, Map<Long, Long>> usdAveragePriceMapsPerTickUnit,
Set<TradeStatistics3> tradeStatisticsSet) {
if (usdAveragePriceMapsPerTickUnit.isEmpty()) {
static CompletableFuture<Map<TradesChartsViewModel.TickUnit, Map<Long, Long>>> buildUsdPricesPerTickUnit(Set<TradeStatistics3> tradeStatisticsSet) {
return CompletableFuture.supplyAsync(() -> {
Map<TradesChartsViewModel.TickUnit, Map<Long, Long>> usdAveragePriceMapsPerTickUnit = new HashMap<>();
Map<TradesChartsViewModel.TickUnit, Map<Long, List<TradeStatistics3>>> dateMapsPerTickUnit = new HashMap<>();
for (TradesChartsViewModel.TickUnit tick : TradesChartsViewModel.TickUnit.values()) {
dateMapsPerTickUnit.put(tick, new HashMap<>());
@ -61,7 +62,8 @@ public class ChartCalculations {
map.forEach((date, tradeStatisticsList) -> priceMap.put(date, getAveragePrice(tradeStatisticsList)));
usdAveragePriceMapsPerTickUnit.put(tick, priceMap);
});
}
return usdAveragePriceMapsPerTickUnit;
});
}

View File

@ -38,6 +38,7 @@ import bisq.core.trade.statistics.TradeStatistics3;
import bisq.core.trade.statistics.TradeStatisticsManager;
import bisq.core.user.Preferences;
import bisq.common.UserThread;
import bisq.common.util.MathUtils;
import org.bitcoinj.core.Coin;
@ -154,10 +155,21 @@ class TradesChartsViewModel extends ActivatableViewModel {
syncPriceFeedCurrency();
setMarketPriceFeedCurrency();
ChartCalculations.buildUsdPricesPerTickUnit(usdAveragePriceMapsPerTickUnit, tradeStatisticsManager.getObservableTradeStatisticsSet());
ChartCalculations.buildUsdPricesPerTickUnit(tradeStatisticsManager.getObservableTradeStatisticsSet())
.whenComplete((usdAveragePriceMapsPerTickUnit, throwable) -> {
if (throwable != null) {
log.error(throwable.toString());
return;
}
updateSelectedTradeStatistics(getCurrencyCode());
updateChartData();
UserThread.execute(() -> {
this.usdAveragePriceMapsPerTickUnit.clear();
this.usdAveragePriceMapsPerTickUnit.putAll(usdAveragePriceMapsPerTickUnit);
updateSelectedTradeStatistics(getCurrencyCode());
updateChartData();
});
});
}
@Override