Add show all currency filter option in offerbook views

This commit is contained in:
Manfred Karrer 2016-02-18 15:42:26 +01:00
parent 485b33acf6
commit f5a61f9924
9 changed files with 106 additions and 79 deletions

View File

@ -41,10 +41,10 @@ public class CurrencyUtil {
set.addAll(getSortedSEPACurrencyCodes());
// PerfectMoney:
set.add(new TradeCurrency("USD"));
set.add(new FiatCurrency("USD"));
// Alipay:
set.add(new TradeCurrency("CNY"));
set.add(new FiatCurrency("CNY"));
// OKPay: We want to maintain the order so we don't use a Set but add items if nto already in list
getAllOKPayCurrencies().stream().forEach(set::add);

View File

@ -109,7 +109,10 @@ public final class PaymentMethod implements Persistable, Comparable {
@Override
public int compareTo(@NotNull Object other) {
return this.id.compareTo(((PaymentMethod) other).id);
if (id != null)
return this.id.compareTo(((PaymentMethod) other).id);
else
return 0;
}
@Override
@ -119,24 +122,24 @@ public final class PaymentMethod implements Persistable, Comparable {
PaymentMethod that = (PaymentMethod) o;
if (getLockTime() != that.getLockTime()) return false;
if (getMaxTradePeriod() != that.getMaxTradePeriod()) return false;
return !(getId() != null ? !getId().equals(that.getId()) : that.getId() != null);
if (lockTime != that.lockTime) return false;
if (maxTradePeriod != that.maxTradePeriod) return false;
return !(id != null ? !id.equals(that.id) : that.id != null);
}
@Override
public int hashCode() {
int result = getId() != null ? getId().hashCode() : 0;
result = 31 * result + (int) (getLockTime() ^ (getLockTime() >>> 32));
result = 31 * result + getMaxTradePeriod();
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (int) (lockTime ^ (lockTime >>> 32));
result = 31 * result + maxTradePeriod;
return result;
}
@Override
public String toString() {
return "PaymentMethod{" +
"name='" + id + '\'' +
"id='" + id + '\'' +
", lockTime=" + lockTime +
", waitPeriodForOpenDispute=" + maxTradePeriod +
'}';

View File

@ -24,6 +24,7 @@ import io.bitsquare.btc.FeePolicy;
import io.bitsquare.common.persistance.Persistable;
import io.bitsquare.locale.CountryUtil;
import io.bitsquare.locale.CurrencyUtil;
import io.bitsquare.locale.FiatCurrency;
import io.bitsquare.locale.TradeCurrency;
import io.bitsquare.storage.Storage;
import javafx.beans.Observable;
@ -78,7 +79,7 @@ public final class Preferences implements Persistable {
return defaultLocale;
}
private static TradeCurrency defaultTradeCurrency = new TradeCurrency(CurrencyUtil.getCurrencyByCountryCode(CountryUtil.getDefaultCountryCode()).getCurrency().getCurrencyCode());
private static TradeCurrency defaultTradeCurrency = new FiatCurrency(CurrencyUtil.getCurrencyByCountryCode(CountryUtil.getDefaultCountryCode()).getCurrency().getCurrencyCode());
public static TradeCurrency getDefaultTradeCurrency() {
return defaultTradeCurrency;

View File

@ -1,26 +0,0 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.gui.main.offer.offerbook;
import io.bitsquare.payment.PaymentMethod;
public class AllPaymentMethodsEntry extends PaymentMethod {
public AllPaymentMethodsEntry() {
super("All", 0, 0);
}
}

View File

@ -89,7 +89,10 @@ public class OfferBookView extends ActivatableViewAndModel<GridPane, OfferBookVi
currencyComboBox.setConverter(new StringConverter<TradeCurrency>() {
@Override
public String toString(TradeCurrency tradeCurrency) {
return tradeCurrency.getNameAndCode();
if (!tradeCurrency.getCode().equals(OfferBookViewModel.SHOW_ALL_FLAG))
return tradeCurrency.getNameAndCode();
else
return "Show all";
}
@Override
@ -103,7 +106,8 @@ public class OfferBookView extends ActivatableViewAndModel<GridPane, OfferBookVi
paymentMethodComboBox.setConverter(new StringConverter<PaymentMethod>() {
@Override
public String toString(PaymentMethod paymentMethod) {
return BSResources.get(paymentMethod.getId());
String id = paymentMethod.getId();
return BSResources.get(!id.equals(OfferBookViewModel.SHOW_ALL_FLAG) ? id : "Show all");
}
@Override
@ -152,7 +156,7 @@ public class OfferBookView extends ActivatableViewAndModel<GridPane, OfferBookVi
@Override
protected void activate() {
currencyComboBox.setItems(model.getTradeCurrencies());
currencyComboBox.getSelectionModel().select(model.getTradeCurrency());
currencyComboBox.getSelectionModel().select(model.getSelectedTradeCurrency());
currencyComboBox.setVisibleRowCount(Math.min(currencyComboBox.getItems().size(), 25));
paymentMethodComboBox.setItems(model.getPaymentMethods());
paymentMethodComboBox.getSelectionModel().select(0);
@ -160,8 +164,14 @@ public class OfferBookView extends ActivatableViewAndModel<GridPane, OfferBookVi
currencyComboBox.setOnAction(e -> model.onSetTradeCurrency(currencyComboBox.getSelectionModel().getSelectedItem()));
paymentMethodComboBox.setOnAction(e -> model.onSetPaymentMethod(paymentMethodComboBox.getSelectionModel().getSelectedItem()));
createOfferButton.setOnAction(e -> onCreateOffer());
priceColumn.textProperty().bind(createStringBinding(
() -> "Price in " + model.tradeCurrencyCode.get() + "/BTC", model.tradeCurrencyCode));
() -> !model.showAllTradeCurrenciesProperty.get() ?
"Price in " + model.tradeCurrencyCode.get() + "/BTC" :
"Price (mixed currencies)",
model.tradeCurrencyCode,
model.showAllTradeCurrenciesProperty));
volumeColumn.textProperty().bind(createStringBinding(
() -> "Amount in " + model.tradeCurrencyCode.get() + " (Min.)", model.tradeCurrencyCode));
model.getOfferList().comparatorProperty().bind(tableView.comparatorProperty());
@ -213,7 +223,8 @@ public class OfferBookView extends ActivatableViewAndModel<GridPane, OfferBookVi
showWarning("You don't have setup a payment account yet.",
"You need to setup your payment account before you can trade.\nDo you want to do this now?", PaymentAccountView.class);
} else if (!model.hasPaymentAccountForCurrency()) {
showWarning("You don't have a payment account with that selected currency.",
showWarning("You don't have a payment account for the currency:\n" +
model.getSelectedTradeCurrency().getCodeAndName(),
"You need to setup a payment account for the selected currency to be able to trade in that currency.\n" +
"Do you want to do this now?", PaymentAccountView.class);
} else if (!model.hasAcceptedArbitrators()) {
@ -222,7 +233,7 @@ public class OfferBookView extends ActivatableViewAndModel<GridPane, OfferBookVi
"Do you want to do this now?", ArbitratorSelectionView.class);
} else {
createOfferButton.setDisable(true);
offerActionHandler.onCreateOffer(model.getTradeCurrency());
offerActionHandler.onCreateOffer(model.getSelectedTradeCurrency());
}
}

View File

@ -24,10 +24,7 @@ import io.bitsquare.common.handlers.ErrorMessageHandler;
import io.bitsquare.common.handlers.ResultHandler;
import io.bitsquare.gui.common.model.ActivatableViewModel;
import io.bitsquare.gui.util.BSFormatter;
import io.bitsquare.locale.BSResources;
import io.bitsquare.locale.CountryUtil;
import io.bitsquare.locale.CurrencyUtil;
import io.bitsquare.locale.TradeCurrency;
import io.bitsquare.locale.*;
import io.bitsquare.p2p.NodeAddress;
import io.bitsquare.p2p.P2PService;
import io.bitsquare.payment.PaymentMethod;
@ -36,6 +33,8 @@ import io.bitsquare.trade.offer.Offer;
import io.bitsquare.trade.offer.OpenOfferManager;
import io.bitsquare.user.Preferences;
import io.bitsquare.user.User;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
@ -48,6 +47,8 @@ import java.util.List;
import java.util.Optional;
class OfferBookViewModel extends ActivatableViewModel {
final static String SHOW_ALL_FLAG = "XXX";
private final OpenOfferManager openOfferManager;
private final User user;
private final OfferBook offerBook;
@ -58,16 +59,24 @@ class OfferBookViewModel extends ActivatableViewModel {
private final FilteredList<OfferBookListItem> filteredItems;
private final SortedList<OfferBookListItem> sortedItems;
private TradeCurrency tradeCurrency;
private TradeCurrency selectedTradeCurrency;
private final ObservableList<TradeCurrency> allTradeCurrencies = FXCollections.observableArrayList();
private Offer.Direction direction;
private final StringProperty btcCode = new SimpleStringProperty();
final StringProperty tradeCurrencyCode = new SimpleStringProperty();
private PaymentMethod paymentMethod = new AllPaymentMethodsEntry();
// If id is empty string we ignore filter (display all methods)
private PaymentMethod selectedPaymentMethod = new PaymentMethod(SHOW_ALL_FLAG, 0, 0);
private final ObservableList<OfferBookListItem> offerBookListItems;
private final ListChangeListener<OfferBookListItem> listChangeListener;
private boolean isTabSelected;
final BooleanProperty showAllTradeCurrenciesProperty = new SimpleBooleanProperty();
private boolean showAllPaymentMethods = true;
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor, lifecycle
@ -93,12 +102,21 @@ class OfferBookViewModel extends ActivatableViewModel {
this.filteredItems = new FilteredList<>(offerBookListItems);
this.sortedItems = new SortedList<>(filteredItems);
tradeCurrency = CurrencyUtil.getDefaultTradeCurrency();
tradeCurrencyCode.set(tradeCurrency.getCode());
selectedTradeCurrency = CurrencyUtil.getDefaultTradeCurrency();
tradeCurrencyCode.set(selectedTradeCurrency.getCode());
preferences.getTradeCurrenciesAsObservable().addListener(new ListChangeListener<TradeCurrency>() {
@Override
public void onChanged(Change<? extends TradeCurrency> c) {
fillAllTradeCurrencies();
}
});
}
@Override
protected void activate() {
fillAllTradeCurrencies();
btcCode.bind(preferences.btcDenominationProperty());
offerBookListItems.addListener(listChangeListener);
offerBook.fillOfferBookListItems();
@ -113,6 +131,13 @@ class OfferBookViewModel extends ActivatableViewModel {
offerBookListItems.removeListener(listChangeListener);
}
private void fillAllTradeCurrencies() {
allTradeCurrencies.clear();
// Used for ignoring filter (show all)
TradeCurrency dummy = new FiatCurrency(SHOW_ALL_FLAG);
allTradeCurrencies.add(dummy);
allTradeCurrencies.addAll(preferences.getTradeCurrenciesAsObservable());
}
///////////////////////////////////////////////////////////////////////////////////////////
// API
@ -134,15 +159,22 @@ class OfferBookViewModel extends ActivatableViewModel {
///////////////////////////////////////////////////////////////////////////////////////////
public void onSetTradeCurrency(TradeCurrency tradeCurrency) {
this.tradeCurrency = tradeCurrency;
String code = tradeCurrency.getCode();
tradeCurrencyCode.set(code);
marketPriceFeed.setCurrencyCode(code);
showAllTradeCurrenciesProperty.set(isShowAllEntry(code));
if (!showAllTradeCurrenciesProperty.get()) {
this.selectedTradeCurrency = tradeCurrency;
tradeCurrencyCode.set(code);
marketPriceFeed.setCurrencyCode(code);
}
filterList();
}
public void onSetPaymentMethod(PaymentMethod paymentMethod) {
this.paymentMethod = paymentMethod;
showAllPaymentMethods = isShowAllEntry(paymentMethod.getId());
if (!showAllPaymentMethods)
this.selectedPaymentMethod = paymentMethod;
filterList();
}
@ -168,20 +200,20 @@ class OfferBookViewModel extends ActivatableViewModel {
}
public ObservableList<TradeCurrency> getTradeCurrencies() {
return preferences.getTradeCurrenciesAsObservable();
return allTradeCurrencies;
}
boolean isBootstrapped() {
return p2PService.isBootstrapped();
}
public TradeCurrency getTradeCurrency() {
return tradeCurrency;
public TradeCurrency getSelectedTradeCurrency() {
return selectedTradeCurrency;
}
public ObservableList<PaymentMethod> getPaymentMethods() {
ObservableList<PaymentMethod> list = FXCollections.observableArrayList(PaymentMethod.ALL_VALUES);
list.add(0, paymentMethod);
list.add(0, selectedPaymentMethod);
return list;
}
@ -192,7 +224,10 @@ class OfferBookViewModel extends ActivatableViewModel {
}
String getPrice(OfferBookListItem item) {
return (item != null) ? formatter.formatFiat(item.getOffer().getPrice()) : "";
if (showAllTradeCurrenciesProperty.get())
return (item != null) ? formatter.formatFiatWithCode(item.getOffer().getPrice()) : "";
else
return (item != null) ? formatter.formatFiat(item.getOffer().getPrice()) : "";
}
String getVolume(OfferBookListItem item) {
@ -271,7 +306,7 @@ class OfferBookViewModel extends ActivatableViewModel {
}
public boolean hasPaymentAccountForCurrency() {
return user.hasPaymentAccountForCurrency(tradeCurrency);
return user.hasPaymentAccountForCurrency(selectedTradeCurrency);
}
boolean hasAcceptedArbitrators() {
@ -286,11 +321,10 @@ class OfferBookViewModel extends ActivatableViewModel {
filteredItems.setPredicate(offerBookListItem -> {
Offer offer = offerBookListItem.getOffer();
boolean directionResult = offer.getDirection() != direction;
boolean currencyResult = offer.getCurrencyCode().equals(tradeCurrency.getCode());
boolean paymentMethodResult = true;
if (!(paymentMethod instanceof AllPaymentMethodsEntry))
paymentMethodResult = offer.getPaymentMethod().equals(paymentMethod);
boolean currencyResult = showAllTradeCurrenciesProperty.get() ||
offer.getCurrencyCode().equals(selectedTradeCurrency.getCode());
boolean paymentMethodResult = showAllPaymentMethods ||
offer.getPaymentMethod().equals(selectedPaymentMethod);
return directionResult && currencyResult && paymentMethodResult;
});
}
@ -308,4 +342,8 @@ class OfferBookViewModel extends ActivatableViewModel {
public boolean hasSameProtocolVersion(Offer offer) {
return offer.getProtocolVersion() == Version.TRADE_PROTOCOL_VERSION;
}
private boolean isShowAllEntry(String id) {
return id.equals(SHOW_ALL_FLAG);
}
}

View File

@ -33,6 +33,7 @@ import io.bitsquare.gui.common.model.ActivatableDataModel;
import io.bitsquare.gui.main.popups.Popup;
import io.bitsquare.gui.main.popups.WalletPasswordPopup;
import io.bitsquare.gui.util.BSFormatter;
import io.bitsquare.locale.CurrencyUtil;
import io.bitsquare.locale.TradeCurrency;
import io.bitsquare.payment.PaymentAccount;
import io.bitsquare.payment.PaymentMethod;
@ -356,8 +357,12 @@ class TakeOfferDataModel extends ActivatableDataModel {
return offer.getPaymentMethod();
}
public TradeCurrency getTradeCurrency() {
return new TradeCurrency(offer.getCurrencyCode());
public String getCurrencyCode() {
return offer.getCurrencyCode();
}
public String getCurrencyNameAndCode() {
return CurrencyUtil.getNameByCode(offer.getCurrencyCode());
}
public Coin getSecurityDepositAsCoin() {

View File

@ -150,12 +150,12 @@ public class TakeOfferView extends ActivatableViewAndModel<AnchorPane, TakeOffer
takeOfferSpinnerInfoLabel.visibleProperty().bind(model.isTakeOfferSpinnerVisible);
priceCurrencyLabel.textProperty().bind(createStringBinding(() ->
model.getTradeCurrency().getCode() + "/" + model.btcCode.get(), model.btcCode));
model.dataModel.getCurrencyCode() + "/" + model.btcCode.get(), model.btcCode));
volumeCurrencyLabel.setText(model.getTradeCurrency().getCode());
volumeCurrencyLabel.setText(model.dataModel.getCurrencyCode());
amountRangeBtcLabel.textProperty().bind(model.btcCode);
priceDescriptionLabel.setText(BSResources.get("createOffer.amountPriceBox.priceDescription", model.getTradeCurrency().getCode()));
priceDescriptionLabel.setText(BSResources.get("createOffer.amountPriceBox.priceDescription", model.dataModel.getCurrencyCode()));
volumeDescriptionLabel.setText(model.volumeDescriptionLabel.get());
errorPopupDisplayed = new SimpleBooleanProperty();
@ -321,7 +321,7 @@ public class TakeOfferView extends ActivatableViewAndModel<AnchorPane, TakeOffer
paymentMethodLabel.setManaged(!showComboBox);
if (!showComboBox)
paymentMethodTextField.setText(BSResources.get(model.getPaymentMethod().getId()));
currencyTextField.setText(model.getTradeCurrency().getNameAndCode());
currencyTextField.setText(model.dataModel.getCurrencyNameAndCode());
buyLabel.setText(model.getDirectionLabel());
amountDescriptionLabel.setText(model.getAmountDescription());
amountRangeTextField.setText(model.getAmountRange());

View File

@ -25,7 +25,6 @@ import io.bitsquare.gui.util.BSFormatter;
import io.bitsquare.gui.util.validation.BtcValidator;
import io.bitsquare.gui.util.validation.InputValidator;
import io.bitsquare.locale.BSResources;
import io.bitsquare.locale.TradeCurrency;
import io.bitsquare.p2p.P2PService;
import io.bitsquare.p2p.network.CloseConnectionReason;
import io.bitsquare.p2p.network.Connection;
@ -360,9 +359,9 @@ class TakeOfferViewModel extends ActivatableWithDataModel<TakeOfferDataModel> im
if (dataModel.getDirection() == Offer.Direction.BUY) {
volumeDescriptionLabel.set(BSResources.get("createOffer.amountPriceBox.buy.volumeDescription", dataModel.getTradeCurrency().getCode()));
volumeDescriptionLabel.set(BSResources.get("createOffer.amountPriceBox.buy.volumeDescription", dataModel.getCurrencyCode()));
} else {
volumeDescriptionLabel.set(BSResources.get("createOffer.amountPriceBox.sell.volumeDescription", dataModel.getTradeCurrency().getCode()));
volumeDescriptionLabel.set(BSResources.get("createOffer.amountPriceBox.sell.volumeDescription", dataModel.getCurrencyCode()));
}
totalToPay.bind(createStringBinding(() -> formatter.formatCoinWithCode(dataModel.totalToPayAsCoin.get()), dataModel.totalToPayAsCoin));
totalToPayAsCoin.bind(dataModel.totalToPayAsCoin);
@ -539,10 +538,6 @@ class TakeOfferViewModel extends ActivatableWithDataModel<TakeOfferDataModel> im
return dataModel.getPossiblePaymentAccounts();
}
public TradeCurrency getTradeCurrency() {
return dataModel.getTradeCurrency();
}
public List<Arbitrator> getArbitrators() {
return dataModel.getArbitrators();
}