Manage data after login
This commit is contained in:
@@ -3,8 +3,14 @@ package org.to.telegramfinalproject.Client;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.to.telegramfinalproject.Models.ChatEntry;
|
||||
|
||||
public class ActionHandler {
|
||||
private final PrintWriter out;
|
||||
@@ -51,20 +57,146 @@ public class ActionHandler {
|
||||
this.send(request);
|
||||
}
|
||||
|
||||
|
||||
public void search(){
|
||||
|
||||
System.out.print("Enter keyword to search: ");
|
||||
String keyword = scanner.nextLine();
|
||||
|
||||
JSONObject request = new JSONObject();
|
||||
request.put("action", "search");
|
||||
request.put("keyword", keyword);
|
||||
|
||||
send(request);
|
||||
|
||||
}
|
||||
|
||||
private void send(JSONObject request) {
|
||||
try {
|
||||
this.out.println(request.toString());
|
||||
String responseText = this.in.readLine();
|
||||
|
||||
if (responseText != null) {
|
||||
JSONObject response = new JSONObject(responseText);
|
||||
System.out.println("Server response: " + response.getString("message"));
|
||||
String status = response.getString("status");
|
||||
|
||||
String action = request.getString("action");
|
||||
|
||||
if (status.equals("success") && response.has("data") && !response.isNull("data")) {
|
||||
if (action.equals("login") || action.equals("register")) {
|
||||
|
||||
Session.currentUser = response.getJSONObject("data");
|
||||
|
||||
JSONArray chatListJson = Session.currentUser.getJSONArray("chat_list");
|
||||
List<ChatEntry> chatList = new ArrayList<>();
|
||||
|
||||
for (Object obj : chatListJson) {
|
||||
JSONObject chat = (JSONObject) obj;
|
||||
|
||||
ChatEntry entry = new ChatEntry(
|
||||
chat.getString("id"),
|
||||
chat.getString("name"),
|
||||
chat.getString("image_url"),
|
||||
chat.getString("type"),
|
||||
chat.isNull("last_message_time") ? null : LocalDateTime.parse(chat.getString("last_message_time"))
|
||||
);
|
||||
|
||||
chatList.add(entry);
|
||||
}
|
||||
|
||||
Session.chatList = chatList;
|
||||
}
|
||||
|
||||
else if (action.equals("search")) {
|
||||
|
||||
JSONArray results = response.getJSONObject("data").getJSONArray("results");
|
||||
|
||||
System.out.println("\nSearch Results:");
|
||||
for (Object obj : results) {
|
||||
JSONObject item = (JSONObject) obj;
|
||||
System.out.println("- [" + item.getString("type") + "] " + item.getString("name") + " (ID: " + item.getString("id") + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.out.println("No response from server.");
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void userMenu() {
|
||||
while (true) {
|
||||
System.out.println("\nUser Menu:");
|
||||
System.out.println("1. Show chat list");
|
||||
System.out.println("2. Search");
|
||||
System.out.println("3. Logout");
|
||||
|
||||
System.out.print("Choose an option: ");
|
||||
String choice = scanner.nextLine();
|
||||
|
||||
switch (choice) {
|
||||
|
||||
case "1":
|
||||
showChatListAndSelect();
|
||||
break;
|
||||
case "2" :
|
||||
search();
|
||||
break;
|
||||
|
||||
case "3":
|
||||
Session.currentUser = null;
|
||||
Session.chatList = null;
|
||||
System.out.println("Logged out.");
|
||||
return;
|
||||
default:
|
||||
System.out.println("Invalid choice.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void showChatListAndSelect() {
|
||||
if (Session.chatList == null || Session.chatList.isEmpty()) {
|
||||
System.out.println("No chats available.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("\nYour Chats:");
|
||||
for (int i = 0; i < Session.chatList.size(); i++) {
|
||||
ChatEntry entry = Session.chatList.get(i);
|
||||
String time = entry.getLastMessageTime() == null ? "No messages yet" : entry.getLastMessageTime().toString();
|
||||
System.out.println((i + 1) + ". [" + entry.getType() + "] " + entry.getName() + " - Last: " + time);
|
||||
}
|
||||
|
||||
System.out.print("Select a chat by number: ");
|
||||
int choice = Integer.parseInt(scanner.nextLine()) - 1;
|
||||
|
||||
if (choice < 0 || choice >= Session.chatList.size()) {
|
||||
System.out.println("Invalid selection.");
|
||||
return;
|
||||
}
|
||||
|
||||
ChatEntry selected = Session.chatList.get(choice);
|
||||
openChat(selected);
|
||||
}
|
||||
|
||||
|
||||
private void openChat(ChatEntry chat) {
|
||||
JSONObject request = new JSONObject();
|
||||
request.put("action", "get_messages");
|
||||
request.put("receiver_id", chat.getId());
|
||||
request.put("receiver_type", chat.getType());
|
||||
|
||||
send(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user