From b72159fcf8e9fdc7199f6bafcff2118707b43be0 Mon Sep 17 00:00:00 2001 From: woodser Date: Wed, 12 Feb 2025 07:34:32 -0500 Subject: [PATCH] synchronize access to pending trades data model Co-authored-by: XMRZombie --- .../pendingtrades/PendingTradesDataModel.java | 36 +++++++++++-------- .../pendingtrades/PendingTradesView.java | 28 +++++++++------ 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/desktop/src/main/java/haveno/desktop/main/portfolio/pendingtrades/PendingTradesDataModel.java b/desktop/src/main/java/haveno/desktop/main/portfolio/pendingtrades/PendingTradesDataModel.java index e4fc8b1bdc..7649274aa1 100644 --- a/desktop/src/main/java/haveno/desktop/main/portfolio/pendingtrades/PendingTradesDataModel.java +++ b/desktop/src/main/java/haveno/desktop/main/portfolio/pendingtrades/PendingTradesDataModel.java @@ -332,15 +332,17 @@ public class PendingTradesDataModel extends ActivatableDataModel { } // add shown trades to list - list.clear(); - list.addAll(tradeManager.getObservableList().stream() - .filter(trade -> isTradeShown(trade)) - .map(trade -> new PendingTradesListItem(trade, btcFormatter)) - .collect(Collectors.toList())); - } + synchronized (list) { + list.clear(); + list.addAll(tradeManager.getObservableList().stream() + .filter(trade -> isTradeShown(trade)) + .map(trade -> new PendingTradesListItem(trade, btcFormatter)) + .collect(Collectors.toList())); - // we sort by date, earliest first - list.sort((o1, o2) -> o2.getTrade().getDate().compareTo(o1.getTrade().getDate())); + // we sort by date, earliest first + list.sort((o1, o2) -> o2.getTrade().getDate().compareTo(o1.getTrade().getDate())); + } + } selectBestItem(); } @@ -350,17 +352,21 @@ public class PendingTradesDataModel extends ActivatableDataModel { } private void selectBestItem() { - if (list.size() == 1) - doSelectItem(list.get(0)); - else if (list.size() > 1 && (selectedItemProperty.get() == null || !list.contains(selectedItemProperty.get()))) - doSelectItem(list.get(0)); - else if (list.size() == 0) - doSelectItem(null); + synchronized (list) { + if (list.size() == 1) + doSelectItem(list.get(0)); + else if (list.size() > 1 && (selectedItemProperty.get() == null || !list.contains(selectedItemProperty.get()))) + doSelectItem(list.get(0)); + else if (list.size() == 0) + doSelectItem(null); + } } private void selectItemByTradeId(String tradeId) { if (activated) { - list.stream().filter(e -> e.getTrade().getId().equals(tradeId)).findAny().ifPresent(this::doSelectItem); + synchronized (list) { + list.stream().filter(e -> e.getTrade().getId().equals(tradeId)).findAny().ifPresent(this::doSelectItem); + } } } diff --git a/desktop/src/main/java/haveno/desktop/main/portfolio/pendingtrades/PendingTradesView.java b/desktop/src/main/java/haveno/desktop/main/portfolio/pendingtrades/PendingTradesView.java index 8c31e3d8e6..645fbb5014 100644 --- a/desktop/src/main/java/haveno/desktop/main/portfolio/pendingtrades/PendingTradesView.java +++ b/desktop/src/main/java/haveno/desktop/main/portfolio/pendingtrades/PendingTradesView.java @@ -363,7 +363,11 @@ public class PendingTradesView extends ActivatableViewAndModel moveTradeToFailedColumn.setVisible(model.dataModel.list.stream().anyMatch(item -> isMaybeInvalidTrade(item.getTrade())))); + UserThread.execute(() -> { + synchronized (model.dataModel.list) { + moveTradeToFailedColumn.setVisible(model.dataModel.list.stream().anyMatch(item -> isMaybeInvalidTrade(item.getTrade()))); + } + }); } private boolean isMaybeInvalidTrade(Trade trade) { @@ -420,16 +424,18 @@ public class PendingTradesView extends ActivatableViewAndModel { - Trade trade = t.getTrade(); - synchronized (trade.getChatMessages()) { - newChatMessagesByTradeMap.put(trade.getId(), - trade.getChatMessages().stream() - .filter(m -> !m.isWasDisplayed()) - .filter(m -> !m.isSystemMessage()) - .count()); - } - }); + synchronized (model.dataModel.list) { + model.dataModel.list.forEach(t -> { + Trade trade = t.getTrade(); + synchronized (trade.getChatMessages()) { + newChatMessagesByTradeMap.put(trade.getId(), + trade.getChatMessages().stream() + .filter(m -> !m.isWasDisplayed()) + .filter(m -> !m.isSystemMessage()) + .count()); + } + }); + } } private void openChat(Trade trade) {