improve amount error labels adding dust threshold label for too low amounts

This commit is contained in:
Craig Raw 2021-06-29 12:48:02 +02:00
parent badf8c8f2f
commit 8033e5fd88
3 changed files with 35 additions and 11 deletions

View file

@ -21,6 +21,8 @@ import com.sparrowwallet.sparrow.event.FiatCurrencySelectedEvent;
import com.sparrowwallet.sparrow.io.Config;
import com.sparrowwallet.sparrow.net.ExchangeSource;
import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
@ -65,12 +67,19 @@ public class PaymentController extends WalletFormController implements Initializ
@FXML
private Label amountStatus;
@FXML
private Label dustStatus;
@FXML
private ToggleButton maxButton;
@FXML
private Button addPaymentButton;
private final BooleanProperty emptyAmountProperty = new SimpleBooleanProperty(true);
private final BooleanProperty dustAmountProperty = new SimpleBooleanProperty();
private final ChangeListener<String> amountListener = new ChangeListener<>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
@ -86,8 +95,12 @@ public class PaymentController extends WalletFormController implements Initializ
Long recipientValueSats = getRecipientValueSats();
if(recipientValueSats != null) {
setFiatAmount(AppServices.getFiatCurrencyExchangeRate(), recipientValueSats);
dustAmountProperty.set(recipientValueSats <= getRecipientDustThreshold());
emptyAmountProperty.set(false);
} else {
fiatAmount.setText("");
dustAmountProperty.set(false);
emptyAmountProperty.set(true);
}
sendController.updateTransaction();
@ -115,7 +128,7 @@ public class PaymentController extends WalletFormController implements Initializ
//ignore, not a URI
}
revalidate(amount, amountListener);
revalidateAmount();
maxButton.setDisable(!isValidAddressAndLabel());
sendController.updateTransaction();
@ -148,10 +161,9 @@ public class PaymentController extends WalletFormController implements Initializ
maxButton.setText("Max" + newValue);
});
amountStatus.managedProperty().bind(amountStatus.visibleProperty());
amountStatus.setVisible(sendController.isInsufficientInputs());
sendController.insufficientInputsProperty().addListener((observable, oldValue, newValue) -> {
amountStatus.setVisible(newValue);
});
amountStatus.visibleProperty().bind(sendController.insufficientInputsProperty().and(dustAmountProperty.not()).and(emptyAmountProperty.not()));
dustStatus.managedProperty().bind(dustStatus.visibleProperty());
dustStatus.visibleProperty().bind(dustAmountProperty);
Optional<Tab> firstTab = sendController.getPaymentTabs().getTabs().stream().findFirst();
if(firstTab.isPresent()) {
@ -173,7 +185,7 @@ public class PaymentController extends WalletFormController implements Initializ
Validator.createEmptyValidator("Label is required")
));
validationSupport.registerValidator(amount, Validator.combine(
(Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Insufficient Inputs", sendController.isInsufficientInputs()),
(Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Insufficient Inputs", getRecipientValueSats() != null && sendController.isInsufficientInputs()),
(Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Insufficient Value", getRecipientValueSats() != null && getRecipientValueSats() <= getRecipientDustThreshold())
));
}
@ -234,8 +246,11 @@ public class PaymentController extends WalletFormController implements Initializ
}
}
public void revalidate() {
public void revalidateAmount() {
revalidate(amount, amountListener);
Long recipientValueSats = getRecipientValueSats();
dustAmountProperty.set(recipientValueSats != null && recipientValueSats <= getRecipientDustThreshold());
emptyAmountProperty.set(recipientValueSats == null);
}
private void revalidate(TextField field, ChangeListener<String> listener) {
@ -302,7 +317,7 @@ public class PaymentController extends WalletFormController implements Initializ
fiatAmount.setText("");
setSendMax(false);
amountStatus.setVisible(false);
dustAmountProperty.set(false);
}
public void setMaxInput(ActionEvent event) {

View file

@ -170,7 +170,7 @@ public class SendController extends WalletFormController implements Initializabl
userFeeSet.set(false);
for(Tab tab : paymentTabs.getTabs()) {
PaymentController controller = (PaymentController)tab.getUserData();
controller.revalidate();
controller.revalidateAmount();
}
updateTransaction();
}
@ -183,7 +183,7 @@ public class SendController extends WalletFormController implements Initializabl
userFeeSet.set(false);
for(Tab tab : paymentTabs.getTabs()) {
PaymentController controller = (PaymentController)tab.getUserData();
controller.revalidate();
controller.revalidateAmount();
}
updateTransaction();
}
@ -228,7 +228,7 @@ public class SendController extends WalletFormController implements Initializabl
insufficientInputsProperty.addListener((observable, oldValue, newValue) -> {
for(Tab tab : paymentTabs.getTabs()) {
PaymentController controller = (PaymentController)tab.getUserData();
controller.revalidate();
controller.revalidateAmount();
}
revalidate(fee, feeListener);
});
@ -905,6 +905,7 @@ public class SendController extends WalletFormController implements Initializabl
excludedChangeNodes.clear();
walletTransactionProperty.setValue(null);
createdWalletTransactionProperty.set(null);
insufficientInputsProperty.set(false);
validationSupport.setErrorDecorationEnabled(false);
}

View file

@ -65,6 +65,14 @@
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="EXCLAMATION_CIRCLE" styleClass="failure" />
</graphic>
</Label>
<Label fx:id="dustStatus" text="Amount too low">
<tooltip>
<Tooltip text="Amount below the Bitcoin network dust threshold for this address type" />
</tooltip>
<graphic>
<Glyph fontFamily="Font Awesome 5 Free Solid" fontSize="12" icon="EXCLAMATION_CIRCLE" styleClass="failure" />
</graphic>
</Label>
</Group>
<Region style="-fx-pref-width: 5" />
<ToggleButton fx:id="maxButton" text="Max" onAction="#setMaxInput" />