Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow-up for https://github.com/haveno-dex/haveno/pull/1030 #1065

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion core/src/main/java/haveno/core/locale/CurrencyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ public static List<TraditionalCurrency> 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;
}
Expand Down Expand Up @@ -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());
}

Expand Down
15 changes: 14 additions & 1 deletion core/src/main/java/haveno/core/locale/TradeCurrency.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,10 +34,21 @@ public abstract class TradeCurrency implements PersistablePayload, Comparable<Tr
protected final String code;
@EqualsAndHashCode.Exclude
protected final String name;
@EqualsAndHashCode.Exclude
@Getter
@Setter
protected Boolean selected;

public TradeCurrency(String code, String name, @NotNull Boolean selected) {
this.code = code;
this.name = name;
this.selected = selected;
}

public TradeCurrency(String code, String name) {
this.code = code;
this.name = name;
this.selected = false;
}

///////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -57,7 +69,8 @@ public static TradeCurrency fromProto(protobuf.TradeCurrency proto) {
public protobuf.TradeCurrency.Builder getTradeCurrencyBuilder() {
return protobuf.TradeCurrency.newBuilder()
.setCode(code)
.setName(name);
.setName(name)
.setSelected(selected);
}


Expand Down
17 changes: 15 additions & 2 deletions core/src/main/java/haveno/core/locale/TraditionalCurrency.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,31 @@ public TraditionalCurrency(String currencyCode) {
this(Currency.getInstance(currencyCode), getLocale());
}

public TraditionalCurrency(String currencyCode, Boolean selected) {
this(Currency.getInstance(currencyCode), getLocale(), selected);
}

public TraditionalCurrency(String currencyCode, String name, Boolean selected) {
super(currencyCode, name, selected);
}

public TraditionalCurrency(String currencyCode, String name) {
super(currencyCode, name);
}

public TraditionalCurrency(TraditionalCurrency currency) {
this(currency.getCode(), currency.getName());
this(currency.getCode(), currency.getName(), currency.getSelected());
}

@SuppressWarnings("WeakerAccess")
public TraditionalCurrency(Currency currency) {
this(currency, getLocale());
}

public TraditionalCurrency(Currency currency, Locale locale, Boolean selected) {
super(currency.getCurrencyCode(), currency.getDisplayName(locale), selected);
}

@SuppressWarnings("WeakerAccess")
public TraditionalCurrency(Currency currency, Locale locale) {
super(currency.getCurrencyCode(), currency.getDisplayName(locale));
Expand All @@ -83,12 +95,13 @@ public Message toProtoMessage() {
return getTradeCurrencyBuilder()
.setCode(code)
.setName(name)
.setSelected(getSelected())
.setTraditionalCurrency(protobuf.TraditionalCurrency.newBuilder())
.build();
}

public static TraditionalCurrency fromProto(protobuf.TradeCurrency proto) {
return new TraditionalCurrency(proto.getCode(), proto.getName());
return new TraditionalCurrency(proto.getCode(), proto.getName(), proto.getSelected());
}


Expand Down
22 changes: 19 additions & 3 deletions core/src/main/java/haveno/core/payment/PaymentAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public void removeCurrency(TradeCurrency tradeCurrency) {
}

public boolean hasMultipleCurrencies() {
return tradeCurrencies.size() > 1;
return getSelectedTradeCurrencies().size() > 1;
}

public void setSingleTradeCurrency(TradeCurrency tradeCurrency) {
Expand All @@ -235,7 +235,10 @@ public void setSingleTradeCurrency(TradeCurrency tradeCurrency) {

@Nullable
public TradeCurrency getSingleTradeCurrency() {
if (tradeCurrencies.size() == 1)
List<TradeCurrency> currencies = getSelectedTradeCurrencies();
if (currencies.size() == 1)
return currencies.get(0);
else if (tradeCurrencies.size() == 1)
return tradeCurrencies.get(0);
else
return null;
Expand Down Expand Up @@ -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<TradeCurrency> 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
Expand All @@ -285,12 +300,13 @@ public boolean hasPaymentMethodWithId(String paymentMethodId) {
* @return Optional of the trade currency for the given payment account
*/
public Optional<TradeCurrency> getTradeCurrency() {
List<TradeCurrency> 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();
}
Expand Down
21 changes: 21 additions & 0 deletions core/src/main/java/haveno/core/user/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<TradeCurrency> currencies) {
List<TraditionalCurrency> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,14 @@ public void onSaveNewAccount(PaymentAccount paymentAccount) {
TradeCurrency singleTradeCurrency = paymentAccount.getSingleTradeCurrency();
List<TradeCurrency> 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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -499,7 +500,9 @@ protected void onPaymentAccountsComboBoxSelected() {
model.onCurrencySelected(model.getTradeCurrency());

if (paymentAccount.hasMultipleCurrencies()) {
final List<TradeCurrency> tradeCurrencies = paymentAccount.getTradeCurrencies();
final List<TradeCurrency> tradeCurrencies = paymentAccount.getTradeCurrencies().stream()
.filter(TradeCurrency::getSelected)
.collect(Collectors.toList());
currencyComboBox.setItems(FXCollections.observableArrayList(tradeCurrencies));
currencyComboBox.getSelectionModel().select(model.getTradeCurrency());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
5 changes: 3 additions & 2 deletions proto/src/main/proto/pb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
Loading