Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only show range when necessary #1365

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/src/main/java/io/bisq/core/offer/Offer.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ public Coin getMinAmount() {
return Coin.valueOf(offerPayload.getMinAmount());
}

public boolean isRange() {
return offerPayload.getAmount() != offerPayload.getMinAmount();
}

public Date getDate() {
return new Date(offerPayload.getDate());
}
Expand Down
36 changes: 36 additions & 0 deletions core/src/test/java/io/bisq/core/offer/OfferTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.bisq.core.offer;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest(OfferPayload.class)
public class OfferTest {

@Test
public void testHasNoRange() {
OfferPayload payload = mock(OfferPayload.class);
when(payload.getMinAmount()).thenReturn(1000L);
when(payload.getAmount()).thenReturn(1000L);

Offer offer = new Offer(payload);
assertFalse(offer.isRange());
}

@Test
public void testHasRange() {
OfferPayload payload = mock(OfferPayload.class);
when(payload.getMinAmount()).thenReturn(1000L);
when(payload.getAmount()).thenReturn(2000L);

Offer offer = new Offer(payload);
assertTrue(offer.isRange());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ public class TakeOfferView extends ActivatableViewAndModel<AnchorPane, TakeOffer
private ScrollPane scrollPane;
private GridPane gridPane;
private TitledGroupBg payFundsPane, paymentAccountTitledGroupBg;
private VBox priceAsPercentageInputBox;
private VBox priceAsPercentageInputBox, amountRangeBox;
private HBox fundingHBox;
private ComboBox<PaymentAccount> paymentAccountsComboBox;
private Label directionLabel, amountDescriptionLabel, addressLabel, balanceLabel, totalToPayLabel,
paymentAccountsLabel, paymentMethodLabel,
priceCurrencyLabel, priceAsPercentageLabel,
volumeCurrencyLabel, priceDescriptionLabel, volumeDescriptionLabel,
waitingForFundsLabel, offerAvailabilityLabel;
waitingForFundsLabel, offerAvailabilityLabel, amountCurrency;
private InputTextField amountTextField;
private TextField paymentMethodTextField, currencyTextField, priceTextField, priceAsPercentageTextField,
volumeTextField, amountRangeTextField;
Expand All @@ -116,7 +116,6 @@ public class TakeOfferView extends ActivatableViewAndModel<AnchorPane, TakeOffer
private boolean offerDetailsWindowDisplayed, clearXchangeWarningDisplayed;
private SimpleBooleanProperty errorPopupDisplayed;


///////////////////////////////////////////////////////////////////////////////////////////
// Constructor, lifecycle
///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -197,6 +196,10 @@ protected void activate() {
balanceTextField.setTargetAmount(model.dataModel.totalToPayAsCoin.get());

maybeShowClearXchangeWarning();

if (!model.isRange()) {
showNextStepAfterAmountIsSet();
}
}

private void showInsufficientBsqFundsForBtcFeePaymentPopup() {
Expand Down Expand Up @@ -278,7 +281,17 @@ public void initWithData(Offer offer) {
currencyTextField.setText(model.dataModel.getCurrencyNameAndCode());
directionLabel.setText(model.getDirectionLabel());
amountDescriptionLabel.setText(model.getAmountDescription());
amountRangeTextField.setText(model.getAmountRange());

if (model.isRange()) {
amountRangeTextField.setText(model.getAmountRange());
amountRangeBox.setVisible(true);
} else {
amountTextField.setMouseTransparent(true);
amountTextField.setEditable(false);
amountTextField.setFocusTraversable(false);
amountCurrency.setId("currency-info-label-disabled");
}

priceTextField.setText(model.getPrice());
priceAsPercentageTextField.setText(model.marketPriceMargin);
addressTextField.setPaymentLabel(model.getPaymentLabel());
Expand Down Expand Up @@ -726,10 +739,7 @@ private void addButtons() {
nextButton = new AutoTooltipButton(Res.get("shared.nextStep"));
nextButton.setDefaultButton(true);
nextButton.setOnAction(e -> {
if (DevEnv.DAO_TRADING_ACTIVATED)
showFeeOption();
else
onShowPayFundsScreen();
showNextStepAfterAmountIsSet();
});

cancelButton1 = new AutoTooltipButton(Res.get("shared.cancel"));
Expand All @@ -741,6 +751,13 @@ private void addButtons() {
});
}

private void showNextStepAfterAmountIsSet() {
if (DevEnv.DAO_TRADING_ACTIVATED)
showFeeOption();
else
onShowPayFundsScreen();
}

private void showFeeOption() {
Coin makerFee = model.dataModel.getTakerFee(false);
String missingBsq = null;
Expand Down Expand Up @@ -892,6 +909,7 @@ private void addAmountPriceFields() {
Tuple3<HBox, InputTextField, Label> amountValueCurrencyBoxTuple = getAmountCurrencyBox(Res.get("takeOffer.amount.prompt"));
HBox amountValueCurrencyBox = amountValueCurrencyBoxTuple.first;
amountTextField = amountValueCurrencyBoxTuple.second;
amountCurrency = amountValueCurrencyBoxTuple.third;
Tuple2<Label, VBox> amountInputBoxTuple = getTradeInputBox(amountValueCurrencyBox, model.getAmountDescription());
amountDescriptionLabel = amountInputBoxTuple.first;
VBox amountBox = amountInputBoxTuple.second;
Expand Down Expand Up @@ -951,12 +969,14 @@ private void addSecondRow() {
priceAsPercentageLabel.getStyleClass().add("percentage-label");

Tuple3<HBox, TextField, Label> amountValueCurrencyBoxTuple = getNonEditableValueCurrencyBox();
HBox amountValueCurrencyBox = amountValueCurrencyBoxTuple.first;
amountRangeTextField = amountValueCurrencyBoxTuple.second;

Tuple2<Label, VBox> amountInputBoxTuple = getTradeInputBox(amountValueCurrencyBox,
Tuple2<Label, VBox> amountInputBoxTuple = getTradeInputBox( amountValueCurrencyBoxTuple.first,
Res.get("takeOffer.amountPriceBox.amountRangeDescription"));

amountRangeBox = amountInputBoxTuple.second;
amountRangeBox.setVisible(false);

Label xLabel = new AutoTooltipLabel("x");
xLabel.setFont(Font.font("Helvetica-Bold", 20));
xLabel.setPadding(new Insets(14, 3, 0, 3));
Expand All @@ -965,7 +985,7 @@ private void addSecondRow() {
HBox hBox = new HBox();
hBox.setSpacing(5);
hBox.setAlignment(Pos.CENTER_LEFT);
hBox.getChildren().addAll(amountInputBoxTuple.second, xLabel, priceAsPercentageInputBox);
hBox.getChildren().addAll(amountRangeBox, xLabel, priceAsPercentageInputBox);

GridPane.setRowIndex(hBox, ++gridRow);
GridPane.setColumnIndex(hBox, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ public Offer getOffer() {
return dataModel.getOffer();
}

public boolean isRange() {return dataModel.getOffer().isRange(); }

public String getAmountRange() {
return amountRange;
}
Expand Down
4 changes: 2 additions & 2 deletions gui/src/main/java/io/bisq/gui/util/BSFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public String formatVolumeLabel(String currencyCode, String postFix) {
}

public String formatMinVolumeAndVolume(Offer offer) {
return formatVolume(offer.getMinVolume()) + " - " + formatVolume(offer.getVolume());
return offer.isRange() ? formatVolume(offer.getMinVolume()) + " - " + formatVolume(offer.getVolume()) : formatVolume(offer.getVolume());
}


Expand All @@ -316,7 +316,7 @@ public String formatAmount(Offer offer) {
}

public String formatAmountWithMinAmount(Offer offer) {
return formatCoin(offer.getMinAmount()) + " - " + formatCoin(offer.getAmount());
return offer.isRange() ? formatCoin(offer.getMinAmount()) + " - " + formatCoin(offer.getAmount()) : formatCoin(offer.getAmount());
}


Expand Down
72 changes: 71 additions & 1 deletion gui/src/test/java/io/bisq/gui/util/BSFormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,32 @@
package io.bisq.gui.util;

import io.bisq.common.locale.Res;
import io.bisq.common.monetary.Volume;
import io.bisq.core.offer.Offer;
import io.bisq.core.offer.OfferPayload;
import org.bitcoinj.core.Coin;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.Locale;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest({Offer.class,OfferPayload.class})
public class BSFormatterTest {

private BSFormatter formatter;

@Before
public void setup() {
public void setUp() {
Locale.setDefault(new Locale("en", "US"));
formatter = new BSFormatter();
Res.setBaseCurrencyCode("BTC");
Expand Down Expand Up @@ -67,4 +78,63 @@ public void testFormatDurationAsWords() {
assertEquals("", formatter.formatDurationAsWords(0));
assertTrue(formatter.formatDurationAsWords(0).isEmpty());
}

@Test
public void testFormatSameVolume() {
Offer offer = mock(Offer.class);
Volume btc = Volume.parse("0.10", "BTC");
when(offer.getMinVolume()).thenReturn(btc);
when(offer.getVolume()).thenReturn(btc);

assertEquals("0.10000000", formatter.formatMinVolumeAndVolume(offer));
}

@Test
public void testFormatDifferentVolume() {
Offer offer = mock(Offer.class);
Volume btcMin = Volume.parse("0.10", "BTC");
Volume btcMax = Volume.parse("0.25", "BTC");
when(offer.isRange()).thenReturn(true);
when(offer.getMinVolume()).thenReturn(btcMin);
when(offer.getVolume()).thenReturn(btcMax);

assertEquals("0.10000000 - 0.25000000", formatter.formatMinVolumeAndVolume(offer));
}

@Test
public void testFormatNullVolume() {
Offer offer = mock(Offer.class);
when(offer.getMinVolume()).thenReturn(null);
when(offer.getVolume()).thenReturn(null);

assertEquals("", formatter.formatMinVolumeAndVolume(offer));
}

@Test
public void testFormatSameAmount() {
Offer offer = mock(Offer.class);
when(offer.getMinAmount()).thenReturn(Coin.valueOf(10000000));
when(offer.getAmount()).thenReturn(Coin.valueOf(10000000));

assertEquals("0.10", formatter.formatAmountWithMinAmount(offer));
}

@Test
public void testFormatDifferentAmount() {
OfferPayload offerPayload = mock(OfferPayload.class);
Offer offer = new Offer(offerPayload);
when(offerPayload.getMinAmount()).thenReturn(10000000L);
when(offerPayload.getAmount()).thenReturn(20000000L);

assertEquals("0.10 - 0.20", formatter.formatAmountWithMinAmount(offer));
}

@Test
public void testFormatNullAmount() {
Offer offer = mock(Offer.class);
when(offer.getMinAmount()).thenReturn(null);
when(offer.getAmount()).thenReturn(null);

assertEquals("", formatter.formatAmountWithMinAmount(offer));
}
}