Skip to content

Commit

Permalink
Merge pull request #6041 from xyzmaker123/5300-default-currencies
Browse files Browse the repository at this point in the history
Update currency filtering logic
  • Loading branch information
ripcurlx committed Mar 9, 2022
2 parents 0ea70dc + f2533b1 commit 43fb646
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* Implements searchable dropdown (an autocomplete like experience).
Expand All @@ -44,8 +45,9 @@
* @param <T> type of the ComboBox item; in the simplest case this can be a String
*/
public class AutocompleteComboBox<T> extends JFXComboBox<T> {
private ArrayList<T> completeList;
private ArrayList<T> matchingList;
private List<? extends T> list;
private List<? extends T> extendedList;
private List<T> matchingList;
private JFXComboBoxListViewSkin<T> comboBoxListViewSkin;

public AutocompleteComboBox() {
Expand All @@ -65,15 +67,20 @@ private AutocompleteComboBox(ObservableList<T> items) {
/**
* Set the complete list of ComboBox items. Use this instead of setItems().
*/
public void setAutocompleteItems(List<? extends T> items) {
completeList = new ArrayList<>(items);
matchingList = new ArrayList<>(completeList);
public void setAutocompleteItems(List<? extends T> items, List<? extends T> allItems) {
list = items;
extendedList = allItems;
matchingList = new ArrayList<>(list);
setValue(null);
getSelectionModel().clearSelection();
setItems(FXCollections.observableList(matchingList));
getEditor().setText("");
}

public void setAutocompleteItems(List<? extends T> items) {
setAutocompleteItems(items, null);
}

/**
* Triggered when value change is *confirmed*. In practical terms
* this is when user clicks item on the dropdown or hits [ENTER]
Expand Down Expand Up @@ -135,11 +142,11 @@ private void fixSpaceKey() {
}

private void filterBy(String query) {
ArrayList<T> newMatchingList = new ArrayList<>();
for (T item : completeList)
if (StringUtils.containsIgnoreCase(asString(item), query))
newMatchingList.add(item);
matchingList = newMatchingList;
matchingList = (extendedList != null && query.length() > 0 ? extendedList : list)
.stream()
.filter(item -> StringUtils.containsIgnoreCase(asString(item), query))
.collect(Collectors.toList());

setValue(null);
getSelectionModel().clearSelection();
setItems(FXCollections.observableList(matchingList));
Expand All @@ -153,7 +160,7 @@ private void reactToQueryChanges() {
getEditor().addEventHandler(KeyEvent.KEY_RELEASED, (KeyEvent event) -> {
UserThread.execute(() -> {
String query = getEditor().getText();
var exactMatch = completeList.stream().anyMatch(item -> asString(item).equalsIgnoreCase(query));
var exactMatch = list.stream().anyMatch(item -> asString(item).equalsIgnoreCase(query));
if (!exactMatch) {
if (query.isEmpty())
removeFilter();
Expand All @@ -166,7 +173,7 @@ private void reactToQueryChanges() {
}

private void removeFilter() {
matchingList = new ArrayList<>(completeList);
matchingList = new ArrayList<>(list);
setValue(null);
getSelectionModel().clearSelection();
setItems(FXCollections.observableList(matchingList));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ protected void activate() {
currencyComboBox.setConverter(new CurrencyStringConverter(currencyComboBox));
currencyComboBox.getEditor().getStyleClass().add("combo-box-editor-bold");

currencyComboBox.setAutocompleteItems(model.getTradeCurrencies());
currencyComboBox.setAutocompleteItems(model.getTradeCurrencies(), model.getAllCurrencies());
currencyComboBox.setVisibleRowCount(Math.min(currencyComboBox.getItems().size(), 10));

currencyComboBox.setOnChangeConfirmed(e -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand All @@ -121,7 +122,10 @@ class OfferBookViewModel extends ActivatableViewModel {
private final ListChangeListener<TradeCurrency> tradeCurrencyListChangeListener;
private final ListChangeListener<OfferBookListItem> filterItemsListener;
private TradeCurrency selectedTradeCurrency;
private final ObservableList<TradeCurrency> allTradeCurrencies = FXCollections.observableArrayList();
@Getter
private final ObservableList<TradeCurrency> tradeCurrencies = FXCollections.observableArrayList();
@Getter
private final ObservableList<TradeCurrency> allCurrencies = FXCollections.observableArrayList();

private OfferDirection direction;

Expand Down Expand Up @@ -187,7 +191,7 @@ public OfferBookViewModel(User user,
this.coreApi = coreApi;
this.sortedItems = new SortedList<>(filteredItems);

tradeCurrencyListChangeListener = c -> fillAllTradeCurrencies();
tradeCurrencyListChangeListener = c -> fillCurrencies();

filterItemsListener = c -> {
final Optional<OfferBookListItem> highestAmountOffer = filteredItems.stream()
Expand Down Expand Up @@ -234,7 +238,7 @@ protected void activate() {
}
useOffersMatchingMyAccountsFilter = !disableMatchToggle.get() && isShowOffersMatchingMyAccounts();

fillAllTradeCurrencies();
fillCurrencies();
preferences.getTradeCurrenciesAsObservable().addListener(tradeCurrencyListChangeListener);
offerBook.fillOfferBookListItems();
filterOffers();
Expand Down Expand Up @@ -305,7 +309,7 @@ void onSetPaymentMethod(PaymentMethod paymentMethod) {
// If we select TransferWise we switch to show all currencies as TransferWise supports
// sending to most currencies.
if (paymentMethod.getId().equals(PaymentMethod.TRANSFERWISE_ID)) {
onSetTradeCurrency(getShowAllEntryForCurrency());
onSetTradeCurrency(new CryptoCurrency(GUIUtil.SHOW_ALL_FLAG, ""));
}
} else {
this.selectedPaymentMethod = getShowAllEntryForPaymentMethod();
Expand Down Expand Up @@ -353,10 +357,6 @@ OfferDirection getDirection() {
return direction;
}

public ObservableList<TradeCurrency> getTradeCurrencies() {
return allTradeCurrencies;
}

boolean isBootstrappedOrShowPopup() {
return GUIUtil.isBootstrappedOrShowPopup(p2PService);
}
Expand Down Expand Up @@ -549,12 +549,18 @@ private void setMarketPriceFeedCurrency() {
}
}

private void fillAllTradeCurrencies() {
allTradeCurrencies.clear();
private void fillCurrencies() {
tradeCurrencies.clear();
// Used for ignoring filter (show all)
allTradeCurrencies.add(getShowAllEntryForCurrency());
allTradeCurrencies.addAll(preferences.getTradeCurrenciesAsObservable());
allTradeCurrencies.add(getEditEntryForCurrency());
tradeCurrencies.add(new CryptoCurrency(GUIUtil.SHOW_ALL_FLAG, ""));
tradeCurrencies.addAll(preferences.getTradeCurrenciesAsObservable());
tradeCurrencies.add(new CryptoCurrency(GUIUtil.EDIT_FLAG, ""));

allCurrencies.clear();
allCurrencies.add(new CryptoCurrency(GUIUtil.SHOW_ALL_FLAG, ""));
allCurrencies.addAll(CurrencyUtil.getAllSortedFiatCurrencies());
allCurrencies.addAll(CurrencyUtil.getAllSortedCryptoCurrencies());
allCurrencies.add(new CryptoCurrency(GUIUtil.EDIT_FLAG, ""));
}


Expand Down Expand Up @@ -663,14 +669,6 @@ public String formatDepositString(Coin deposit, long amount) {
return btcFormatter.formatCoin(deposit) + " (" + percentage + ")";
}

private TradeCurrency getShowAllEntryForCurrency() {
return new CryptoCurrency(GUIUtil.SHOW_ALL_FLAG, "");
}

private TradeCurrency getEditEntryForCurrency() {
return new CryptoCurrency(GUIUtil.EDIT_FLAG, "");
}

PaymentMethod getShowAllEntryForPaymentMethod() {
return PaymentMethod.getDummyPaymentMethod(GUIUtil.SHOW_ALL_FLAG);
}
Expand Down

0 comments on commit 43fb646

Please sign in to comment.