synchronize access to pending trades data model

Co-authored-by: XMRZombie <monerozombie@proton.me>
This commit is contained in:
woodser 2025-02-12 07:34:32 -05:00
parent cd71bcdde7
commit b72159fcf8
2 changed files with 38 additions and 26 deletions

View File

@ -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);
}
}
}

View File

@ -363,7 +363,11 @@ public class PendingTradesView extends ActivatableViewAndModel<VBox, PendingTrad
}
private void updateMoveTradeToFailedColumnState() {
UserThread.execute(() -> 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<VBox, PendingTrad
///////////////////////////////////////////////////////////////////////////////////////////
private void updateNewChatMessagesByTradeMap() {
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());
}
});
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) {