From 462eaec85c7df49a1e223f9ecc1acd0b5e554c2a Mon Sep 17 00:00:00 2001 From: xyzmaker123 Date: Thu, 3 Mar 2022 17:59:20 +0100 Subject: [PATCH 1/2] Currency filtering - possibility to search for all currencies --- .../components/AutocompleteComboBox.java | 31 ++++++++------ .../main/offer/offerbook/OfferBookView.java | 2 +- .../offer/offerbook/OfferBookViewModel.java | 40 +++++++++---------- 3 files changed, 39 insertions(+), 34 deletions(-) diff --git a/desktop/src/main/java/bisq/desktop/components/AutocompleteComboBox.java b/desktop/src/main/java/bisq/desktop/components/AutocompleteComboBox.java index 716f81dc0c9..6113dfebc52 100644 --- a/desktop/src/main/java/bisq/desktop/components/AutocompleteComboBox.java +++ b/desktop/src/main/java/bisq/desktop/components/AutocompleteComboBox.java @@ -35,6 +35,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** * Implements searchable dropdown (an autocomplete like experience). @@ -44,8 +45,9 @@ * @param type of the ComboBox item; in the simplest case this can be a String */ public class AutocompleteComboBox extends JFXComboBox { - private ArrayList completeList; - private ArrayList matchingList; + private List list; + private List extendedList; + private List matchingList; private JFXComboBoxListViewSkin comboBoxListViewSkin; public AutocompleteComboBox() { @@ -65,15 +67,20 @@ private AutocompleteComboBox(ObservableList items) { /** * Set the complete list of ComboBox items. Use this instead of setItems(). */ - public void setAutocompleteItems(List items) { - completeList = new ArrayList<>(items); - matchingList = new ArrayList<>(completeList); + public void setAutocompleteItems(List items, List allItems) { + list = items; + extendedList = allItems; + matchingList = new ArrayList<>(list); setValue(null); getSelectionModel().clearSelection(); setItems(FXCollections.observableList(matchingList)); getEditor().setText(""); } + public void setAutocompleteItems(List 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] @@ -135,11 +142,11 @@ private void fixSpaceKey() { } private void filterBy(String query) { - ArrayList newMatchingList = new ArrayList<>(); - for (T item : completeList) - if (StringUtils.containsIgnoreCase(asString(item), query)) - newMatchingList.add(item); - matchingList = newMatchingList; + matchingList = (extendedList != null && query.length() > 1 ? extendedList : list) + .stream() + .filter(item -> StringUtils.containsIgnoreCase(asString(item), query)) + .collect(Collectors.toList()); + setValue(null); getSelectionModel().clearSelection(); setItems(FXCollections.observableList(matchingList)); @@ -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(); @@ -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)); diff --git a/desktop/src/main/java/bisq/desktop/main/offer/offerbook/OfferBookView.java b/desktop/src/main/java/bisq/desktop/main/offer/offerbook/OfferBookView.java index 894719ae3b5..2065068f8ef 100644 --- a/desktop/src/main/java/bisq/desktop/main/offer/offerbook/OfferBookView.java +++ b/desktop/src/main/java/bisq/desktop/main/offer/offerbook/OfferBookView.java @@ -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 -> { diff --git a/desktop/src/main/java/bisq/desktop/main/offer/offerbook/OfferBookViewModel.java b/desktop/src/main/java/bisq/desktop/main/offer/offerbook/OfferBookViewModel.java index 338a97c3897..beaa0b6ca9c 100644 --- a/desktop/src/main/java/bisq/desktop/main/offer/offerbook/OfferBookViewModel.java +++ b/desktop/src/main/java/bisq/desktop/main/offer/offerbook/OfferBookViewModel.java @@ -95,6 +95,7 @@ import java.util.function.Predicate; import java.util.stream.Collectors; +import lombok.Getter; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -121,7 +122,10 @@ class OfferBookViewModel extends ActivatableViewModel { private final ListChangeListener tradeCurrencyListChangeListener; private final ListChangeListener filterItemsListener; private TradeCurrency selectedTradeCurrency; - private final ObservableList allTradeCurrencies = FXCollections.observableArrayList(); + @Getter + private final ObservableList tradeCurrencies = FXCollections.observableArrayList(); + @Getter + private final ObservableList allCurrencies = FXCollections.observableArrayList(); private OfferDirection direction; @@ -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 highestAmountOffer = filteredItems.stream() @@ -234,7 +238,7 @@ protected void activate() { } useOffersMatchingMyAccountsFilter = !disableMatchToggle.get() && isShowOffersMatchingMyAccounts(); - fillAllTradeCurrencies(); + fillCurrencies(); preferences.getTradeCurrenciesAsObservable().addListener(tradeCurrencyListChangeListener); offerBook.fillOfferBookListItems(); filterOffers(); @@ -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(); @@ -353,10 +357,6 @@ OfferDirection getDirection() { return direction; } - public ObservableList getTradeCurrencies() { - return allTradeCurrencies; - } - boolean isBootstrappedOrShowPopup() { return GUIUtil.isBootstrappedOrShowPopup(p2PService); } @@ -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, "")); } @@ -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); } From f2533b1772198863927a724daf321614bce02b37 Mon Sep 17 00:00:00 2001 From: xyzmaker123 Date: Tue, 8 Mar 2022 11:13:29 +0100 Subject: [PATCH 2/2] Currency filtering - fix logic --- .../main/java/bisq/desktop/components/AutocompleteComboBox.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/desktop/src/main/java/bisq/desktop/components/AutocompleteComboBox.java b/desktop/src/main/java/bisq/desktop/components/AutocompleteComboBox.java index 6113dfebc52..44d1015b684 100644 --- a/desktop/src/main/java/bisq/desktop/components/AutocompleteComboBox.java +++ b/desktop/src/main/java/bisq/desktop/components/AutocompleteComboBox.java @@ -142,7 +142,7 @@ private void fixSpaceKey() { } private void filterBy(String query) { - matchingList = (extendedList != null && query.length() > 1 ? extendedList : list) + matchingList = (extendedList != null && query.length() > 0 ? extendedList : list) .stream() .filter(item -> StringUtils.containsIgnoreCase(asString(item), query)) .collect(Collectors.toList());