Clean code.

This commit is contained in:
Asal Lotfi
2025-08-29 20:34:28 +03:30
parent c5164592e9
commit 3091764a3b
4 changed files with 51 additions and 66 deletions
@@ -72,6 +72,11 @@ public class ChatPageController {
@FXML @FXML
private ImageView sendIcon; private ImageView sendIcon;
// ===== Time formatter for messages =====
private static final DateTimeFormatter FMT_HHMM = DateTimeFormatter.ofPattern("HH:mm");
private static final DateTimeFormatter FMT_DATE_TIME = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
private static final String YESTERDAY_LABEL = "Yesterday";
// ===== state ===== // ===== state =====
private String chatName; private String chatName;
private final ThemeManager themeManager = ThemeManager.getInstance(); private final ThemeManager themeManager = ThemeManager.getInstance();
@@ -81,53 +86,8 @@ public class ChatPageController {
private ChatEntry currentChat; private ChatEntry currentChat;
private UUID me; private UUID me;
private void initCurrentUserId() {
try {
String meStr = org.to.telegramfinalproject.Client.Session
.currentUser.getString("internal_uuid");
me = UUID.fromString(meStr);
} catch (Exception ignore) {
me = null;
}
}
//Time formatter for messages
private static final DateTimeFormatter FMT_HHMM = DateTimeFormatter.ofPattern("HH:mm");
private static final DateTimeFormatter FMT_DATE_TIME = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
private static final String YESTERDAY_LABEL = "Yesterday";
private String formatWhen(LocalDateTime ts) {
if (ts == null) return "";
LocalDate today = LocalDate.now();
LocalDate d = ts.toLocalDate();
if (d.isEqual(today)) {
return FMT_HHMM.format(ts);
} else if (d.isEqual(today.minusDays(1))) {
return YESTERDAY_LABEL + " " + FMT_HHMM.format(ts);
} else {
return FMT_DATE_TIME.format(ts);
}
}
/** ISO → LocalDateTime (با پشتیبانی از Offset/Z) */
private LocalDateTime parseWhen(String iso) {
if (iso == null || iso.isEmpty()) return null;
try {
return OffsetDateTime.parse(iso).toLocalDateTime();
} catch (Exception ignore) {
try {
return LocalDateTime.parse(iso);
} catch (Exception e) {
return null;
}
}
}
@FXML @FXML
public void initialize() { public void initialize() {
initCurrentUserId(); initCurrentUserId();
// Send button // Send button
@@ -235,6 +195,44 @@ public class ChatPageController {
themeManager.darkModeProperty().addListener((o, oldVal, isDark) -> syncIconsWithTheme()); themeManager.darkModeProperty().addListener((o, oldVal, isDark) -> syncIconsWithTheme());
} }
private void initCurrentUserId() {
try {
String meStr = org.to.telegramfinalproject.Client.Session
.currentUser.getString("internal_uuid");
me = UUID.fromString(meStr);
} catch (Exception ignore) {
me = null;
}
}
private String formatWhen(LocalDateTime ts) {
if (ts == null) return "";
LocalDate today = LocalDate.now();
LocalDate d = ts.toLocalDate();
if (d.isEqual(today)) {
return FMT_HHMM.format(ts);
} else if (d.isEqual(today.minusDays(1))) {
return YESTERDAY_LABEL + " " + FMT_HHMM.format(ts);
} else {
return FMT_DATE_TIME.format(ts);
}
}
/** ISO → LocalDateTime (با پشتیبانی از Offset/Z) */
private LocalDateTime parseWhen(String iso) {
if (iso == null || iso.isEmpty()) return null;
try {
return OffsetDateTime.parse(iso).toLocalDateTime();
} catch (Exception ignore) {
try {
return LocalDateTime.parse(iso);
} catch (Exception e) {
return null;
}
}
}
@FXML @FXML
private void openSearchPanel() { private void openSearchPanel() {
MainController.getInstance().showSearchPanel(); MainController.getInstance().showSearchPanel();
@@ -424,7 +422,6 @@ public class ChatPageController {
}).start(); }).start();
} }
private void renderMessages(org.json.JSONArray arr) { private void renderMessages(org.json.JSONArray arr) {
messageContainer.getChildren().clear(); messageContainer.getChildren().clear();
@@ -465,8 +462,6 @@ public class ChatPageController {
messageScrollPane.setVvalue(1.0); messageScrollPane.setVvalue(1.0);
} }
private void markAsRead(ChatEntry entry) { private void markAsRead(ChatEntry entry) {
JSONObject readReq = new JSONObject(); JSONObject readReq = new JSONObject();
readReq.put("action", "mark_as_read"); readReq.put("action", "mark_as_read");
@@ -508,7 +503,4 @@ public class ChatPageController {
messageContainer.getChildren().add(row); messageContainer.getChildren().add(row);
} }
} }
@@ -50,6 +50,12 @@ public class MainController {
@FXML private ImageView menuIcon; @FXML private ImageView menuIcon;
private SidebarMenuController sidebarController; //save sidebar controller private SidebarMenuController sidebarController; //save sidebar controller
// === Time formatter for messages ===
private static final java.time.format.DateTimeFormatter FMT_HHMM =
java.time.format.DateTimeFormatter.ofPattern("HH:mm");
private static final java.time.format.DateTimeFormatter FMT_DATE_TIME =
java.time.format.DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
private static final String YESTERDAY_LABEL = "Yesterday";
// === STATE === // === STATE ===
private static MainController instance; private static MainController instance;
@@ -174,13 +180,6 @@ public class MainController {
// //
// } // }
private static final java.time.format.DateTimeFormatter FMT_HHMM =
java.time.format.DateTimeFormatter.ofPattern("HH:mm");
private static final java.time.format.DateTimeFormatter FMT_DATE_TIME =
java.time.format.DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
private static final String YESTERDAY_LABEL = "Yesterday";
// ... // ...
private String formatChatTime(java.time.LocalDateTime ts) { private String formatChatTime(java.time.LocalDateTime ts) {
if (ts == null) return ""; if (ts == null) return "";
@@ -198,9 +197,6 @@ public class MainController {
} }
} }
private void populateChatListFromSession() { private void populateChatListFromSession() {
chatListContainer.getChildren().clear(); chatListContainer.getChildren().clear();
@@ -216,7 +212,6 @@ public class MainController {
} }
} }
// private void addChatNode(ChatEntry chat ) { // private void addChatNode(ChatEntry chat ) {
// try { // try {
// FXMLLoader fx = new FXMLLoader(getClass().getResource( // FXMLLoader fx = new FXMLLoader(getClass().getResource(
@@ -260,7 +255,6 @@ public class MainController {
} }
} }
private String mapTypeToLabel(String t) { private String mapTypeToLabel(String t) {
switch (t.toUpperCase()) { switch (t.toUpperCase()) {
case "IMAGE": return "[Image]"; case "IMAGE": return "[Image]";
@@ -271,7 +265,6 @@ public class MainController {
} }
} }
// private void addChat(String name, String lastMsg, String time, int unread) { // private void addChat(String name, String lastMsg, String time, int unread) {
// try { // try {
// FXMLLoader loader = new FXMLLoader(getClass().getResource("/org/to/telegramfinalproject/Fxml/chat_item.fxml")); // FXMLLoader loader = new FXMLLoader(getClass().getResource("/org/to/telegramfinalproject/Fxml/chat_item.fxml"));
@@ -197,7 +197,7 @@ public class SidebarMenuController {
String img = user.optString("image_url", ""); String img = user.optString("image_url", "");
Image pic = tryLoadImage(img); Image pic = tryLoadImage(img);
if (pic == null) { if (pic == null) {
pic = loadImage("/org/to/telegramfinalproject/Avatars/profile.png"); pic = loadImage("/org/to/telegramfinalproject/Avatars/default_profile.png");
} }
if (pic != null) profileImage.setImage(pic); if (pic != null) profileImage.setImage(pic);
} }
@@ -45,7 +45,7 @@ import java.io.IOException;
public class TelegramApplication extends Application { public class TelegramApplication extends Application {
@Override @Override
public void start(Stage stage) throws IOException { public void start(Stage stage) throws IOException {
// اول intro.fxml // First: intro.fxml
FXMLLoader fx = new FXMLLoader( FXMLLoader fx = new FXMLLoader(
TelegramApplication.class.getResource("/org/to/telegramfinalproject/Fxml/intro.fxml")); TelegramApplication.class.getResource("/org/to/telegramfinalproject/Fxml/intro.fxml"));
Scene scene = new Scene(fx.load(), 1480, 820); Scene scene = new Scene(fx.load(), 1480, 820);