update offer state on same thread to fix blocking

This commit is contained in:
woodser 2024-04-06 17:39:21 -04:00
parent 6fea58c197
commit 59fbd805a5
4 changed files with 76 additions and 70 deletions

View file

@ -265,7 +265,7 @@ public class Offer implements NetworkPayload, PersistablePayload {
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
public void setState(Offer.State state) { public void setState(Offer.State state) {
UserThread.await(() -> stateProperty().set(state)); stateProperty.set(state);
} }
public ObjectProperty<Offer.State> stateProperty() { public ObjectProperty<Offer.State> stateProperty() {

View file

@ -200,22 +200,24 @@ public class MarketView extends ActivatableView<TabPane, Void> {
} }
private String getAllOffersWithReferralId() { private String getAllOffersWithReferralId() {
List<String> list = offerBook.getOfferBookListItems().stream() synchronized (offerBook.getOfferBookListItems()) {
.map(OfferBookListItem::getOffer) List<String> list = offerBook.getOfferBookListItems().stream()
.filter(offer -> offer.getExtraDataMap() != null) .map(OfferBookListItem::getOffer)
.filter(offer -> offer.getExtraDataMap().get(OfferPayload.REFERRAL_ID) != null) .filter(offer -> offer.getExtraDataMap() != null)
.map(offer -> { .filter(offer -> offer.getExtraDataMap().get(OfferPayload.REFERRAL_ID) != null)
StringBuilder sb = new StringBuilder(); .map(offer -> {
sb.append("Offer ID: ").append(offer.getId()).append("\n") StringBuilder sb = new StringBuilder();
.append("Type: ").append(offer.getDirection().name()).append("\n") sb.append("Offer ID: ").append(offer.getId()).append("\n")
.append("Market: ").append(CurrencyUtil.getCurrencyPair(offer.getCurrencyCode())).append("\n") .append("Type: ").append(offer.getDirection().name()).append("\n")
.append("Price: ").append(FormattingUtils.formatPrice(offer.getPrice())).append("\n") .append("Market: ").append(CurrencyUtil.getCurrencyPair(offer.getCurrencyCode())).append("\n")
.append("Amount: ").append(DisplayUtils.formatAmount(offer, formatter)).append(" BTC\n") .append("Price: ").append(FormattingUtils.formatPrice(offer.getPrice())).append("\n")
.append("Payment method: ").append(Res.get(offer.getPaymentMethod().getId())).append("\n") .append("Amount: ").append(DisplayUtils.formatAmount(offer, formatter)).append(" BTC\n")
.append("ReferralID: ").append(offer.getExtraDataMap().get(OfferPayload.REFERRAL_ID)); .append("Payment method: ").append(Res.get(offer.getPaymentMethod().getId())).append("\n")
return sb.toString(); .append("ReferralID: ").append(offer.getExtraDataMap().get(OfferPayload.REFERRAL_ID));
}) return sb.toString();
.collect(Collectors.toList()); })
return Joiner.on("\n\n").join(list); .collect(Collectors.toList());
return Joiner.on("\n\n").join(list);
}
} }
} }

View file

@ -147,16 +147,18 @@ class OfferBookChartViewModel extends ActivatableViewModel {
private void fillTradeCurrencies() { private void fillTradeCurrencies() {
// Don't use a set as we need all entries // Don't use a set as we need all entries
List<TradeCurrency> tradeCurrencyList = offerBookListItems.stream() synchronized (offerBookListItems) {
.map(e -> { List<TradeCurrency> tradeCurrencyList = offerBookListItems.stream()
String currencyCode = e.getOffer().getCurrencyCode(); .map(e -> {
Optional<TradeCurrency> tradeCurrencyOptional = CurrencyUtil.getTradeCurrency(currencyCode); String currencyCode = e.getOffer().getCurrencyCode();
return tradeCurrencyOptional.orElse(null); Optional<TradeCurrency> tradeCurrencyOptional = CurrencyUtil.getTradeCurrency(currencyCode);
}) return tradeCurrencyOptional.orElse(null);
.filter(Objects::nonNull) })
.collect(Collectors.toList()); .filter(Objects::nonNull)
.collect(Collectors.toList());
currencyListItems.updateWithCurrencies(tradeCurrencyList, null); currencyListItems.updateWithCurrencies(tradeCurrencyList, null);
}
} }
@Override @Override

View file

@ -350,50 +350,52 @@ class TakeOfferViewModel extends ActivatableWithDataModel<TakeOfferDataModel> im
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
private void applyOfferState(Offer.State state) { private void applyOfferState(Offer.State state) {
offerWarning.set(null); UserThread.execute(() -> {
offerWarning.set(null);
// We have 2 situations handled here: // We have 2 situations handled here:
// 1. when clicking take offer in the offerbook screen, we do the availability check // 1. when clicking take offer in the offerbook screen, we do the availability check
// 2. Before actually taking the offer in the take offer screen, we check again the availability as some time might have passed in the meantime // 2. Before actually taking the offer in the take offer screen, we check again the availability as some time might have passed in the meantime
// So we use the takeOfferRequested flag to display different network_messages depending on the context. // So we use the takeOfferRequested flag to display different network_messages depending on the context.
switch (state) { switch (state) {
case UNKNOWN: case UNKNOWN:
break; break;
case OFFER_FEE_RESERVED: case OFFER_FEE_RESERVED:
// irrelevant for taker // irrelevant for taker
break; break;
case AVAILABLE: case AVAILABLE:
isOfferAvailable.set(true); isOfferAvailable.set(true);
updateButtonDisableState(); updateButtonDisableState();
break; break;
case NOT_AVAILABLE: case NOT_AVAILABLE:
if (takeOfferRequested) if (takeOfferRequested)
offerWarning.set(Res.get("takeOffer.failed.offerNotAvailable")); offerWarning.set(Res.get("takeOffer.failed.offerNotAvailable"));
else else
offerWarning.set(Res.get("takeOffer.failed.offerTaken")); offerWarning.set(Res.get("takeOffer.failed.offerTaken"));
takeOfferRequested = false; takeOfferRequested = false;
break; break;
case REMOVED: case REMOVED:
if (!takeOfferRequested) if (!takeOfferRequested)
offerWarning.set(Res.get("takeOffer.failed.offerRemoved")); offerWarning.set(Res.get("takeOffer.failed.offerRemoved"));
takeOfferRequested = false; takeOfferRequested = false;
break; break;
case MAKER_OFFLINE: case MAKER_OFFLINE:
if (takeOfferRequested) if (takeOfferRequested)
offerWarning.set(Res.get("takeOffer.failed.offererNotOnline")); offerWarning.set(Res.get("takeOffer.failed.offererNotOnline"));
else else
offerWarning.set(Res.get("takeOffer.failed.offererOffline")); offerWarning.set(Res.get("takeOffer.failed.offererOffline"));
takeOfferRequested = false; takeOfferRequested = false;
break; break;
default: default:
log.error("Unhandled offer state: " + state); log.error("Unhandled offer state: " + state);
break; break;
} }
updateSpinnerInfo(); updateSpinnerInfo();
updateButtonDisableState(); updateButtonDisableState();
});
} }
private void applyTradeErrorMessage(@Nullable String errorMessage) { private void applyTradeErrorMessage(@Nullable String errorMessage) {