Merge remote-tracking branch 'origin/Sidebar-Menu' into Sidebar-Menu

# Conflicts:
#	src/main/java/org/to/telegramfinalproject/Client/SidebarHandler.java
This commit is contained in:
2025-08-13 11:07:09 +03:30
35 changed files with 563 additions and 152 deletions
@@ -2555,16 +2555,16 @@ public class ClientHandler implements Runnable {
break;
}
case "get_saved_messages": {
UUID user_Id = UUID.fromString(requestJson.getString("user_id"));
response = SidebarService.handleGetSavedMessages(user_Id);
break;
}
case "send_saved_messages": {
response = SidebarService.handleSendMessage(requestJson);
break;
}
// case "get_saved_messages": {
// UUID user_Id = UUID.fromString(requestJson.getString("user_id"));
// response = SidebarService.handleGetSavedMessages(user_Id);
// break;
// }
//
// case "send_saved_messages": {
// response = SidebarService.handleSendMessage(requestJson);
// break;
// }
case "search_contacts": {
String user_id = requestJson.getString("user_id");
@@ -211,86 +211,81 @@ public class SidebarService {
}
}
public static ResponseModel handleGetSavedMessages(UUID userId) {
try {
UUID chatId = PrivateChatDatabase.getOrCreateSavedMessagesChat(userId);
if (chatId == null) {
return new ResponseModel("error", "Failed to create or find saved messages chat.");
}
List<Message> messages = MessageDatabase.privateChatHistory(chatId, userId);
JSONArray messageArray = new JSONArray();
if (!messages.isEmpty()) {
for (Message msg : messages) {
JSONObject msgJson = new JSONObject();
msgJson.put("message_id", msg.getMessage_id().toString());
msgJson.put("sender_id", msg.getSender_id().toString());
msgJson.put("receiver_type", msg.getReceiver_type());
msgJson.put("receiver_id", msg.getReceiver_id().toString());
msgJson.put("content", msg.getContent());
msgJson.put("message_type", msg.getMessage_type());
msgJson.put("send_at", msg.getSend_at().toString()); // LocalDateTime
msgJson.put("status", msg.getStatus());
msgJson.put("reply_to_id", msg.getReply_to_id() != null ? msg.getReply_to_id().toString() : JSONObject.NULL);
msgJson.put("is_edited", msg.isIs_edited());
msgJson.put("original_message_id", msg.getOriginal_message_id() != null ? msg.getOriginal_message_id().toString() : JSONObject.NULL);
msgJson.put("forwarded_by", msg.getForwarded_by() != null ? msg.getForwarded_by().toString() : JSONObject.NULL);
msgJson.put("forwarded_from", msg.getForwarded_from() != null ? msg.getForwarded_from().toString() : JSONObject.NULL);
messageArray.put(msgJson);
}
}
JSONObject data = new JSONObject();
data.put("chat_id", chatId.toString());
data.put("messages", messageArray);
return new ResponseModel("success", "Saved messages retrieved successfully", data);
} catch (Exception e) {
e.printStackTrace();
return new ResponseModel("error", "Unexpected server error.");
}
}
// public static ResponseModel handleGetSavedMessages(UUID userId) {
// try {
//
// UUID chatId = PrivateChatDatabase.getOrCreateSavedMessagesChat(userId);
// if (chatId == null) {
// return new ResponseModel("error", "Failed to create or find saved messages chat.");
// }
//
// List<Message> messages = MessageDatabase.privateChatHistory(chatId, userId);
//
// JSONArray messageArray = new JSONArray();
// if (!messages.isEmpty()) {
// for (Message msg : messages) {
// JSONObject msgJson = new JSONObject();
// msgJson.put("message_id", msg.getMessage_id().toString());
// msgJson.put("sender_id", msg.getSender_id().toString());
// msgJson.put("receiver_type", msg.getReceiver_type());
// msgJson.put("receiver_id", msg.getReceiver_id().toString());
// msgJson.put("content", msg.getContent());
// msgJson.put("message_type", msg.getMessage_type());
// msgJson.put("send_at", msg.getSend_at().toString()); // LocalDateTime
// msgJson.put("status", msg.getStatus());
// msgJson.put("reply_to_id", msg.getReply_to_id() != null ? msg.getReply_to_id().toString() : JSONObject.NULL);
// msgJson.put("is_edited", msg.isIs_edited());
// msgJson.put("original_message_id", msg.getOriginal_message_id() != null ? msg.getOriginal_message_id().toString() : JSONObject.NULL);
// msgJson.put("forwarded_by", msg.getForwarded_by() != null ? msg.getForwarded_by().toString() : JSONObject.NULL);
// msgJson.put("forwarded_from", msg.getForwarded_from() != null ? msg.getForwarded_from().toString() : JSONObject.NULL);
//
// messageArray.put(msgJson);
// }
// }
//
// JSONObject data = new JSONObject();
// data.put("chat_id", chatId.toString());
// data.put("messages", messageArray);
//
// return new ResponseModel("success", "Saved messages retrieved successfully", data);
//
// } catch (Exception e) {
// e.printStackTrace();
// return new ResponseModel("error", "Unexpected server error.");
// }
// }
// Save messages to DB
public static ResponseModel handleSendMessage(JSONObject requestJson) {
try {
Message message = new Message(
UUID.fromString(requestJson.getString("message_id")),
UUID.fromString(requestJson.getString("sender_id")),
requestJson.getString("receiver_type"),
UUID.fromString(requestJson.getString("receiver_id")),
requestJson.optString("content", null),
requestJson.optString("message_type", "TEXT"),
LocalDateTime.now(), // send_at
requestJson.optString("status", "SEND"),
requestJson.isNull("reply_to_id") ? null : UUID.fromString(requestJson.getString("reply_to_id")),
requestJson.optBoolean("is_edited", false),
requestJson.isNull("original_message_id") ? null : UUID.fromString(requestJson.getString("original_message_id")),
requestJson.isNull("forwarded_by") ? null : UUID.fromString(requestJson.getString("forwarded_by")),
requestJson.isNull("forwarded_from") ? null : UUID.fromString(requestJson.getString("forwarded_from")),
requestJson.optBoolean("is_deleted_globally", false),
requestJson.isNull("edited_at") ? null :
LocalDateTime.ofInstant(
Instant.ofEpochMilli(requestJson.getLong("edited_at")),
ZoneId.systemDefault()
)
);
MessageDatabase.insertSavedMessage(message);
return new ResponseModel("success", "Message saved successfully.");
} catch (Exception e) {
e.printStackTrace();
return new ResponseModel("error", "Unexpected server error.");
}
}
// Changes username
public static boolean updateUserName(String userId, String newUserName) {
return false;
}
// public static ResponseModel handleSendMessage(JSONObject requestJson) {
// try {
// Message message = new Message(
// UUID.fromString(requestJson.getString("message_id")),
// UUID.fromString(requestJson.getString("sender_id")),
// requestJson.getString("receiver_type"),
// UUID.fromString(requestJson.getString("receiver_id")),
// requestJson.optString("content", null),
// requestJson.optString("message_type", "TEXT"),
// LocalDateTime.now(), // send_at
// requestJson.optString("status", "SEND"),
// requestJson.isNull("reply_to_id") ? null : UUID.fromString(requestJson.getString("reply_to_id")),
// requestJson.optBoolean("is_edited", false),
// requestJson.isNull("original_message_id") ? null : UUID.fromString(requestJson.getString("original_message_id")),
// requestJson.isNull("forwarded_by") ? null : UUID.fromString(requestJson.getString("forwarded_by")),
// requestJson.isNull("forwarded_from") ? null : UUID.fromString(requestJson.getString("forwarded_from")),
// requestJson.optBoolean("is_deleted_globally", false),
// requestJson.isNull("edited_at") ? null :
// LocalDateTime.ofInstant(
// Instant.ofEpochMilli(requestJson.getLong("edited_at")),
// ZoneId.systemDefault()
// )
// );
//
// MessageDatabase.insertSavedMessage(message);
// return new ResponseModel("success", "Message saved successfully.");
//
// } catch (Exception e) {
// e.printStackTrace();
// return new ResponseModel("error", "Unexpected server error.");
// }
// }
}
@@ -1,6 +1,5 @@
package org.to.telegramfinalproject.UI;
import javafx.animation.TranslateTransition;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
@@ -10,12 +9,10 @@ import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.util.Duration;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
@@ -125,7 +122,7 @@ public class IntroController {
@FXML
private void handleStartMessaging(ActionEvent event) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/org/to/telegramfinalproject/login_view.fxml"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("/org/to/telegramfinalproject/Fxml/login_view.fxml"));
Scene loginScene = new Scene(loader.load());
// Get the current stage and its dimensions
@@ -0,0 +1,30 @@
package org.to.telegramfinalproject.UI;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import org.to.telegramfinalproject.Models.User;
public class MyProfileController {
@FXML private ImageView profileImage;
@FXML private Label displayName;
@FXML private Label status;
@FXML private Label bio;
@FXML private Label userId;
// public void initialize() {
// // Load data from your logged-in user object
// // Example:
// displayName.setText(CurrentUser.getName());
// status.setText(CurrentUser.isOnline() ? "online" : "offline");
// bio.setText(CurrentUser.getBio());
// userId.setText(CurrentUser.getUserId());
//
// if (CurrentUser.getProfileImagePath() != null) {
// profileImage.setImage(new Image(CurrentUser.getProfileImagePath()));
// }
// }
}
@@ -1,11 +1,25 @@
package org.to.telegramfinalproject.UI;
import javafx.animation.TranslateTransition;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.util.Duration;
public class SidebarMenuController {
import java.net.URL;
public class SidebarMenuController {
private static final String ICON_PATH = "/org/to/telegramfinalproject/Icons/";
@FXML private VBox sidebarRoot;
@FXML private ImageView profileImage;
@FXML private Label usernameLabel;
@@ -18,14 +32,149 @@ public class SidebarMenuController {
@FXML private Button telegramFeaturesButton;
@FXML private Button telegramQnAButton;
@FXML private ToggleButton nightModeToggle;
// Custom Telegram-style toggle
@FXML private HBox nightModeToggle;
@FXML private Region toggleThumb;
@FXML private ImageView nightModeIcon;
// convenience handle
private final ThemeManager themeManager = ThemeManager.getInstance();
@FXML
public void initialize() {
Image image = new Image(getClass().getResource("/org/to/telegramfinalproject/Images/profile.png").toExternalForm());
profileImage.setImage(image);
// Load default profile image (if present)
Image profile = loadImage("/org/to/telegramfinalproject/Images/profile.png");
if (profile != null) profileImage.setImage(profile);
// Called automatically after FXML is loaded
usernameLabel.setText("Asal");
setupButtonActions();
setupToggleAction();
// When the Scene is ready:
sidebarRoot.sceneProperty().addListener((obs, oldScene, newScene) -> {
if (newScene != null) {
// 1) Register with ThemeManager so this scene auto-updates on theme changes
themeManager.registerScene(newScene);
// 2) Make sure we start in LIGHT mode (if that's what you want)
// (If you already set the initial mode elsewhere, remove the next line)
themeManager.setDarkMode(false);
// 3) Sync icons & toggle with current mode
boolean dark = themeManager.isDarkMode();
updateIcons(dark);
syncToggleVisual(dark, /*animate=*/true);
// Ensure the thumb is correctly positioned after layout pass
Platform.runLater(() -> syncToggleVisual(themeManager.isDarkMode(), false));
}
});
// If theme is changed from somewhere else (another screen), keep sidebar in sync
themeManager.darkModeProperty().addListener((o, wasDark, isDark) -> {
updateIcons(isDark);
syncToggleVisual(isDark, /*animate=*/true);
});
}
private void setupButtonActions() {
myProfileButton.setOnAction(e -> openMyProfile());
newGroupButton.setOnAction(e -> createNewGroup());
newChannelButton.setOnAction(e -> createNewChannel());
contactsButton.setOnAction(e -> openContacts());
savedMessagesButton.setOnAction(e -> openSavedMessages());
settingsButton.setOnAction(e -> openSettings());
telegramFeaturesButton.setOnAction(e -> openTelegramFeatures());
telegramQnAButton.setOnAction(e -> openTelegramQnA());
}
private void setupToggleAction() {
nightModeToggle.setOnMouseClicked(e -> {
boolean newDark = !themeManager.isDarkMode();
// Update the global theme via ThemeManager
themeManager.setDarkMode(newDark);
// Animate the thumb to its new position (listener will handle icons & final position sync)
syncToggleVisual(newDark, /*animate=*/true);
});
}
/** Update all button icons to match theme.
* darkMode == true => white icons => use *_light.png
* darkMode == false => dark icons => use *_dark.png
*/
private void updateIcons(boolean darkMode) {
String suffix = darkMode ? "_light.png" : "_dark.png";
myProfileButton.setGraphic(makeIcon(ICON_PATH + "my_profile" + suffix));
newGroupButton.setGraphic(makeIcon(ICON_PATH + "new_group" + suffix));
newChannelButton.setGraphic(makeIcon(ICON_PATH + "new_channel" + suffix));
contactsButton.setGraphic(makeIcon(ICON_PATH + "contacts" + suffix));
savedMessagesButton.setGraphic(makeIcon(ICON_PATH + "saved_messages" + suffix));
settingsButton.setGraphic(makeIcon(ICON_PATH + "settings" + suffix));
telegramFeaturesButton.setGraphic(makeIcon(ICON_PATH + "telegram_features" + suffix));
telegramQnAButton.setGraphic(makeIcon(ICON_PATH + "telegram_qna" + suffix));
// Night-mode moon icon
Image moon = loadImage(ICON_PATH + "night_mode" + suffix);
if (moon != null) nightModeIcon.setImage(moon);
}
/** Keep the toggles CSS class and thumb position in sync with the current mode. */
private void syncToggleVisual(boolean darkMode, boolean animate) {
// CSS class "on" on the track
if (darkMode) {
if (!nightModeToggle.getStyleClass().contains("on")) {
nightModeToggle.getStyleClass().add("on");
}
} else {
nightModeToggle.getStyleClass().remove("on");
}
// Compute target X for the thumb
double offX = 2;
double onX = Math.max(2, nightModeToggle.getWidth() - toggleThumb.getWidth() - 4);
double targetX = darkMode ? onX : offX;
if (animate) {
TranslateTransition tt = new TranslateTransition(Duration.millis(200), toggleThumb);
tt.setToX(targetX);
tt.play();
} else {
toggleThumb.setTranslateX(targetX);
}
}
// --- helpers -------------------------------------------------------------
private Image loadImage(String path) {
URL res = getClass().getResource(path);
if (res == null) {
System.err.println("Resource not found: " + path);
return null;
}
return new Image(res.toExternalForm());
}
private ImageView makeIcon(String path) {
ImageView iv = new ImageView();
Image img = loadImage(path);
if (img != null) {
iv.setImage(img);
iv.setFitWidth(22);
iv.setFitHeight(22);
iv.setPreserveRatio(true);
}
return iv;
}
// Example button actions
private void openMyProfile() { System.out.println("Opening My Profile..."); }
private void createNewGroup() { System.out.println("Creating New Group..."); }
private void createNewChannel() { System.out.println("Creating New Channel..."); }
private void openContacts() { System.out.println("Opening Contacts..."); }
private void openSavedMessages() { System.out.println("Opening Saved Messages..."); }
private void openSettings() { System.out.println("Opening Settings..."); }
private void openTelegramFeatures() { System.out.println("Opening Telegram Features..."); }
private void openTelegramQnA() { System.out.println("Opening Telegram Q&A..."); }
}
@@ -5,17 +5,16 @@ import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.to.telegramfinalproject.HelloApplication;
import java.io.IOException;
public class TelegramApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(TelegramApplication.class.getResource("/org/to/telegramfinalproject/sidebar_menu.fxml"));
FXMLLoader fxmlLoader = new FXMLLoader(TelegramApplication.class.getResource("/org/to/telegramfinalproject/Fxml/sidebar_menu.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 1480, 820);
scene.getStylesheets().add(getClass().getResource("/org/to/telegramfinalproject/CSS/sidebar_menu.css").toExternalForm());
scene.getStylesheets().add(getClass().getResource("/org/to/telegramfinalproject/CSS/light_theme.css").toExternalForm());
stage.setTitle("Telegram");
stage.setScene(scene);
@@ -0,0 +1,58 @@
package org.to.telegramfinalproject.UI;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.Scene;
import java.util.ArrayList;
import java.util.List;
public class ThemeManager {
private static final ThemeManager instance = new ThemeManager();
private final BooleanProperty darkMode = new SimpleBooleanProperty(false);
private final List<Scene> registeredScenes = new ArrayList<>();
private ThemeManager() {
// Whenever darkMode changes, update all registered scenes
darkMode.addListener((obs, oldVal, newVal) -> applyThemeToAll());
}
public static ThemeManager getInstance() {
return instance;
}
public BooleanProperty darkModeProperty() {
return darkMode;
}
public boolean isDarkMode() {
return darkMode.get();
}
public void setDarkMode(boolean dark) {
darkMode.set(dark);
}
public void registerScene(Scene scene) {
if (!registeredScenes.contains(scene)) {
registeredScenes.add(scene);
applyTheme(scene); // Apply current theme immediately
}
}
private void applyThemeToAll() {
for (Scene scene : registeredScenes) {
applyTheme(scene);
}
}
private void applyTheme(Scene scene) {
scene.getStylesheets().clear();
if (isDarkMode()) {
scene.getStylesheets().add(getClass().getResource("/org/to/telegramfinalproject/CSS/dark_theme.css").toExternalForm());
} else {
scene.getStylesheets().add(getClass().getResource("/org/to/telegramfinalproject/CSS/light_theme.css").toExternalForm());
}
}
}