RealTime (update chat list automatically)

This commit is contained in:
2025-07-15 23:30:14 +03:30
parent 005302da03
commit b387137a00
3 changed files with 57 additions and 6 deletions
@@ -588,12 +588,18 @@ public class ActionHandler {
public void userMenu(UUID internal_uuid) throws IOException { public void userMenu(UUID internal_uuid) throws IOException {
while (true) { while (true) {
if (Session.forceRefreshChatList) {
System.out.println("🔁 Refresh triggered by real-time event."); if (Session.backToChatList) {
requestChatList(); // با sendWithResponse جواب می‌گیری Session.backToChatList = false;
Session.forceRefreshChatList = false; return;
} }
// if (Session.forceRefreshChatList) {
// System.out.println("🔁 Refresh triggered by real-time event.");
// requestChatList(); // با sendWithResponse جواب می‌گیری
// Session.forceRefreshChatList = false;
// }
System.out.println("\nUser Menu:"); System.out.println("\nUser Menu:");
@@ -606,7 +612,11 @@ public class ActionHandler {
String choice = scanner.nextLine(); String choice = scanner.nextLine();
switch (choice) { switch (choice) {
case "1" -> showChatListAndSelect(); case "1" -> {
Session.inChatListMenu = true;
showChatListAndSelect();
Session.inChatListMenu = false;
}
case "2" -> search(); case "2" -> search();
case "3" -> createChannel(); case "3" -> createChannel();
case "4" -> createGroup(); case "4" -> createGroup();
@@ -2062,7 +2072,7 @@ public class ActionHandler {
} }
public void requestChatList() { public static void requestChatList() {
JSONObject req = new JSONObject(); JSONObject req = new JSONObject();
req.put("action", "get_chat_list"); req.put("action", "get_chat_list");
req.put("user_id", Session.getUserUUID()); req.put("user_id", Session.getUserUUID());
@@ -2127,7 +2137,43 @@ public class ActionHandler {
} }
} }
public static class ChatStateMonitor implements Runnable {
private final PrintWriter out;
public ChatStateMonitor(PrintWriter out) {
this.out = out;
}
@Override
public void run() {
while (true) {
try {
if (forceExitChat) {
System.out.println("🚪 You were removed from the chat. Returning to chat list...");
forceExitChat = false;
Session.backToChatList = true;
}
if (Session.forceRefreshChatList) {
Session.forceRefreshChatList = false;
System.out.println("🔁 Refresh triggered by real-time event.");
ActionHandler.requestChatList();
if (Session.inChatListMenu) {
System.out.println("\n📬 Updated Chat List:");
ActionHandler.displayChatList(); // این متدی باشه که چت‌ها رو نمایش بده
System.out.print("Select a chat by number: ");
}
}
Thread.sleep(300); // جلوگیری از مصرف بی‌مورد CPU
} catch (Exception e) {
System.out.println("❌ ChatStateMonitor crashed: " + e.getMessage());
}
}
}
}
} }
@@ -14,6 +14,10 @@ public class Session {
public static JSONObject currentUser; public static JSONObject currentUser;
public static List<ChatEntry> chatList = new ArrayList<>(); public static List<ChatEntry> chatList = new ArrayList<>();
public static volatile boolean forceRefreshChatList = false; public static volatile boolean forceRefreshChatList = false;
public static volatile boolean backToChatList = false;
public static boolean inChatListMenu = false;
public static String getUserUUID() { public static String getUserUUID() {
@@ -72,6 +72,7 @@ public class TelegramClient {
handler.loginHandler(); handler.loginHandler();
if (Session.currentUser != null) { if (Session.currentUser != null) {
System.out.println("✅ Login successful."); System.out.println("✅ Login successful.");
new Thread(new ActionHandler.ChatStateMonitor(out)).start();
UUID internalId = UUID.fromString(Session.currentUser.getString("internal_uuid")); UUID internalId = UUID.fromString(Session.currentUser.getString("internal_uuid"));
loggedInUserId = internalId; loggedInUserId = internalId;