Use async methods with CompletableFuture in viewmodel to

avoid that slow operations in data model are running on UI thread
This commit is contained in:
chimp1984 2021-11-02 02:54:53 +01:00
parent 07f4074d0c
commit 6b89bf0da9
No known key found for this signature in database
GPG Key ID: 9801B4EC591F90E3
4 changed files with 86 additions and 33 deletions

View File

@ -21,6 +21,8 @@ import bisq.desktop.components.chart.ChartView;
import bisq.core.locale.Res;
import bisq.common.UserThread;
import javax.inject.Inject;
import javafx.scene.chart.XYChart;
@ -124,14 +126,13 @@ public class PriceChartView extends ChartView<PriceChartViewModel> {
@Override
protected void activateSeries(XYChart.Series<Number, Number> series) {
super.activateSeries(series);
String seriesId = getSeriesId(series);
if (seriesId.equals(getSeriesId(seriesBsqUsdPrice))) {
seriesBsqUsdPrice.getData().setAll(model.getBsqUsdPriceChartData());
applyBsqUsdPriceChartDataAsync();
} else if (seriesId.equals(getSeriesId(seriesBsqBtcPrice))) {
seriesBsqBtcPrice.getData().setAll(model.getBsqBtcPriceChartData());
applyBsqBtcPriceChartData();
} else if (seriesId.equals(getSeriesId(seriesBtcUsdPrice))) {
seriesBtcUsdPrice.getData().setAll(model.getBtcUsdPriceChartData());
applyBtcUsdPriceChartData();
}
}
@ -143,16 +144,43 @@ public class PriceChartView extends ChartView<PriceChartViewModel> {
@Override
protected void applyData() {
if (activeSeries.contains(seriesBsqUsdPrice)) {
seriesBsqUsdPrice.getData().setAll(model.getBsqUsdPriceChartData());
applyBsqUsdPriceChartDataAsync();
}
if (activeSeries.contains(seriesBsqBtcPrice)) {
seriesBsqBtcPrice.getData().setAll(model.getBsqBtcPriceChartData());
applyBsqBtcPriceChartData();
}
if (activeSeries.contains(seriesBtcUsdPrice)) {
seriesBtcUsdPrice.getData().setAll(model.getBtcUsdPriceChartData());
applyBtcUsdPriceChartData();
}
averageBsqBtcPriceProperty.set(model.averageBsqBtcPrice());
averageBsqUsdPriceProperty.set(model.averageBsqUsdPrice());
model.averageBsqBtcPrice()
.whenComplete((data, t) ->
UserThread.execute(() ->
averageBsqBtcPriceProperty.set(data)));
model.averageBsqUsdPrice()
.whenComplete((data, t) ->
UserThread.execute(() ->
averageBsqUsdPriceProperty.set(data)));
}
private void applyBsqUsdPriceChartDataAsync() {
model.getBsqUsdPriceChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
seriesBsqUsdPrice.getData().setAll(data)));
}
private void applyBtcUsdPriceChartData() {
model.getBtcUsdPriceChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
seriesBtcUsdPrice.getData().setAll(data)));
}
private void applyBsqBtcPriceChartData() {
model.getBsqBtcPriceChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
seriesBsqBtcPrice.getData().setAll(data)));
}
}

View File

@ -32,6 +32,7 @@ import javafx.util.StringConverter;
import java.text.DecimalFormat;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import lombok.extern.slf4j.Slf4j;
@ -53,12 +54,12 @@ public class PriceChartViewModel extends ChartViewModel<PriceChartDataModel> {
// Average price from timeline selection
///////////////////////////////////////////////////////////////////////////////////////////
double averageBsqUsdPrice() {
return dataModel.averageBsqUsdPrice();
CompletableFuture<Double> averageBsqUsdPrice() {
return CompletableFuture.supplyAsync(() -> dataModel.averageBsqUsdPrice());
}
double averageBsqBtcPrice() {
return dataModel.averageBsqBtcPrice();
CompletableFuture<Double> averageBsqBtcPrice() {
return CompletableFuture.supplyAsync(() -> dataModel.averageBsqBtcPrice());
}
@ -66,16 +67,17 @@ public class PriceChartViewModel extends ChartViewModel<PriceChartDataModel> {
// Chart data
///////////////////////////////////////////////////////////////////////////////////////////
List<XYChart.Data<Number, Number>> getBsqUsdPriceChartData() {
return toChartDoubleData(dataModel.getBsqUsdPriceByInterval());
CompletableFuture<List<XYChart.Data<Number, Number>>> getBsqUsdPriceChartData() {
return CompletableFuture.supplyAsync(() -> toChartDoubleData(dataModel.getBsqUsdPriceByInterval()));
}
List<XYChart.Data<Number, Number>> getBsqBtcPriceChartData() {
return toChartDoubleData(dataModel.getBsqBtcPriceByInterval());
CompletableFuture<List<XYChart.Data<Number, Number>>> getBsqBtcPriceChartData() {
return CompletableFuture.supplyAsync(() -> toChartDoubleData(dataModel.getBsqBtcPriceByInterval()));
}
List<XYChart.Data<Number, Number>> getBtcUsdPriceChartData() {
return toChartDoubleData(dataModel.getBtcUsdPriceByInterval());
CompletableFuture<List<XYChart.Data<Number, Number>>> getBtcUsdPriceChartData() {
return CompletableFuture.supplyAsync(() -> toChartDoubleData(dataModel.getBtcUsdPriceByInterval()));
}

View File

@ -21,6 +21,8 @@ import bisq.desktop.components.chart.ChartView;
import bisq.core.locale.Res;
import bisq.common.UserThread;
import javax.inject.Inject;
import javafx.scene.chart.XYChart;
@ -122,9 +124,9 @@ public class VolumeChartView extends ChartView<VolumeChartViewModel> {
super.activateSeries(series);
if (getSeriesId(series).equals(getSeriesId(seriesUsdVolume))) {
seriesUsdVolume.getData().setAll(model.getUsdVolumeChartData());
applyUsdVolumeChartData();
} else if (getSeriesId(series).equals(getSeriesId(seriesBtcVolume))) {
seriesBtcVolume.getData().setAll(model.getBtcVolumeChartData());
applyBtcVolumeChartData();
}
}
@ -136,13 +138,33 @@ public class VolumeChartView extends ChartView<VolumeChartViewModel> {
@Override
protected void applyData() {
if (activeSeries.contains(seriesUsdVolume)) {
seriesUsdVolume.getData().setAll(model.getUsdVolumeChartData());
applyUsdVolumeChartData();
}
if (activeSeries.contains(seriesBtcVolume)) {
seriesBtcVolume.getData().setAll(model.getBtcVolumeChartData());
applyBtcVolumeChartData();
}
usdVolumeProperty.set(model.getUsdVolume());
btcVolumeProperty.set(model.getBtcVolume());
model.getUsdVolume()
.whenComplete((data, t) ->
UserThread.execute(() ->
usdVolumeProperty.set(data)));
model.getBtcVolume()
.whenComplete((data, t) ->
UserThread.execute(() ->
btcVolumeProperty.set(data)));
}
private void applyBtcVolumeChartData() {
model.getBtcVolumeChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
seriesBtcVolume.getData().setAll(data)));
}
private void applyUsdVolumeChartData() {
model.getUsdVolumeChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
seriesUsdVolume.getData().setAll(data)));
}
}

View File

@ -32,6 +32,7 @@ import javafx.util.StringConverter;
import java.text.DecimalFormat;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import lombok.extern.slf4j.Slf4j;
@ -54,12 +55,12 @@ public class VolumeChartViewModel extends ChartViewModel<VolumeChartDataModel> {
// Total amounts
///////////////////////////////////////////////////////////////////////////////////////////
long getUsdVolume() {
return dataModel.getUsdVolume();
CompletableFuture<Long> getUsdVolume() {
return CompletableFuture.supplyAsync(dataModel::getUsdVolume);
}
long getBtcVolume() {
return dataModel.getBtcVolume();
CompletableFuture<Long> getBtcVolume() {
return CompletableFuture.supplyAsync(dataModel::getBtcVolume);
}
@ -67,12 +68,12 @@ public class VolumeChartViewModel extends ChartViewModel<VolumeChartDataModel> {
// Chart data
///////////////////////////////////////////////////////////////////////////////////////////
List<XYChart.Data<Number, Number>> getUsdVolumeChartData() {
return toChartLongData(dataModel.getUsdVolumeByInterval());
CompletableFuture<List<XYChart.Data<Number, Number>>> getUsdVolumeChartData() {
return CompletableFuture.supplyAsync(() -> toChartLongData(dataModel.getUsdVolumeByInterval()));
}
List<XYChart.Data<Number, Number>> getBtcVolumeChartData() {
return toChartLongData(dataModel.getBtcVolumeByInterval());
CompletableFuture<List<XYChart.Data<Number, Number>>> getBtcVolumeChartData() {
return CompletableFuture.supplyAsync(() -> toChartLongData(dataModel.getBtcVolumeByInterval()));
}