Skip to content

Commit

Permalink
Merge pull request #3066 from axpoems/limit-char-count-in-amount-sele…
Browse files Browse the repository at this point in the history
…ction

Limit char count in amount selection
  • Loading branch information
axpoems authored Dec 29, 2024
2 parents 8708605 + 3206c24 commit 16a7f4c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,23 @@

@Slf4j
public class AmountSelectionView extends View<VBox, AmountSelectionModel, AmountSelectionController> {
public final static int AMOUNT_BOX_WIDTH = 300;
public final static int AMOUNT_BOX_HEIGHT = 120;
private final static Insets SLIDER_INDICATORS_RANGE_MARGIN = new Insets(-15, 0, 0, 0);
private final static Insets SLIDER_INDICATORS_FIXED_MARGIN = new Insets(2.5, 0, 0, 0);
private final static String INPUT_TEXT_9_STYLE_CLASS = "input-text-9";
private final static String INPUT_TEXT_10_STYLE_CLASS = "input-text-10";
private final static String INPUT_TEXT_11_STYLE_CLASS = "input-text-11";
private final static String INPUT_TEXT_12_STYLE_CLASS = "input-text-12";
private final static String INPUT_TEXT_13_STYLE_CLASS = "input-text-13";
private final static String INPUT_TEXT_14_STYLE_CLASS = "input-text-14";
private final static String INPUT_TEXT_15_STYLE_CLASS = "input-text-15";
private final static String INPUT_TEXT_16_STYLE_CLASS = "input-text-16";
private final static String INPUT_TEXT_17_STYLE_CLASS = "input-text-17";
private final static String INPUT_TEXT_18_STYLE_CLASS = "input-text-18";
private final static String INPUT_TEXT_19_STYLE_CLASS = "input-text-19";
public static final int AMOUNT_BOX_WIDTH = 300;
public static final int AMOUNT_BOX_HEIGHT = 120;
private static final int RANGE_INPUT_TEXT_MAX_LENGTH = 9;
private static final int FIXED_INPUT_TEXT_MAX_LENGTH = 19;
private static final Insets SLIDER_INDICATORS_RANGE_MARGIN = new Insets(-15, 0, 0, 0);
private static final Insets SLIDER_INDICATORS_FIXED_MARGIN = new Insets(2.5, 0, 0, 0);
private static final String INPUT_TEXT_9_STYLE_CLASS = "input-text-9";
private static final String INPUT_TEXT_10_STYLE_CLASS = "input-text-10";
private static final String INPUT_TEXT_11_STYLE_CLASS = "input-text-11";
private static final String INPUT_TEXT_12_STYLE_CLASS = "input-text-12";
private static final String INPUT_TEXT_13_STYLE_CLASS = "input-text-13";
private static final String INPUT_TEXT_14_STYLE_CLASS = "input-text-14";
private static final String INPUT_TEXT_15_STYLE_CLASS = "input-text-15";
private static final String INPUT_TEXT_16_STYLE_CLASS = "input-text-16";
private static final String INPUT_TEXT_17_STYLE_CLASS = "input-text-17";
private static final String INPUT_TEXT_18_STYLE_CLASS = "input-text-18";
private static final String INPUT_TEXT_19_STYLE_CLASS = "input-text-19";
@SuppressWarnings("UnnecessaryUnicodeEscape")
private static final String EN_DASH_SYMBOL = "\u2013"; // Unicode for "–"

Expand Down Expand Up @@ -222,6 +224,8 @@ protected void onViewAttached() {
root.getStyleClass().add("amount-selection");
root.getStyleClass().add(isRangeAmountEnabled ? "range-amount" : "fixed-amount");
VBox.setMargin(sliderIndicators, isRangeAmountEnabled ? SLIDER_INDICATORS_RANGE_MARGIN : SLIDER_INDICATORS_FIXED_MARGIN);
maxOrFixedQuoteAmount.setTextInputMaxCharCount(isRangeAmountEnabled ? RANGE_INPUT_TEXT_MAX_LENGTH : FIXED_INPUT_TEXT_MAX_LENGTH);
minQuoteAmount.setTextInputMaxCharCount(RANGE_INPUT_TEXT_MAX_LENGTH);
applyTextInputFontStyle();
applyTextInputPrefWidth();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

@Slf4j
public abstract class AmountInput {
protected final Controller controller;
Expand Down Expand Up @@ -97,6 +99,10 @@ public void requestFocus() {
textInput.selectRange(textInput.getLength(), textInput.getLength());
}

public void setTextInputMaxCharCount(int maxCharCount) {
controller.model.textInputMaxCharCount = Optional.of(maxCharCount);
}

protected static class Controller implements bisq.desktop.common.view.Controller {
@Setter
protected Model model;
Expand Down Expand Up @@ -178,31 +184,35 @@ private void updateModel() {
}

protected static class Model implements bisq.desktop.common.view.Model {
private final BooleanProperty isAmountValid = new SimpleBooleanProperty(true);
protected final boolean isBaseCurrency;
protected final boolean showCurrencyCode;
protected final ObjectProperty<Monetary> amount = new SimpleObjectProperty<>();
protected final StringProperty code = new SimpleStringProperty();
private Optional<Integer> textInputMaxCharCount = Optional.empty();
protected Market selectedMarket;
protected boolean hasFocus;
@Setter
protected boolean useLowPrecision = true;
private final BooleanProperty isAmountValid = new SimpleBooleanProperty(true);

protected Model(boolean isBaseCurrency, boolean showCurrencyCode) {
this.isBaseCurrency = isBaseCurrency;
this.showCurrencyCode = showCurrencyCode;
}

void reset() {
isAmountValid.set(true);
amount.set(null);
code.set(null);
textInputMaxCharCount = Optional.empty();
selectedMarket = null;
hasFocus = false;
isAmountValid.set(true);
useLowPrecision = true;
}
}

protected static class View extends bisq.desktop.common.view.View<HBox, Model, Controller> {
private final ChangeListener<String> textListener;
protected final ChangeListener<Boolean> focusListener;
protected final ChangeListener<Monetary> amountListener;
protected final TextField textInput;
Expand All @@ -217,6 +227,7 @@ protected View(Model model, Controller controller) {
codeLabel.setVisible(model.showCurrencyCode);
codeLabel.setManaged(model.showCurrencyCode);
root.getChildren().addAll(textInput, codeLabel);
textListener = this::onTextChanged;
focusListener = this::onFocusChanged;
amountListener = this::onAmountChanged;
initView();
Expand All @@ -233,6 +244,14 @@ protected Label createCodeLabel() {
protected void initView() {
}

private void onTextChanged(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (model.textInputMaxCharCount.isPresent()) {
if (newValue.length() > model.textInputMaxCharCount.get()) {
textInput.setText(oldValue);
}
}
}

private void onFocusChanged(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
controller.onFocusChange(newValue);
if (oldValue) {
Expand All @@ -255,6 +274,7 @@ protected void applyAmount(Monetary newValue) {
protected void onViewAttached() {
codeLabel.textProperty().bind(model.code);

textInput.textProperty().addListener(textListener);
textInput.focusedProperty().addListener(focusListener);
model.amount.addListener(amountListener);

Expand All @@ -267,6 +287,7 @@ protected void onViewAttached() {
protected void onViewDetached() {
codeLabel.textProperty().unbind();

textInput.textProperty().removeListener(textListener);
textInput.focusedProperty().removeListener(focusListener);
model.amount.removeListener(amountListener);
}
Expand Down

0 comments on commit 16a7f4c

Please sign in to comment.