Add mapToUserThread method to ChartView

For initial rendering we need to delay a bit and wait
until async calls are completed.
This commit is contained in:
chimp1984 2021-11-02 03:29:49 +01:00
parent a4b106cd4a
commit b61d64e1ea
No known key found for this signature in database
GPG key ID: 9801B4EC591F90E3
4 changed files with 51 additions and 39 deletions

View file

@ -107,8 +107,9 @@ public abstract class ChartView<T extends ChartViewModel<? extends ChartDataMode
private int maxDataPointsForShowingSymbols = 100;
private ChangeListener<Number> yAxisWidthListener;
private EventHandler<MouseEvent> dividerMouseDraggedEventHandler;
private StringProperty fromProperty = new SimpleStringProperty();
private StringProperty toProperty = new SimpleStringProperty();
private final StringProperty fromProperty = new SimpleStringProperty();
private final StringProperty toProperty = new SimpleStringProperty();
private boolean dataApplied;
///////////////////////////////////////////////////////////////////////////////////////////
@ -219,18 +220,16 @@ public abstract class ChartView<T extends ChartViewModel<? extends ChartDataMode
@Override
public void activate() {
timelineNavigation.setDividerPositions(model.getDividerPositions()[0], model.getDividerPositions()[1]);
UserThread.execute(this::applyTimeLineNavigationLabels);
UserThread.execute(this::onTimelineChanged);
TemporalAdjuster temporalAdjuster = model.getTemporalAdjuster();
applyTemporalAdjuster(temporalAdjuster);
findTimeIntervalToggleByTemporalAdjuster(temporalAdjuster).ifPresent(timeIntervalToggleGroup::selectToggle);
defineAndAddActiveSeries();
applyData();
applyData(); //todo
initBoundsForTimelineNavigation();
updateChartAfterDataChange();
updateChartAfterDataChange(); //todo
// Apply listeners and handlers
root.widthProperty().addListener(widthListener);
@ -771,4 +770,23 @@ public abstract class ChartView<T extends ChartViewModel<? extends ChartDataMode
protected String getSeriesId(XYChart.Series<Number, Number> series) {
return series.getName();
}
protected void mapToUserThread(Runnable command) {
UserThread.execute(() -> {
command.run();
onDataApplied();
});
}
// For the async handling we need to wait until we get the data applied and then still delay a bit otherwise
// the UI does not get rendered at first start
protected void onDataApplied() {
if (!dataApplied) {
dataApplied = true;
UserThread.execute(() -> {
applyTimeLineNavigationLabels();
onTimelineChanged();
});
}
}
}

View file

@ -21,8 +21,6 @@ import bisq.desktop.components.chart.ChartView;
import bisq.core.locale.Res;
import bisq.common.UserThread;
import javax.inject.Inject;
import javafx.scene.chart.XYChart;
@ -39,8 +37,8 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class PriceChartView extends ChartView<PriceChartViewModel> {
private XYChart.Series<Number, Number> seriesBsqUsdPrice, seriesBsqBtcPrice, seriesBtcUsdPrice;
private DoubleProperty averageBsqUsdPriceProperty = new SimpleDoubleProperty();
private DoubleProperty averageBsqBtcPriceProperty = new SimpleDoubleProperty();
private final DoubleProperty averageBsqUsdPriceProperty = new SimpleDoubleProperty();
private final DoubleProperty averageBsqBtcPriceProperty = new SimpleDoubleProperty();
@Inject
public PriceChartView(PriceChartViewModel model) {
@ -155,32 +153,32 @@ public class PriceChartView extends ChartView<PriceChartViewModel> {
model.averageBsqBtcPrice()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
averageBsqBtcPriceProperty.set(data)));
model.averageBsqUsdPrice()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
averageBsqUsdPriceProperty.set(data)));
}
private void applyBsqUsdPriceChartDataAsync() {
model.getBsqUsdPriceChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
seriesBsqUsdPrice.getData().setAll(data)));
}
private void applyBtcUsdPriceChartData() {
model.getBtcUsdPriceChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
seriesBtcUsdPrice.getData().setAll(data)));
}
private void applyBsqBtcPriceChartData() {
model.getBsqBtcPriceChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
seriesBsqBtcPrice.getData().setAll(data)));
}
}

View file

@ -21,8 +21,6 @@ import bisq.desktop.components.chart.ChartView;
import bisq.core.locale.Res;
import bisq.common.UserThread;
import javax.inject.Inject;
import javafx.scene.chart.XYChart;
@ -40,8 +38,8 @@ import lombok.extern.slf4j.Slf4j;
public class VolumeChartView extends ChartView<VolumeChartViewModel> {
private XYChart.Series<Number, Number> seriesUsdVolume, seriesBtcVolume;
private LongProperty usdVolumeProperty = new SimpleLongProperty();
private LongProperty btcVolumeProperty = new SimpleLongProperty();
private final LongProperty usdVolumeProperty = new SimpleLongProperty();
private final LongProperty btcVolumeProperty = new SimpleLongProperty();
@Inject
public VolumeChartView(VolumeChartViewModel model) {
@ -146,25 +144,25 @@ public class VolumeChartView extends ChartView<VolumeChartViewModel> {
model.getUsdVolume()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
usdVolumeProperty.set(data)));
model.getBtcVolume()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
btcVolumeProperty.set(data)));
}
private void applyBtcVolumeChartData() {
model.getBtcVolumeChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
seriesBtcVolume.getData().setAll(data)));
}
private void applyUsdVolumeChartData() {
model.getUsdVolumeChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
seriesUsdVolume.getData().setAll(data)));
}
}

View file

@ -21,8 +21,6 @@ import bisq.desktop.components.chart.ChartView;
import bisq.core.locale.Res;
import bisq.common.UserThread;
import javax.inject.Inject;
import javafx.scene.chart.XYChart;
@ -38,10 +36,10 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class DaoChartView extends ChartView<DaoChartViewModel> {
private LongProperty compensationAmountProperty = new SimpleLongProperty();
private LongProperty reimbursementAmountProperty = new SimpleLongProperty();
private LongProperty bsqTradeFeeAmountProperty = new SimpleLongProperty();
private LongProperty proofOfBurnAmountProperty = new SimpleLongProperty();
private final LongProperty compensationAmountProperty = new SimpleLongProperty();
private final LongProperty reimbursementAmountProperty = new SimpleLongProperty();
private final LongProperty bsqTradeFeeAmountProperty = new SimpleLongProperty();
private final LongProperty proofOfBurnAmountProperty = new SimpleLongProperty();
private XYChart.Series<Number, Number> seriesBsqTradeFee, seriesProofOfBurn, seriesCompensation,
seriesReimbursement, seriesTotalIssued, seriesTotalBurned;
@ -183,61 +181,61 @@ public class DaoChartView extends ChartView<DaoChartViewModel> {
model.getCompensationAmount()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
compensationAmountProperty.set(data)));
model.getReimbursementAmount()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
reimbursementAmountProperty.set(data)));
model.getBsqTradeFeeAmount()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
bsqTradeFeeAmountProperty.set(data)));
model.getProofOfBurnAmount()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
proofOfBurnAmountProperty.set(data)));
}
private void applyTotalIssued() {
model.getTotalIssuedChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
seriesTotalIssued.getData().setAll(data)));
}
private void applyCompensation() {
model.getCompensationChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
seriesCompensation.getData().setAll(data)));
}
private void applyReimbursement() {
model.getReimbursementChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
seriesReimbursement.getData().setAll(data)));
}
private void applyTotalBurned() {
model.getTotalBurnedChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
seriesTotalBurned.getData().setAll(data)));
}
private void applyBsqTradeFee() {
model.getBsqTradeFeeChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
seriesBsqTradeFee.getData().setAll(data)));
}
private void applyProofOfBurn() {
model.getProofOfBurnChartData()
.whenComplete((data, t) ->
UserThread.execute(() ->
mapToUserThread(() ->
seriesProofOfBurn.getData().setAll(data)));
}
}