use bindings in create offer controller

This commit is contained in:
Manfred Karrer 2014-08-07 18:44:10 +02:00
parent a9be983caa
commit af30d89921
3 changed files with 90 additions and 79 deletions

View file

@ -1,6 +1,5 @@
package io.bitsquare.gui.market.createOffer; package io.bitsquare.gui.market.createOffer;
import com.google.bitcoin.core.Coin;
import io.bitsquare.BitSquare; import io.bitsquare.BitSquare;
import io.bitsquare.bank.BankAccount; import io.bitsquare.bank.BankAccount;
import io.bitsquare.btc.AddressEntry; import io.bitsquare.btc.AddressEntry;
@ -45,48 +44,49 @@ import static javafx.beans.binding.Bindings.createDoubleBinding;
import static javafx.beans.binding.Bindings.createStringBinding; import static javafx.beans.binding.Bindings.createStringBinding;
/** /**
* Represents the visible state of he view * Represents the visible state of the view
*/ */
class ViewModel class ViewModel
{ {
StringProperty amount = new SimpleStringProperty(); final StringProperty amount = new SimpleStringProperty();
StringProperty minAmount = new SimpleStringProperty(); final StringProperty minAmount = new SimpleStringProperty();
StringProperty price = new SimpleStringProperty(); final StringProperty price = new SimpleStringProperty();
StringProperty volume = new SimpleStringProperty(); final StringProperty volume = new SimpleStringProperty();
StringProperty collateral = new SimpleStringProperty(); final StringProperty collateral = new SimpleStringProperty();
StringProperty totals = new SimpleStringProperty(); final StringProperty totals = new SimpleStringProperty();
StringProperty direction = new SimpleStringProperty(); final StringProperty directionLabel = new SimpleStringProperty();
final StringProperty collateralLabel = new SimpleStringProperty();
final StringProperty feeLabel = new SimpleStringProperty();
BooleanProperty isOfferPlacedScreen = new SimpleBooleanProperty(); final StringProperty bankAccountType = new SimpleStringProperty();
final StringProperty bankAccountCurrency = new SimpleStringProperty();
final StringProperty bankAccountCounty = new SimpleStringProperty();
final StringProperty acceptedCountries = new SimpleStringProperty();
final StringProperty acceptedLanguages = new SimpleStringProperty();
final StringProperty transactionId = new SimpleStringProperty();
final BooleanProperty isOfferPlacedScreen = new SimpleBooleanProperty();
final BooleanProperty isPlaceOfferButtonDisabled = new SimpleBooleanProperty();
} }
public class CreateOfferController implements Initializable, ChildController, Hibernate public class CreateOfferController implements Initializable, ChildController, Hibernate
{ {
private static final Logger log = LoggerFactory.getLogger(CreateOfferController.class); private static final Logger log = LoggerFactory.getLogger(CreateOfferController.class);
private NavigationController navigationController;
private final TradeManager tradeManager; private final TradeManager tradeManager;
private final WalletFacade walletFacade; private final WalletFacade walletFacade;
private final Settings settings;
private final User user;
private final ViewModel viewModel = new ViewModel(); private final ViewModel viewModel = new ViewModel();
private final double collateral;
private Direction direction; private Direction direction;
private NavigationController navigationController; @FXML private AnchorPane rootContainer;
@FXML private Label buyLabel, confirmationLabel, txTitleLabel, collateralLabel;
@FXML @FXML private TextField volumeTextField, amountTextField, priceTextField, totalsTextField;
private AnchorPane rootContainer; @FXML private Button placeOfferButton, closeButton;
@FXML @FXML private TextField collateralTextField, minAmountTextField, bankAccountTypeTextField, bankAccountCurrencyTextField, bankAccountCountyTextField, acceptedCountriesTextField, acceptedLanguagesTextField,
private Label buyLabel, confirmationLabel, txTitleLabel, collateralLabel; feeLabel, transactionIdTextField;
@FXML @FXML private ConfidenceProgressIndicator progressIndicator;
private TextField volumeTextField, amountTextField, priceTextField, totalsTextField;
@FXML
private Button placeOfferButton, closeButton;
@FXML
private TextField collateralTextField, minAmountTextField, bankAccountTypeTextField, bankAccountCurrencyTextField, bankAccountCountyTextField, acceptedCountriesTextField,
acceptedLanguagesTextField, feeLabel, txTextField;
@FXML
private ConfidenceProgressIndicator progressIndicator;
@FXML private AddressTextField addressTextField; @FXML private AddressTextField addressTextField;
@FXML private BalanceTextField balanceTextField; @FXML private BalanceTextField balanceTextField;
@ -100,8 +100,19 @@ public class CreateOfferController implements Initializable, ChildController, Hi
{ {
this.tradeManager = tradeManager; this.tradeManager = tradeManager;
this.walletFacade = walletFacade; this.walletFacade = walletFacade;
this.settings = settings;
this.user = user; this.collateral = settings.getCollateral();
BankAccount bankAccount = user.getCurrentBankAccount();
if (bankAccount != null)
{
viewModel.bankAccountType.set(Localisation.get(bankAccount.getBankAccountType().toString()));
viewModel.bankAccountCurrency.set(bankAccount.getCurrency().getCurrencyCode());
viewModel.bankAccountCounty.set(bankAccount.getCountry().getName());
}
viewModel.acceptedCountries.set(BitSquareFormatter.countryLocalesToString(settings.getAcceptedCountries()));
viewModel.acceptedLanguages.set(BitSquareFormatter.languageLocalesToString(settings.getAcceptedLanguageLocales()));
viewModel.feeLabel.set(BitSquareFormatter.formatCoinWithCode(FeePolicy.CREATE_OFFER_FEE.add(FeePolicy.TX_FEE)));
} }
@ -113,24 +124,22 @@ public class CreateOfferController implements Initializable, ChildController, Hi
{ {
direction = orderBookFilter.getDirection(); direction = orderBookFilter.getDirection();
viewModel.direction.set(BitSquareFormatter.formatDirection(direction, false)); viewModel.directionLabel.set(BitSquareFormatter.formatDirection(direction, false) + ":");
viewModel.amount.set(BitSquareFormatter.formatCoin(orderBookFilter.getAmount())); viewModel.amount.set(BitSquareFormatter.formatCoin(orderBookFilter.getAmount()));
viewModel.minAmount.set(BitSquareFormatter.formatCoin(orderBookFilter.getAmount())); viewModel.minAmount.set(BitSquareFormatter.formatCoin(orderBookFilter.getAmount()));
viewModel.price.set(BitSquareFormatter.formatPrice(orderBookFilter.getPrice())); viewModel.price.set(BitSquareFormatter.formatPrice(orderBookFilter.getPrice()));
viewModel.collateralLabel.set("Collateral (" + BitSquareFormatter.formatCollateralPercent(collateral) + "):");
buyLabel.setText(viewModel.direction.get() + ":");
collateralLabel.setText("Collateral (" + BitSquareFormatter.formatCollateralPercent(settings.getCollateral()) + "):");
//TODO just for dev testing //TODO just for dev testing
if (BitSquare.fillFormsWithDummyData) if (BitSquare.fillFormsWithDummyData)
{ {
if (orderBookFilter.getAmount() != null) if (orderBookFilter.getAmount() == null)
{ {
amountTextField.setText("1"); amountTextField.setText("1");
minAmountTextField.setText("0.1"); minAmountTextField.setText("0.1");
} }
if (orderBookFilter.getPrice() != 0) if (orderBookFilter.getPrice() == 0)
priceTextField.setText("" + (int) (499 - new Random().nextDouble() * 1000 / 100)); priceTextField.setText("" + (int) (499 - new Random().nextDouble() * 1000 / 100));
} }
} }
@ -140,22 +149,10 @@ public class CreateOfferController implements Initializable, ChildController, Hi
// Interface implementation: Initializable // Interface implementation: Initializable
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@Override @Override
public void initialize(URL url, ResourceBundle rb) public void initialize(URL url, ResourceBundle rb)
{ {
// static data
BankAccount currentBankAccount = user.getCurrentBankAccount();
if (currentBankAccount != null)
{
bankAccountTypeTextField.setText(Localisation.get(currentBankAccount.getBankAccountType().toString()));
bankAccountCurrencyTextField.setText(currentBankAccount.getCurrency().getCurrencyCode());
bankAccountCountyTextField.setText(currentBankAccount.getCountry().getName());
}
acceptedCountriesTextField.setText(BitSquareFormatter.countryLocalesToString(settings.getAcceptedCountries()));
acceptedLanguagesTextField.setText(BitSquareFormatter.languageLocalesToString(settings.getAcceptedLanguageLocales()));
feeLabel.setText(BitSquareFormatter.formatCoinWithCode(FeePolicy.CREATE_OFFER_FEE.add(FeePolicy.TX_FEE)));
AddressEntry addressEntry = walletFacade.getUnusedTradeAddressInfo(); AddressEntry addressEntry = walletFacade.getUnusedTradeAddressInfo();
addressTextField.setAddress(addressEntry.getAddress().toString()); addressTextField.setAddress(addressEntry.getAddress().toString());
@ -165,35 +162,37 @@ public class CreateOfferController implements Initializable, ChildController, Hi
// setup bindings // setup bindings
DoubleBinding amountBinding = createDoubleBinding(() -> BitSquareConverter.stringToDouble(viewModel.amount.get()), viewModel.amount); DoubleBinding amountBinding = createDoubleBinding(() -> BitSquareConverter.stringToDouble(viewModel.amount.get()), viewModel.amount);
DoubleBinding priceBinding = createDoubleBinding(() -> BitSquareConverter.stringToDouble(viewModel.price.get()), viewModel.price); DoubleBinding priceBinding = createDoubleBinding(() -> BitSquareConverter.stringToDouble(viewModel.price.get()), viewModel.price);
viewModel.volume.bind(createStringBinding(() -> BitSquareFormatter.formatVolume(amountBinding.get() * priceBinding.get()), amountBinding, priceBinding)); viewModel.volume.bind(createStringBinding(() -> BitSquareFormatter.formatVolume(amountBinding.get() * priceBinding.get()), amountBinding, priceBinding));
viewModel.collateral.bind(createStringBinding(() -> BitSquareFormatter.formatCollateralAsBtc(viewModel.amount.get(), collateral), amountBinding));
viewModel.collateral.bind(createStringBinding(() -> { viewModel.totals.bind(createStringBinding(() -> BitSquareFormatter.formatTotalsAsBtc(viewModel.amount.get(), collateral, FeePolicy.CREATE_OFFER_FEE.add(FeePolicy.TX_FEE)), amountBinding, priceBinding));
Coin amountAsCoin = BitSquareFormatter.parseToCoin(viewModel.amount.get());
Coin collateralAsCoin = amountAsCoin.divide((long) (1d / settings.getCollateral()));
return BitSquareFormatter.formatCoinWithCode(collateralAsCoin);
}, amountBinding));
viewModel.totals.bind(createStringBinding(() -> {
Coin amountAsCoin = BitSquareFormatter.parseToCoin(viewModel.amount.get());
Coin collateralAsCoin = amountAsCoin.divide((long) (1d / settings.getCollateral()));
Coin totals = FeePolicy.CREATE_OFFER_FEE.add(collateralAsCoin).add(FeePolicy.TX_FEE);
return BitSquareFormatter.formatCoinWithCode(totals);
}, amountBinding, priceBinding));
// apply bindings to controls // apply bindings to controls
buyLabel.textProperty().bind(viewModel.directionLabel);
amountTextField.textProperty().bindBidirectional(viewModel.amount); amountTextField.textProperty().bindBidirectional(viewModel.amount);
priceTextField.textProperty().bindBidirectional(viewModel.price); priceTextField.textProperty().bindBidirectional(viewModel.price);
minAmountTextField.textProperty().bindBidirectional(viewModel.minAmount); minAmountTextField.textProperty().bindBidirectional(viewModel.minAmount);
volumeTextField.textProperty().bind(viewModel.volume); volumeTextField.textProperty().bind(viewModel.volume);
collateralLabel.textProperty().bind(viewModel.collateralLabel);
collateralTextField.textProperty().bind(viewModel.collateral); collateralTextField.textProperty().bind(viewModel.collateral);
totalsTextField.textProperty().bind(viewModel.totals); totalsTextField.textProperty().bind(viewModel.totals);
bankAccountTypeTextField.textProperty().bind(viewModel.bankAccountType);
bankAccountCurrencyTextField.textProperty().bind(viewModel.bankAccountCurrency);
bankAccountCountyTextField.textProperty().bind(viewModel.bankAccountCounty);
acceptedCountriesTextField.textProperty().bind(viewModel.acceptedCountries);
acceptedLanguagesTextField.textProperty().bind(viewModel.acceptedLanguages);
feeLabel.textProperty().bind(viewModel.feeLabel);
transactionIdTextField.textProperty().bind(viewModel.transactionId);
placeOfferButton.visibleProperty().bind(viewModel.isOfferPlacedScreen.not()); placeOfferButton.visibleProperty().bind(viewModel.isOfferPlacedScreen.not());
placeOfferButton.disableProperty().bind(viewModel.isPlaceOfferButtonDisabled);
progressIndicator.visibleProperty().bind(viewModel.isOfferPlacedScreen); progressIndicator.visibleProperty().bind(viewModel.isOfferPlacedScreen);
confirmationLabel.visibleProperty().bind(viewModel.isOfferPlacedScreen); confirmationLabel.visibleProperty().bind(viewModel.isOfferPlacedScreen);
txTitleLabel.visibleProperty().bind(viewModel.isOfferPlacedScreen); txTitleLabel.visibleProperty().bind(viewModel.isOfferPlacedScreen);
txTextField.visibleProperty().bind(viewModel.isOfferPlacedScreen); transactionIdTextField.visibleProperty().bind(viewModel.isOfferPlacedScreen);
closeButton.visibleProperty().bind(viewModel.isOfferPlacedScreen); closeButton.visibleProperty().bind(viewModel.isOfferPlacedScreen);
} }
@ -238,23 +237,19 @@ public class CreateOfferController implements Initializable, ChildController, Hi
{ {
if (inputsValid()) if (inputsValid())
{ {
placeOfferButton.setDisable(true); viewModel.isPlaceOfferButtonDisabled.set(true);
double price = BitSquareConverter.stringToDouble(viewModel.price.get());
Coin amount = BitSquareFormatter.parseToCoin(viewModel.amount.get());
Coin minAmount = BitSquareFormatter.parseToCoin(viewModel.minAmount.get());
tradeManager.requestPlaceOffer(direction, tradeManager.requestPlaceOffer(direction,
price, BitSquareConverter.stringToDouble(viewModel.price.get()),
amount, BitSquareFormatter.parseToCoin(viewModel.amount.get()),
minAmount, BitSquareFormatter.parseToCoin(viewModel.minAmount.get()),
(transaction) -> { (transaction) -> {
viewModel.isOfferPlacedScreen.set(true); viewModel.isOfferPlacedScreen.set(true);
txTextField.setText(transaction.getHashAsString()); viewModel.transactionId.set(transaction.getHashAsString());
}, },
errorMessage -> { errorMessage -> {
Popups.openErrorPopup("An error occurred", errorMessage); Popups.openErrorPopup("An error occurred", errorMessage);
placeOfferButton.setDisable(false); viewModel.isPlaceOfferButtonDisabled.set(false);
}); });
} }
} }
@ -276,7 +271,7 @@ public class CreateOfferController implements Initializable, ChildController, Hi
private boolean inputsValid() private boolean inputsValid()
{ {
//TODO //TODO
boolean inputFieldsValid; /* boolean inputFieldsValid;
double amount = BitSquareConverter.stringToDouble(viewModel.amount.get()); double amount = BitSquareConverter.stringToDouble(viewModel.amount.get());
double minAmount = BitSquareConverter.stringToDouble(viewModel.minAmount.get()); double minAmount = BitSquareConverter.stringToDouble(viewModel.minAmount.get());
double price = BitSquareConverter.stringToDouble(viewModel.price.get()); double price = BitSquareConverter.stringToDouble(viewModel.price.get());
@ -286,27 +281,27 @@ public class CreateOfferController implements Initializable, ChildController, Hi
minAmount > 0 && minAmount > 0 &&
minAmount <= amount/* && minAmount <= amount/* &&
viewModel.collateral >= settings.getMinCollateral() && viewModel.collateral >= settings.getMinCollateral() &&
viewModel.collateral <= settings.getMaxCollateral()*/; viewModel.collateral <= settings.getMaxCollateral()*/ /*;
if (!inputFieldsValid) if (!inputFieldsValid)
{ {
Popups.openWarningPopup("Invalid input", "Your input is invalid"); Popups.openWarningPopup("Invalid input", "Your input is invalid");
return false; return false;
} }
*/
/* Arbitrator arbitrator = settings.getRandomArbitrator(getAmountAsCoin()); /* Arbitrator arbitrator = settings.getRandomArbitrator(getAmountAsCoin());
if (arbitrator == null) if (arbitrator == null)
{ {
Popups.openWarningPopup("No arbitrator available", "No arbitrator from your arbitrator list does match the collateral and amount value."); Popups.openWarningPopup("No arbitrator available", "No arbitrator from your arbitrator list does match the collateral and amount value.");
return false; return false;
}*/ }*/
/*
if (user.getCurrentBankAccount() == null) if (user.getCurrentBankAccount() == null)
{ {
log.error("Must never happen!"); log.error("Must never happen!");
Popups.openWarningPopup("No bank account selected", "No bank account selected."); Popups.openWarningPopup("No bank account selected", "No bank account selected.");
return false; return false;
} }*/
return true; return true;
} }

View file

@ -69,7 +69,7 @@
<Button fx:id="placeOfferButton" defaultButton="true" onAction="#onPlaceOffer" text="Place offer" GridPane.columnIndex="1" GridPane.rowIndex="14"/> <Button fx:id="placeOfferButton" defaultButton="true" onAction="#onPlaceOffer" text="Place offer" GridPane.columnIndex="1" GridPane.rowIndex="14"/>
<Label fx:id="txTitleLabel" text="Transaction ID:" visible="false" GridPane.rowIndex="14"/> <Label fx:id="txTitleLabel" text="Transaction ID:" visible="false" GridPane.rowIndex="14"/>
<TextField fx:id="txTextField" visible="false" editable="false" GridPane.columnIndex="1" GridPane.rowIndex="14"/> <TextField fx:id="transactionIdTextField" visible="false" editable="false" GridPane.columnIndex="1" GridPane.rowIndex="14"/>
<ConfidenceProgressIndicator fx:id="progressIndicator" visible="false" progress="0" GridPane.columnIndex="2" GridPane.halignment="LEFT" <ConfidenceProgressIndicator fx:id="progressIndicator" visible="false" progress="0" GridPane.columnIndex="2" GridPane.halignment="LEFT"
GridPane.rowIndex="14" GridPane.rowSpan="2" GridPane.valignment="TOP"> GridPane.rowIndex="14" GridPane.rowSpan="2" GridPane.valignment="TOP">
<GridPane.margin> <GridPane.margin>

View file

@ -59,6 +59,7 @@ public class BitSquareFormatter
} }
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// FIAT // FIAT
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@ -84,6 +85,21 @@ public class BitSquareFormatter
// Other // Other
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
public static String formatCollateralAsBtc(String amount, double collateral)
{
Coin amountAsCoin = BitSquareFormatter.parseToCoin(amount);
Coin collateralAsCoin = amountAsCoin.divide((long) (1d / collateral));
return formatCoinWithCode(collateralAsCoin);
}
public static String formatTotalsAsBtc(String amount, double collateral, Coin fees)
{
Coin amountAsCoin = BitSquareFormatter.parseToCoin(amount);
Coin collateralAsCoin = amountAsCoin.divide((long) (1d / collateral));
Coin totals = collateralAsCoin.add(fees);
return formatCoinWithCode(totals);
}
public static String formatDirection(Direction direction, boolean allUpperCase) public static String formatDirection(Direction direction, boolean allUpperCase)
{ {
String result = (direction == Direction.BUY) ? "Buy" : "Sell"; String result = (direction == Direction.BUY) ? "Buy" : "Sell";