Skip to content

Commit

Permalink
Allow negative numbers for percentage price
Browse files Browse the repository at this point in the history
  • Loading branch information
axpoems committed Jan 8, 2025
1 parent 590a0b6 commit e16b888
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
import java.util.List;

public class BisqEasyViewUtils {
public static final String NUMERIC_WITH_DECIMAL_REGEX = "\\d*([.,]\\d*)?";
public static final String POSITIVE_NUMERIC_WITH_DECIMAL_REGEX = "\\d*([.,]\\d*)?";
public static final String NUMERIC_WITH_DECIMAL_REGEX = "-?\\d*([.,]\\d*)?";
private static final String[] customPaymentIconIds = {
"CUSTOM_PAYMENT_1", "CUSTOM_PAYMENT_2", "CUSTOM_PAYMENT_3",
"CUSTOM_PAYMENT_4", "CUSTOM_PAYMENT_5", "CUSTOM_PAYMENT_6"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import bisq.common.util.MathUtils;
import bisq.desktop.common.threading.UIThread;
import bisq.desktop.components.controls.validator.NumberValidator;
import bisq.desktop.main.content.bisq_easy.BisqEasyViewUtils;
import bisq.i18n.Res;
import bisq.presentation.formatters.PriceFormatter;
import bisq.presentation.parser.PriceParser;
Expand Down Expand Up @@ -266,7 +267,8 @@ public static class View extends bisq.desktop.common.view.View<Pane, Model, Cont
private View(Model model, Controller controller, NumberValidator validator) {
super(new VBox(), model, controller);

textInput = new PriceInputBox(model.description.get(), Res.get("component.priceInput.prompt"));
textInput = new PriceInputBox(model.description.get(), Res.get("component.priceInput.prompt"),
BisqEasyViewUtils.POSITIVE_NUMERIC_WITH_DECIMAL_REGEX);
textInput.setPrefWidth(WIDTH);
textInput.setValidator(validator);
textInput.conversionPriceSymbolTextProperty().set("%");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package bisq.desktop.main.content.bisq_easy.components;

import bisq.desktop.components.controls.MaterialTextField;
import bisq.desktop.main.content.bisq_easy.BisqEasyViewUtils;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.geometry.Insets;
Expand All @@ -43,7 +42,7 @@ public class PriceInputBox extends MaterialTextField {
private final HBox textInputAndSymbolHBox;
private final ChangeListener<String> textInputTextListener;

public PriceInputBox(String description, String prompt) {
public PriceInputBox(String description, String prompt, String numericRegex) {
super(description, prompt);

bg.getStyleClass().setAll("bisq-dual-amount-bg");
Expand All @@ -67,17 +66,16 @@ public PriceInputBox(String description, String prompt) {
getStyleClass().add("price-input-box");

textInputTextListener = (observable, oldValue, newValue) -> {
if (newValue.length() > INPUT_TEXT_MAX_LENGTH
|| !newValue.matches(BisqEasyViewUtils.NUMERIC_WITH_DECIMAL_REGEX)) {
if (newValue.length() > INPUT_TEXT_MAX_LENGTH || !newValue.matches(numericRegex)) {
textInputControl.setText(oldValue);
}
applyFontStyle(textInputControl.getLength());
};
initialize();
}

public PriceInputBox(String description) {
this(description, null);
public PriceInputBox(String description, String numericRegex) {
this(description, null, numericRegex);
}

public void initialize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ protected void onViewDetached() {
private void onTextChanged(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (model.textInputMaxCharCount.isPresent()) {
if (newValue.length() > model.textInputMaxCharCount.get()
|| !newValue.matches(BisqEasyViewUtils.NUMERIC_WITH_DECIMAL_REGEX)) {
|| !newValue.matches(BisqEasyViewUtils.POSITIVE_NUMERIC_WITH_DECIMAL_REGEX)) {
textInput.setText(oldValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import bisq.desktop.components.containers.Spacer;
import bisq.desktop.components.controls.UnorderedList;
import bisq.desktop.components.controls.validator.PercentageValidator;
import bisq.desktop.main.content.bisq_easy.BisqEasyViewUtils;
import bisq.desktop.main.content.bisq_easy.components.PriceInput;
import bisq.desktop.main.content.bisq_easy.components.PriceInputBox;
import bisq.i18n.Res;
Expand Down Expand Up @@ -80,7 +81,8 @@ public TradeWizardPriceView(TradeWizardPriceModel model, TradeWizardPriceControl
pricingModels.getStyleClass().addAll("selection-models", "bisq-text-3");

// Input box
percentageInput = new PriceInputBox(Res.get("bisqEasy.price.percentage.inputBoxText"));
percentageInput = new PriceInputBox(Res.get("bisqEasy.price.percentage.inputBoxText"),
BisqEasyViewUtils.NUMERIC_WITH_DECIMAL_REGEX);
percentageInput.setValidator(new PercentageValidator());
percentageInput.textInputSymbolTextProperty().set("%");
VBox fieldsBox = new VBox(20, priceInput.getRoot(), percentageInput);
Expand Down

0 comments on commit e16b888

Please sign in to comment.