Add dynamic right align for total amounts column

This commit is contained in:
Christoph Atteneder 2018-02-21 12:40:57 +01:00
parent 35c3d736bd
commit b508671f4e
No known key found for this signature in database
GPG key ID: CD5DC1C529CDFD3B
4 changed files with 84 additions and 7 deletions

View file

@ -268,7 +268,7 @@ public class SpreadView extends ActivatableViewAndModel<GridPane, SpreadViewMode
public void updateItem(final SpreadItem item, boolean empty) {
super.updateItem(item, empty);
if (item != null && !empty)
setGraphic(new ColoredDecimalPlacesWithZerosText(formatter.formatCoin(item.totalAmount, 4), GUIUtil.AMOUNT_DECIMALS_WITH_ZEROS));
setGraphic(new ColoredDecimalPlacesWithZerosText(model.getAmount(item.totalAmount), GUIUtil.AMOUNT_DECIMALS_WITH_ZEROS));
else
setText("");
}

View file

@ -30,6 +30,9 @@ import io.bisq.gui.main.offer.offerbook.OfferBook;
import io.bisq.gui.main.offer.offerbook.OfferBookListItem;
import io.bisq.gui.main.overlays.popups.Popup;
import io.bisq.gui.util.BSFormatter;
import io.bisq.gui.util.GUIUtil;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
@ -52,6 +55,7 @@ class SpreadViewModel extends ActivatableViewModel {
private final ObservableList<OfferBookListItem> offerBookListItems;
private final ListChangeListener<OfferBookListItem> listChangeListener;
final ObservableList<SpreadItem> spreadItems = FXCollections.observableArrayList();
final IntegerProperty maxPlacesForAmount = new SimpleIntegerProperty();
///////////////////////////////////////////////////////////////////////////////////////////
@ -90,6 +94,9 @@ class SpreadViewModel extends ActivatableViewModel {
offersByCurrencyMap.get(currencyCode).add(offer);
}
spreadItems.clear();
Coin totalAmount = null;
for (String currencyCode : offersByCurrencyMap.keySet()) {
List<Offer> offers = offersByCurrencyMap.get(currencyCode);
final boolean isFiatCurrency = CurrencyUtil.isFiatCurrency(currencyCode);
@ -183,9 +190,19 @@ class SpreadViewModel extends ActivatableViewModel {
}
}
Coin totalAmount = Coin.valueOf(offers.stream().mapToLong(offer -> offer.getAmount().getValue()).sum());
totalAmount = Coin.valueOf(offers.stream().mapToLong(offer -> offer.getAmount().getValue()).sum());
spreadItems.add(new SpreadItem(currencyCode, buyOffers.size(), sellOffers.size(),
offers.size(), spread, percentage, totalAmount));
}
maxPlacesForAmount.set(formatAmount(totalAmount, false).length());
}
public String getAmount(Coin amount) {
return formatAmount(amount, true);
}
private String formatAmount(Coin amount, boolean decimalAligned) {
return formatter.formatCoin(amount, GUIUtil.AMOUNT_DECIMALS, decimalAligned, maxPlacesForAmount.get());
}
}

View file

@ -86,23 +86,32 @@ public class BSFormatter {
@NotNull
public String formatCoin(Coin coin, int decimalPlaces) {
return formatCoin(coin, decimalPlaces, false, 0);
}
public String formatCoin(Coin coin, int decimalPlaces, boolean decimalAligned, int maxNumberOfDigits) {
final int repetitions = decimalPlaces;
String formattedCoin = "";
if (coin != null) {
try {
if (decimalPlaces < 0) {
return coinFormat.noCode().format(coin).toString();
formattedCoin = coinFormat.noCode().format(coin).toString();
} else {
final int decimals = decimalPlaces/repetitions;
return coinFormat.noCode().minDecimals(repetitions).repeatOptionalDecimals(decimals, repetitions).format(coin).toString();
formattedCoin = coinFormat.noCode().minDecimals(repetitions).repeatOptionalDecimals(decimals, repetitions).format(coin).toString();
}
} catch (Throwable t) {
log.warn("Exception at formatBtc: " + t.toString());
return "";
}
} else {
return "";
}
if (decimalAligned) {
formattedCoin = fillUpPlacesWithEmptyStrings(formattedCoin, maxNumberOfDigits);
}
return formattedCoin;
}
public String formatCoinWithCode(Coin coin) {

View file

@ -0,0 +1,51 @@
package io.bisq.gui.main.market.spread;
import io.bisq.gui.main.offer.offerbook.OfferBook;
import io.bisq.gui.main.offer.offerbook.OfferBookListItem;
import io.bisq.gui.main.offer.offerbook.OfferBookListItemMaker;
import io.bisq.gui.util.BSFormatter;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static com.natpryce.makeiteasy.MakeItEasy.make;
import static com.natpryce.makeiteasy.MakeItEasy.with;
import static io.bisq.gui.main.offer.offerbook.OfferBookListItemMaker.btcItem;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest(OfferBook.class)
public class SpreadViewModelTest {
@Test
public void testMaxCharactersForAmountWithNoOffers() {
OfferBook offerBook = mock(OfferBook.class);
final ObservableList<OfferBookListItem> offerBookListItems = FXCollections.observableArrayList();
when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems);
final SpreadViewModel model = new SpreadViewModel(offerBook, null, new BSFormatter());
assertEquals(0, model.maxPlacesForAmount.intValue());
}
@Test
public void testMaxCharactersForAmount() {
OfferBook offerBook = mock(OfferBook.class);
final ObservableList<OfferBookListItem> offerBookListItems = FXCollections.observableArrayList();
offerBookListItems.addAll(make(btcItem));
when(offerBook.getOfferBookListItems()).thenReturn(offerBookListItems);
final SpreadViewModel model = new SpreadViewModel(offerBook, null, new BSFormatter());
model.activate();
assertEquals(6, model.maxPlacesForAmount.intValue()); // 0.001
offerBookListItems.addAll(make(btcItem.but(with(OfferBookListItemMaker.amount,1403000000L))));
assertEquals(7, model.maxPlacesForAmount.intValue()); //14.0300
}
}