Skip to content

Commit

Permalink
Merge pull request #1529 from ripcurlx/fix-spread-sorting
Browse files Browse the repository at this point in the history
Fix spread sorting in spread view
  • Loading branch information
ripcurlx authored May 3, 2018
2 parents e193199 + 12662c5 commit 1a1f8d7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ public class SpreadItem {
@Nullable
public final Price priceSpread;
public final String percentage;
public final double percentageValue;
public final Coin totalAmount;

public SpreadItem(String currencyCode, int numberOfBuyOffers, int numberOfSellOffers, int numberOfOffers, @Nullable Price priceSpread, String percentage, Coin totalAmount) {
public SpreadItem(String currencyCode, int numberOfBuyOffers, int numberOfSellOffers, int numberOfOffers,
@Nullable Price priceSpread, String percentage, double percentageValue, Coin totalAmount) {
this.currencyCode = currencyCode;
this.numberOfBuyOffers = numberOfBuyOffers;
this.numberOfSellOffers = numberOfSellOffers;
this.numberOfOffers = numberOfOffers;
this.priceSpread = priceSpread;
this.percentage = percentage;
this.percentageValue = percentageValue;
this.totalAmount = totalAmount;
}
}
12 changes: 5 additions & 7 deletions src/main/java/bisq/desktop/main/market/spread/SpreadView.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

import javafx.util.Callback;

import java.util.Comparator;

@FxmlView
public class SpreadView extends ActivatableViewAndModel<GridPane, SpreadViewModel> {
private final BSFormatter formatter;
Expand Down Expand Up @@ -93,16 +95,12 @@ public void initialize() {
tableView.getColumns().add(spreadColumn);
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

currencyColumn.setComparator((o1, o2) -> CurrencyUtil.getNameByCode(o1.currencyCode).compareTo(CurrencyUtil.getNameByCode(o2.currencyCode)));
currencyColumn.setComparator(Comparator.comparing(o -> CurrencyUtil.getNameByCode(o.currencyCode)));
numberOfOffersColumn.setComparator((o1, o2) -> Integer.valueOf(o1.numberOfOffers).compareTo(o2.numberOfOffers));
numberOfBuyOffersColumn.setComparator((o1, o2) -> Integer.valueOf(o1.numberOfBuyOffers).compareTo(o2.numberOfBuyOffers));
numberOfSellOffersColumn.setComparator((o1, o2) -> Integer.valueOf(o1.numberOfSellOffers).compareTo(o2.numberOfSellOffers));
totalAmountColumn.setComparator((o1, o2) -> o1.totalAmount.compareTo(o2.totalAmount));
spreadColumn.setComparator((o1, o2) -> {
Long spreadO1 = o1.priceSpread != null ? o1.priceSpread.getValue() : 0;
Long spreadO2 = o2.priceSpread != null ? o2.priceSpread.getValue() : 0;
return spreadO1.compareTo(spreadO2);
});
totalAmountColumn.setComparator(Comparator.comparing(o -> o.totalAmount));
spreadColumn.setComparator(Comparator.comparingDouble(o -> o.percentageValue));

numberOfOffersColumn.setSortType(TableColumn.SortType.DESCENDING);
tableView.getSortOrder().add(numberOfOffersColumn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ private void update(ObservableList<OfferBookListItem> offerBookListItems) {

Price spread = null;
String percentage = "";
double percentageValue = 0;
Price bestSellOfferPrice = sellOffers.isEmpty() ? null : sellOffers.get(0).getPrice();
Price bestBuyOfferPrice = buyOffers.isEmpty() ? null : buyOffers.get(0).getPrice();
if (bestBuyOfferPrice != null && bestSellOfferPrice != null) {
Expand All @@ -180,11 +181,11 @@ private void update(ObservableList<OfferBookListItem> offerBookListItems) {
BigDecimal marketPriceAsBigDecimal = BigDecimal.valueOf(marketPriceAsDouble)
.multiply(BigDecimal.valueOf(precision));
// We multiply with 10000 because we use precision of 2 at % (100.00%)
double result = BigDecimal.valueOf(spread.getValue())
percentageValue = BigDecimal.valueOf(spread.getValue())
.multiply(BigDecimal.valueOf(10000))
.divide(marketPriceAsBigDecimal, RoundingMode.HALF_UP)
.doubleValue() / 10000;
percentage = formatter.formatPercentagePrice(result);
percentage = formatter.formatPercentagePrice(percentageValue);
}
} catch (Throwable t) {
try {
Expand All @@ -210,7 +211,7 @@ private void update(ObservableList<OfferBookListItem> offerBookListItems) {

totalAmount = Coin.valueOf(offers.stream().mapToLong(offer -> offer.getAmount().getValue()).sum());
spreadItems.add(new SpreadItem(currencyCode, buyOffers.size(), sellOffers.size(),
uniqueOffers.size(), spread, percentage, totalAmount));
uniqueOffers.size(), spread, percentage, percentageValue, totalAmount));
}

maxPlacesForAmount.set(formatAmount(totalAmount, false).length());
Expand Down

0 comments on commit 1a1f8d7

Please sign in to comment.