From b047d10a509471185e4e90a49dc1d2d75ef34b3b Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Thu, 16 Jun 2016 17:30:36 +0200 Subject: [PATCH] Only call updateChartData if currency matches, refactoring --- .../markets/charts/MarketsChartsView.java | 10 ++--- .../charts/MarketsChartsViewModel.java | 43 +++++++++++++------ 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/gui/src/main/java/io/bitsquare/gui/main/markets/charts/MarketsChartsView.java b/gui/src/main/java/io/bitsquare/gui/main/markets/charts/MarketsChartsView.java index 095b83026f..97d7824278 100644 --- a/gui/src/main/java/io/bitsquare/gui/main/markets/charts/MarketsChartsView.java +++ b/gui/src/main/java/io/bitsquare/gui/main/markets/charts/MarketsChartsView.java @@ -25,7 +25,6 @@ import io.bitsquare.gui.common.view.FxmlView; import io.bitsquare.gui.main.MainView; import io.bitsquare.gui.main.offer.BuyOfferView; import io.bitsquare.gui.main.offer.SellOfferView; -import io.bitsquare.gui.main.offer.offerbook.OfferBookListItem; import io.bitsquare.gui.util.BSFormatter; import io.bitsquare.locale.*; import io.bitsquare.trade.offer.Offer; @@ -34,7 +33,6 @@ import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; -import javafx.collections.ListChangeListener; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.chart.AreaChart; @@ -56,7 +54,7 @@ public class MarketsChartsView extends ActivatableViewAndModel changeListener; + private final ChangeListener updateChartDataFlagListener; private final Navigation navigation; private final BSFormatter formatter; private TableView buyOfferTableView; @@ -80,7 +78,7 @@ public class MarketsChartsView extends ActivatableViewAndModel updateChartData(); + updateChartDataFlagListener = (ov, o, n) -> updateChartData(); } @Override @@ -141,7 +139,7 @@ public class MarketsChartsView extends ActivatableViewAndModel { String code = tradeCurrency.getCode(); @@ -169,7 +167,7 @@ public class MarketsChartsView extends ActivatableViewAndModel listChangeListener; private final ObservableList top3BuyOfferList = FXCollections.observableArrayList(); private final ObservableList top3SellOfferList = FXCollections.observableArrayList(); - private final ChangeListener cacheFilledListener; - + private final ChangeListener currenciesUpdatedListener; + final IntegerProperty updateChartDataFlag = new SimpleIntegerProperty(0); /////////////////////////////////////////////////////////////////////////////////////////// // Constructor, lifecycle @@ -69,15 +71,27 @@ class MarketsChartsViewModel extends ActivatableViewModel { this.priceFeed = priceFeed; offerBookListItems = offerBook.getOfferBookListItems(); - listChangeListener = c -> updateChartData(offerBookListItems); + listChangeListener = c -> { + c.next(); + if (c.wasAdded() || c.wasRemoved()) { + ArrayList list = new ArrayList<>(c.getRemoved()); + list.addAll(c.getAddedSubList()); + if (list.stream() + .map(OfferBookListItem::getOffer) + .filter(e -> e.getCurrencyCode().equals(tradeCurrency.get().getCode())) + .findAny() + .isPresent()) + updateChartData(); + } + }; - cacheFilledListener = new ChangeListener() { + currenciesUpdatedListener = new ChangeListener() { @Override public void changed(ObservableValue observable, Number oldValue, Number newValue) { - if (!offerBookListItems.stream().filter(item -> item.getOffer().getPrice() == null).findAny().isPresent()) { + if (!isAnyPricePresent()) { offerBook.fillOfferBookListItems(); - updateChartData(offerBookListItems); - priceFeed.currenciesUpdateFlagProperty().removeListener(cacheFilledListener); + updateChartData(); + priceFeed.currenciesUpdateFlagProperty().removeListener(currenciesUpdatedListener); } } }; @@ -89,10 +103,10 @@ class MarketsChartsViewModel extends ActivatableViewModel { offerBookListItems.addListener(listChangeListener); offerBook.fillOfferBookListItems(); - updateChartData(offerBookListItems); + updateChartData(); - if (offerBookListItems.stream().filter(item -> item.getOffer().getPrice() == null).findAny().isPresent()) - priceFeed.currenciesUpdateFlagProperty().addListener(cacheFilledListener); + if (isAnyPricePresent()) + priceFeed.currenciesUpdateFlagProperty().addListener(currenciesUpdatedListener); if (!preferences.getUseStickyMarketPrice()) priceFeed.setCurrencyCode(tradeCurrency.get().getCode()); @@ -103,7 +117,12 @@ class MarketsChartsViewModel extends ActivatableViewModel { offerBookListItems.removeListener(listChangeListener); } - private void updateChartData(ObservableList offerBookListItems) { + private boolean isAnyPricePresent() { + return offerBookListItems.stream().filter(item -> item.getOffer().getPrice() == null).findAny().isPresent(); + } + + private void updateChartData() { + updateChartDataFlag.set(updateChartDataFlag.get() + 1); List allBuyOffers = offerBookListItems.stream() .map(OfferBookListItem::getOffer) .filter(e -> e.getCurrencyCode().equals(tradeCurrency.get().getCode()) @@ -159,7 +178,7 @@ class MarketsChartsViewModel extends ActivatableViewModel { public void onSetTradeCurrency(TradeCurrency tradeCurrency) { this.tradeCurrency.set(tradeCurrency); - updateChartData(offerBookListItems); + updateChartData(); if (!preferences.getUseStickyMarketPrice()) priceFeed.setCurrencyCode(tradeCurrency.getCode());