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