From a5aeadfbb0df5d2e6d2e14ecb39e87aed0d7303d Mon Sep 17 00:00:00 2001 From: walkerp07 Date: Sun, 23 Jun 2024 04:40:25 -0400 Subject: [PATCH] Follow-up for https://github.com/haveno-dex/haveno/pull/1030 --- .../java/haveno/core/locale/CurrencyUtil.java | 8 ++++++- .../haveno/core/locale/TradeCurrency.java | 15 ++++++++++++- .../core/locale/TraditionalCurrency.java | 17 ++++++++++++-- .../haveno/core/payment/PaymentAccount.java | 22 ++++++++++++++++--- .../java/haveno/core/user/Preferences.java | 21 ++++++++++++++++++ .../paymentmethods/PaymentMethodForm.java | 5 ++++- .../TraditionalAccountsDataModel.java | 8 ++++++- .../main/offer/MutableOfferDataModel.java | 9 ++++++++ .../desktop/main/offer/MutableOfferView.java | 5 ++++- .../offerbook/XmrOfferBookViewModel.java | 5 ++++- proto/src/main/proto/pb.proto | 5 +++-- 11 files changed, 107 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/haveno/core/locale/CurrencyUtil.java b/core/src/main/java/haveno/core/locale/CurrencyUtil.java index b482ea4f284..cedb1c60cfc 100644 --- a/core/src/main/java/haveno/core/locale/CurrencyUtil.java +++ b/core/src/main/java/haveno/core/locale/CurrencyUtil.java @@ -150,6 +150,12 @@ public static List getMainFiatCurrencies() { list.add(new TraditionalCurrency("RUB")); list.add(new TraditionalCurrency("INR")); list.add(new TraditionalCurrency("NGN")); + list.add(new TraditionalCurrency("CNY")); + list.add(new TraditionalCurrency("JPY")); + list.add(new TraditionalCurrency("BRL")); + list.add(new TraditionalCurrency("THB")); + list.add(new TraditionalCurrency("SEK")); + list.add(new TraditionalCurrency("DKK")); postProcessTraditionalCurrenciesList(list); return list; } @@ -271,7 +277,7 @@ public static boolean isVolumeRoundedToNearestUnit(String currencyCode) { public static boolean isPricePrecise(String currencyCode) { return isCryptoCurrency(currencyCode) || - "XAU".equals(currencyCode.toUpperCase()) || + "XAU".equals(currencyCode.toUpperCase()) || "XAG".equals(currencyCode.toUpperCase()); } diff --git a/core/src/main/java/haveno/core/locale/TradeCurrency.java b/core/src/main/java/haveno/core/locale/TradeCurrency.java index a9da96ea169..eceea6c5ef5 100644 --- a/core/src/main/java/haveno/core/locale/TradeCurrency.java +++ b/core/src/main/java/haveno/core/locale/TradeCurrency.java @@ -21,6 +21,7 @@ import haveno.common.proto.persistable.PersistablePayload; import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.Setter; import lombok.ToString; import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; @@ -33,10 +34,21 @@ public abstract class TradeCurrency implements PersistablePayload, Comparable 1; + return getSelectedTradeCurrencies().size() > 1; } public void setSingleTradeCurrency(TradeCurrency tradeCurrency) { @@ -235,7 +235,10 @@ public void setSingleTradeCurrency(TradeCurrency tradeCurrency) { @Nullable public TradeCurrency getSingleTradeCurrency() { - if (tradeCurrencies.size() == 1) + List currencies = getSelectedTradeCurrencies(); + if (currencies.size() == 1) + return currencies.get(0); + else if (tradeCurrencies.size() == 1) return tradeCurrencies.get(0); else return null; @@ -275,6 +278,18 @@ public boolean hasPaymentMethodWithId(String paymentMethodId) { return this.getPaymentMethod().getId().equals(paymentMethodId); } + public TradeCurrency getTradeCurrencyByCode(String code) { + return tradeCurrencies.stream() + .filter(o1 -> o1.getCode().equals(code)) + .findFirst() + .orElse(null); // This should never be null. + } + + private List getSelectedTradeCurrencies() { + return tradeCurrencies.stream() + .filter(TradeCurrency::getSelected) + .toList(); + } /** * Return an Optional of the trade currency for this payment account, or * Optional.empty() if none is found. If this payment account has a selected @@ -285,12 +300,13 @@ public boolean hasPaymentMethodWithId(String paymentMethodId) { * @return Optional of the trade currency for the given payment account */ public Optional getTradeCurrency() { + List currencies = getSelectedTradeCurrencies(); if (this.getSelectedTradeCurrency() != null) return Optional.of(this.getSelectedTradeCurrency()); else if (this.getSingleTradeCurrency() != null) return Optional.of(this.getSingleTradeCurrency()); else if (!this.getTradeCurrencies().isEmpty()) - return Optional.of(this.getTradeCurrencies().get(0)); + return Optional.of(currencies.get(0)); else return Optional.empty(); } diff --git a/core/src/main/java/haveno/core/user/Preferences.java b/core/src/main/java/haveno/core/user/Preferences.java index 97e7fc67d6e..a4b40c2b8ea 100644 --- a/core/src/main/java/haveno/core/user/Preferences.java +++ b/core/src/main/java/haveno/core/user/Preferences.java @@ -214,6 +214,27 @@ private void initFromPersistedPreferences(PreferencesPayload persisted) { setupPreferences(); } + public void onSinglePaymentAccountCurrienciesSelected(TraditionalCurrency currency) { + prefPayload.setBuyScreenCurrencyCode(currency.getCode()); + prefPayload.setSellScreenCurrencyCode(currency.getCode()); + setPreferredTradeCurrency(currency); + } + + public void onPaymentAccountCurrienciesSelected(List currencies) { + List availableCurrencies = currencies.stream() + .filter(o1 -> o1 instanceof TraditionalCurrency) + .map(o1 -> (TraditionalCurrency) o1) + .distinct() + .sorted((o1, o2) -> { // Give priority to selected currencies. + if (o1.getSelected() && !o2.getSelected()) return -1; + else if (!o1.getSelected() && o2.getSelected()) return 1; + else return 0; + }) + .collect(Collectors.toList()); + setTraditionalCurrencies(availableCurrencies); + onSinglePaymentAccountCurrienciesSelected(availableCurrencies.get(0)); + } + private void initNewPreferences() { prefPayload = new PreferencesPayload(); prefPayload.setUserLanguage(GlobalSettings.getLocale().getLanguage()); diff --git a/desktop/src/main/java/haveno/desktop/components/paymentmethods/PaymentMethodForm.java b/desktop/src/main/java/haveno/desktop/components/paymentmethods/PaymentMethodForm.java index 64d0066d5ec..10d26c64182 100644 --- a/desktop/src/main/java/haveno/desktop/components/paymentmethods/PaymentMethodForm.java +++ b/desktop/src/main/java/haveno/desktop/components/paymentmethods/PaymentMethodForm.java @@ -293,11 +293,14 @@ void fillUpFlowPaneWithCurrencies(boolean isEditable, FlowPane flowPane, TradeCurrency e, PaymentAccount paymentAccount) { CheckBox checkBox = new AutoTooltipCheckBox(e.getCode()); checkBox.setMouseTransparent(!isEditable); - checkBox.setSelected(paymentAccount.getTradeCurrencies().contains(e)); + checkBox.setSelected(paymentAccount.getTradeCurrencies().contains(e) + && paymentAccount.getTradeCurrencyByCode(e.getCode()).getSelected()); checkBox.setMinWidth(60); checkBox.setMaxWidth(checkBox.getMinWidth()); checkBox.setTooltip(new Tooltip(e.getName())); checkBox.setOnAction(event -> { + e.setSelected(checkBox.isSelected()); + if (checkBox.isSelected()) paymentAccount.addCurrency(e); else diff --git a/desktop/src/main/java/haveno/desktop/main/account/content/traditionalaccounts/TraditionalAccountsDataModel.java b/desktop/src/main/java/haveno/desktop/main/account/content/traditionalaccounts/TraditionalAccountsDataModel.java index d34857a15e6..312f29f0982 100644 --- a/desktop/src/main/java/haveno/desktop/main/account/content/traditionalaccounts/TraditionalAccountsDataModel.java +++ b/desktop/src/main/java/haveno/desktop/main/account/content/traditionalaccounts/TraditionalAccountsDataModel.java @@ -108,11 +108,14 @@ public void onSaveNewAccount(PaymentAccount paymentAccount) { TradeCurrency singleTradeCurrency = paymentAccount.getSingleTradeCurrency(); List tradeCurrencies = paymentAccount.getTradeCurrencies(); if (singleTradeCurrency != null) { - if (singleTradeCurrency instanceof TraditionalCurrency) + if (singleTradeCurrency instanceof TraditionalCurrency) { preferences.addTraditionalCurrency((TraditionalCurrency) singleTradeCurrency); + preferences.onSinglePaymentAccountCurrienciesSelected((TraditionalCurrency) singleTradeCurrency); + } else preferences.addCryptoCurrency((CryptoCurrency) singleTradeCurrency); } else if (tradeCurrencies != null && !tradeCurrencies.isEmpty()) { + singleTradeCurrency = tradeCurrencies.getFirst(); if (tradeCurrencies.contains(CurrencyUtil.getDefaultTradeCurrency())) paymentAccount.setSelectedTradeCurrency(CurrencyUtil.getDefaultTradeCurrency()); else @@ -124,6 +127,9 @@ public void onSaveNewAccount(PaymentAccount paymentAccount) { else preferences.addCryptoCurrency((CryptoCurrency) tradeCurrency); }); + + if (singleTradeCurrency instanceof TraditionalCurrency) // Assuming Traditional and Crypto currencies are never mixed + preferences.onPaymentAccountCurrienciesSelected(tradeCurrencies); } user.addPaymentAccount(paymentAccount); diff --git a/desktop/src/main/java/haveno/desktop/main/offer/MutableOfferDataModel.java b/desktop/src/main/java/haveno/desktop/main/offer/MutableOfferDataModel.java index f94ccbd3f70..b07e985144d 100644 --- a/desktop/src/main/java/haveno/desktop/main/offer/MutableOfferDataModel.java +++ b/desktop/src/main/java/haveno/desktop/main/offer/MutableOfferDataModel.java @@ -26,6 +26,7 @@ import haveno.core.account.witness.AccountAgeWitnessService; import haveno.core.locale.CurrencyUtil; import haveno.core.locale.TradeCurrency; +import haveno.core.locale.TraditionalCurrency; import haveno.core.monetary.Price; import haveno.core.monetary.Volume; import haveno.core.offer.CreateOfferService; @@ -303,6 +304,14 @@ void onPlaceOffer(Offer offer, TransactionResultHandler resultHandler, ErrorMess void onPaymentAccountSelected(PaymentAccount paymentAccount) { if (paymentAccount != null && !this.paymentAccount.equals(paymentAccount)) { preferences.setSelectedPaymentAccountForCreateOffer(paymentAccount); + + TradeCurrency singleTradeCurrency = paymentAccount.getTradeCurrencies().getFirst(); + if (singleTradeCurrency instanceof TraditionalCurrency) { + preferences.onPaymentAccountCurrienciesSelected(paymentAccount.getSupportedCurrencies()); + user.requestPersistence(); + paymentAccount.onPersistChanges(); + } + this.paymentAccount = paymentAccount; setTradeCurrencyFromPaymentAccount(paymentAccount); diff --git a/desktop/src/main/java/haveno/desktop/main/offer/MutableOfferView.java b/desktop/src/main/java/haveno/desktop/main/offer/MutableOfferView.java index 53df033ed19..c31f9b4a7e2 100644 --- a/desktop/src/main/java/haveno/desktop/main/offer/MutableOfferView.java +++ b/desktop/src/main/java/haveno/desktop/main/offer/MutableOfferView.java @@ -101,6 +101,7 @@ import java.util.HashMap; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import static haveno.desktop.main.offer.OfferViewUtil.addPayInfoEntry; import static haveno.desktop.util.FormBuilder.add2ButtonsAfterGroup; @@ -499,7 +500,9 @@ protected void onPaymentAccountsComboBoxSelected() { model.onCurrencySelected(model.getTradeCurrency()); if (paymentAccount.hasMultipleCurrencies()) { - final List tradeCurrencies = paymentAccount.getTradeCurrencies(); + final List tradeCurrencies = paymentAccount.getTradeCurrencies().stream() + .filter(TradeCurrency::getSelected) + .collect(Collectors.toList()); currencyComboBox.setItems(FXCollections.observableArrayList(tradeCurrencies)); currencyComboBox.getSelectionModel().select(model.getTradeCurrency()); } else { diff --git a/desktop/src/main/java/haveno/desktop/main/offer/offerbook/XmrOfferBookViewModel.java b/desktop/src/main/java/haveno/desktop/main/offer/offerbook/XmrOfferBookViewModel.java index 38702cde906..e5327e2bd1b 100644 --- a/desktop/src/main/java/haveno/desktop/main/offer/offerbook/XmrOfferBookViewModel.java +++ b/desktop/src/main/java/haveno/desktop/main/offer/offerbook/XmrOfferBookViewModel.java @@ -143,7 +143,10 @@ TradeCurrency getDefaultTradeCurrency() { } else { return CurrencyUtil.getMainTraditionalCurrencies().stream().sorted((o1, o2) -> Boolean.compare(!hasPaymentAccountForCurrency(o1), - !hasPaymentAccountForCurrency(o2))).collect(Collectors.toList()).get(0); + !hasPaymentAccountForCurrency(o2))) + .filter(TradeCurrency::getSelected) // This assumes there will ALWAYS be a default "selected" currency + .collect(Collectors.toList()) + .get(0); } } diff --git a/proto/src/main/proto/pb.proto b/proto/src/main/proto/pb.proto index 8e988bbe571..10db2c03f07 100644 --- a/proto/src/main/proto/pb.proto +++ b/proto/src/main/proto/pb.proto @@ -1807,9 +1807,10 @@ message Currency { message TradeCurrency { string code = 1; string name = 2; + bool selected = 3; oneof message { - CryptoCurrency crypto_currency = 3; - TraditionalCurrency traditional_currency = 4; + CryptoCurrency crypto_currency = 4; + TraditionalCurrency traditional_currency = 5; } }