Only call updateChartData if currency matches, refactoring

This commit is contained in:
Manfred Karrer 2016-06-16 17:30:36 +02:00
parent cb9a3a12ab
commit b047d10a50
2 changed files with 35 additions and 18 deletions

View File

@ -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<VBox, MarketsChar
private NumberAxis xAxis, yAxis;
XYChart.Series seriesBuy, seriesSell;
private final ListChangeListener<OfferBookListItem> changeListener;
private final ChangeListener<Number> updateChartDataFlagListener;
private final Navigation navigation;
private final BSFormatter formatter;
private TableView<Offer> buyOfferTableView;
@ -80,7 +78,7 @@ public class MarketsChartsView extends ActivatableViewAndModel<VBox, MarketsChar
this.navigation = navigation;
this.formatter = formatter;
changeListener = c -> updateChartData();
updateChartDataFlagListener = (ov, o, n) -> updateChartData();
}
@Override
@ -141,7 +139,7 @@ public class MarketsChartsView extends ActivatableViewAndModel<VBox, MarketsChar
updateChartData();
});
model.getOfferBookListItems().addListener(changeListener);
model.updateChartDataFlag.addListener(updateChartDataFlagListener);
tradeCurrencySubscriber = EasyBind.subscribe(model.tradeCurrency,
tradeCurrency -> {
String code = tradeCurrency.getCode();
@ -169,7 +167,7 @@ public class MarketsChartsView extends ActivatableViewAndModel<VBox, MarketsChar
@Override
protected void deactivate() {
model.getOfferBookListItems().removeListener(changeListener);
model.updateChartDataFlag.removeListener(updateChartDataFlagListener);
tradeCurrencySubscriber.unsubscribe();
currencyComboBox.setOnAction(null);
}

View File

@ -27,7 +27,9 @@ import io.bitsquare.locale.CurrencyUtil;
import io.bitsquare.locale.TradeCurrency;
import io.bitsquare.trade.offer.Offer;
import io.bitsquare.user.Preferences;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
@ -55,8 +57,8 @@ class MarketsChartsViewModel extends ActivatableViewModel {
private final ListChangeListener<OfferBookListItem> listChangeListener;
private final ObservableList<Offer> top3BuyOfferList = FXCollections.observableArrayList();
private final ObservableList<Offer> top3SellOfferList = FXCollections.observableArrayList();
private final ChangeListener<Number> cacheFilledListener;
private final ChangeListener<Number> 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<OfferBookListItem> 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<Number>() {
currenciesUpdatedListener = new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> 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<OfferBookListItem> offerBookListItems) {
private boolean isAnyPricePresent() {
return offerBookListItems.stream().filter(item -> item.getOffer().getPrice() == null).findAny().isPresent();
}
private void updateChartData() {
updateChartDataFlag.set(updateChartDataFlag.get() + 1);
List<Offer> 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());