Skip to content

Commit

Permalink
Use async methods with CompletableFuture in viewmodel to
Browse files Browse the repository at this point in the history
avoid that slow operations in data model are running on UI thread
  • Loading branch information
chimp1984 committed Nov 2, 2021
1 parent f6043e3 commit c7dd9a0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import bisq.core.locale.Res;

import bisq.common.UserThread;

import javax.inject.Inject;

import javafx.scene.chart.XYChart;
Expand Down Expand Up @@ -124,14 +126,13 @@ protected void defineAndAddActiveSeries() {
@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();
}
}

Expand All @@ -143,16 +144,43 @@ protected void activateSeries(XYChart.Series<Number, Number> series) {
@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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.text.DecimalFormat;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

import lombok.extern.slf4j.Slf4j;
Expand All @@ -53,29 +54,30 @@ public PriceChartViewModel(PriceChartDataModel dataModel) {
// 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());
}


///////////////////////////////////////////////////////////////////////////////////////////
// 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()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import bisq.core.locale.Res;

import bisq.common.UserThread;

import javax.inject.Inject;

import javafx.scene.chart.XYChart;
Expand Down Expand Up @@ -122,9 +124,9 @@ protected void activateSeries(XYChart.Series<Number, Number> series) {
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();
}
}

Expand All @@ -136,13 +138,33 @@ protected void activateSeries(XYChart.Series<Number, Number> series) {
@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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.text.DecimalFormat;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

import lombok.extern.slf4j.Slf4j;
Expand All @@ -54,25 +55,25 @@ public VolumeChartViewModel(VolumeChartDataModel dataModel) {
// 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);
}


///////////////////////////////////////////////////////////////////////////////////////////
// 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()));
}


Expand Down

0 comments on commit c7dd9a0

Please sign in to comment.