Work on send message Real time in UI(update chat list)

This commit is contained in:
2025-08-30 23:04:59 +03:30
parent edc61ccb96
commit 46216d3368
3 changed files with 44 additions and 32 deletions
@@ -128,8 +128,6 @@ public class ChatEntry {
this.archived = archived;
}
// public void setLastMessageTime(String newTime) {this.lastMessageTime = LocalDateTime.parse(newTime);
// }
public void setLastMessageTime(String newTime) {
@@ -176,4 +174,10 @@ public class ChatEntry {
public void setUnread(int unreadCount){this.unreadCount = unreadCount;}
public int getUnread(){return unreadCount;}
public void setLastMessageTime(LocalDateTime t) {
this.lastMessageTime = t;
}
}
@@ -765,14 +765,12 @@ public class ChatPageController {
boolean edited,
org.json.JSONArray reactions
) {
// meta (فرستنده + زمان [+ edited])
String metaText = (displayName == null ? "" : displayName) + "" + formatWhen(sentAt);
if (edited) metaText += " (edited)";
Label meta = new Label(metaText);
meta.setStyle("-fx-font-size: 11; -fx-text-fill: #7e8a97;");
meta.setWrapText(true);
// نرمال‌سازی نوع پیام و متن
String t = type == null ? "" : type.trim().toUpperCase();
boolean isText = t.isEmpty() ? (content != null && !content.isBlank()) : "TEXT".equals(t);
String bodyText = isText ? (content == null ? "" : content) : bracketLabel(t);
@@ -824,6 +822,7 @@ public class ChatPageController {
row.setPadding(new javafx.geometry.Insets(2, 6, 2, 6));
messageContainer.getChildren().add(row);
}
@@ -79,28 +79,24 @@ public class MainController {
//For realtime handling
public void onChatUpdated(UUID chatId,
String chatType,
LocalDateTime lastTs,
boolean isIncoming,
String lastPreview) {
var opt = java.util.stream.Stream
.of(Session.chatList, Session.activeChats, Session.archivedChats)
.flatMap(java.util.Collection::stream)
.filter(c -> c.getId().equals(chatId))
.findFirst();
if (opt.isEmpty()) return;
public void onChatUpdated(UUID chatId, String chatType, LocalDateTime lastTs,
boolean isIncoming, String lastPreview) {
ChatEntry chat = opt.get();
if (lastTs != null) chat.setLastMessageTime(String.valueOf(lastTs)); // ← نوع: LocalDateTime
if (lastPreview != null) chat.setLastMessagePreview(lastPreview);
// اگر هیچ‌کجا نیست، بی‌خیال
boolean exists =
(Session.chatList != null && Session.chatList.stream().anyMatch(c -> chatId.equals(c.getId()))) ||
(Session.activeChats != null && Session.activeChats.stream().anyMatch(c -> chatId.equals(c.getId()))) ||
(Session.archivedChats != null && Session.archivedChats.stream().anyMatch(c -> chatId.equals(c.getId())));
if (!exists) return;
boolean isOpen = Session.currentChatId != null &&
Session.currentChatId.equals(chatId.toString());
if (isIncoming && !isOpen) {
chat.setUnread(chat.getUnread() + 1);
}
forEachChat(chatId, chat -> {
chat.setLastMessageTime(lastTs);
if (lastPreview != null) chat.setLastMessagePreview(lastPreview);
if (isIncoming && !isOpen) chat.setUnreadCount(chat.getUnreadCount() + 1); // ✅ یکدست با unreadCount
});
java.util.Comparator<ChatEntry> byTimeDesc = (c1, c2) -> {
var t1 = c1.getLastMessageTime();
@@ -110,15 +106,34 @@ public class MainController {
if (t2 == null) return -1;
return t2.compareTo(t1);
};
Session.chatList.sort(byTimeDesc);
Session.activeChats.sort(byTimeDesc);
Session.archivedChats.sort(byTimeDesc);
if (Session.chatList != null) Session.chatList.sort(byTimeDesc);
if (Session.activeChats != null) Session.activeChats.sort(byTimeDesc);
if (Session.archivedChats != null) Session.archivedChats.sort(byTimeDesc);
refreshChatListUI();
refreshBadgesIfAny();
}
// MainController
private void forEachChat(UUID chatId, java.util.function.Consumer<ChatEntry> fn) {
java.util.List<java.util.List<ChatEntry>> lists = java.util.List.of(
Session.chatList != null ? Session.chatList : java.util.List.of(),
Session.activeChats != null ? Session.activeChats : java.util.List.of(),
Session.archivedChats != null ? Session.archivedChats : java.util.List.of()
);
for (var lst : lists) {
for (var c : lst) {
if (chatId.equals(c.getId())) {
fn.accept(c);
}
}
}
}
private void refreshChatListUI() {
Platform.runLater(() -> {
chatListContainer.getChildren().clear();
@@ -195,15 +210,12 @@ public class MainController {
// }
// });
// وقتی کاربر Enter زد روی سرچ‌بار، درخواست سرچ بفرست
searchBar.setOnAction(e -> performGlobalSearch(searchBar.getText().trim()));
// باز/بسته کردن پنل نتایج با تایپ (دلخواه)
searchBar.textProperty().addListener((obs,o,n)->{
if (n!=null && !n.isBlank()) showSearchPanel();
});
// کلیک روی نتیجه
chatSearchResults.setOnMouseClicked(e -> {
int idx = chatSearchResults.getSelectionModel().getSelectedIndex();
if (idx >= 0 && idx < searchBacking.size()) {
@@ -283,7 +295,6 @@ public class MainController {
if (list == null || list.isEmpty()) return;
// جدا کردن Saved از بقیه
ChatEntry saved = null;
java.util.List<ChatEntry> others = new java.util.ArrayList<>();
for (ChatEntry c : list) {
@@ -360,7 +371,6 @@ public class MainController {
String preview = chat.getLastMessagePreview() == null ? "" : chat.getLastMessagePreview();
// اگر lastMessageTime از نوع LocalDateTime است:
String timeText = chat.getLastMessageTime() == null
? ""
: formatChatTime(chat.getLastMessageTime());
@@ -419,7 +429,7 @@ public class MainController {
Session.currentChatId = chat.getId().toString();
chatDisplayArea.getChildren().setAll(chatPage);
chat.setUnreadCount(0);
ChatItemController item = itemControllers.get(chat.getId());
if (item != null) item.setUnread(0);
@@ -815,7 +825,6 @@ public class MainController {
}
private String guessNameForReceiver(SearchResult r) {
// فقط برای زمانی که موجودیت در لیست نبود
if (r.title != null && !r.title.isBlank()) return r.title;
if (r.receiverType != null) {
switch (r.receiverType) {