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) {
UserThread.await(() -> stateProperty().set(state));
stateProperty.set(state);
}
public ObjectProperty<Offer.State> stateProperty() {

View File

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

View File

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

View File

@ -350,50 +350,52 @@ class TakeOfferViewModel extends ActivatableWithDataModel<TakeOfferDataModel> im
///////////////////////////////////////////////////////////////////////////////////////////
private void applyOfferState(Offer.State state) {
offerWarning.set(null);
UserThread.execute(() -> {
offerWarning.set(null);
// We have 2 situations handled here:
// 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
// So we use the takeOfferRequested flag to display different network_messages depending on the context.
switch (state) {
case UNKNOWN:
break;
case OFFER_FEE_RESERVED:
// irrelevant for taker
break;
case AVAILABLE:
isOfferAvailable.set(true);
updateButtonDisableState();
break;
case NOT_AVAILABLE:
if (takeOfferRequested)
offerWarning.set(Res.get("takeOffer.failed.offerNotAvailable"));
else
offerWarning.set(Res.get("takeOffer.failed.offerTaken"));
takeOfferRequested = false;
break;
case REMOVED:
if (!takeOfferRequested)
offerWarning.set(Res.get("takeOffer.failed.offerRemoved"));
takeOfferRequested = false;
break;
case MAKER_OFFLINE:
if (takeOfferRequested)
offerWarning.set(Res.get("takeOffer.failed.offererNotOnline"));
else
offerWarning.set(Res.get("takeOffer.failed.offererOffline"));
takeOfferRequested = false;
break;
default:
log.error("Unhandled offer state: " + state);
break;
}
updateSpinnerInfo();
updateButtonDisableState();
// We have 2 situations handled here:
// 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
// So we use the takeOfferRequested flag to display different network_messages depending on the context.
switch (state) {
case UNKNOWN:
break;
case OFFER_FEE_RESERVED:
// irrelevant for taker
break;
case AVAILABLE:
isOfferAvailable.set(true);
updateButtonDisableState();
break;
case NOT_AVAILABLE:
if (takeOfferRequested)
offerWarning.set(Res.get("takeOffer.failed.offerNotAvailable"));
else
offerWarning.set(Res.get("takeOffer.failed.offerTaken"));
takeOfferRequested = false;
break;
case REMOVED:
if (!takeOfferRequested)
offerWarning.set(Res.get("takeOffer.failed.offerRemoved"));
takeOfferRequested = false;
break;
case MAKER_OFFLINE:
if (takeOfferRequested)
offerWarning.set(Res.get("takeOffer.failed.offererNotOnline"));
else
offerWarning.set(Res.get("takeOffer.failed.offererOffline"));
takeOfferRequested = false;
break;
default:
log.error("Unhandled offer state: " + state);
break;
}
updateSpinnerInfo();
updateButtonDisableState();
});
}
private void applyTradeErrorMessage(@Nullable String errorMessage) {