Edit profile and username/Password from setting

This commit is contained in:
2025-09-05 00:12:25 +03:30
parent 9dbdf89831
commit fbdf10fb13
4 changed files with 241 additions and 87 deletions
@@ -4,13 +4,16 @@ import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import org.json.JSONObject;
import org.to.telegramfinalproject.Client.ActionHandler;
import org.to.telegramfinalproject.Client.Session;
public class ChangeCredentialsController {
@FXML private VBox credentialsCard;
@FXML private Pane overlayBackground;
@FXML private TextField usernameField;
@FXML private Label usernameLabel;
@@ -31,22 +34,21 @@ public class ChangeCredentialsController {
private boolean passwordVisible = false;
private boolean confirmVisible = false;
// ↓↓↓ متدی که از CheckPasswordController مقدار می‌گیرد
private String currentPassword;
public void setCurrentPassword(String p) { this.currentPassword = p; }
@FXML
private void initialize() {
// Toggle password visibility
togglePasswordBtn.setOnAction(e -> togglePasswordVisibility());
toggleConfirmBtn.setOnAction(e -> toggleConfirmVisibility());
saveButton.setOnAction(e -> validateAndSave());
// Close the overlay
closeButton.setOnAction(e ->
MainController.getInstance().closeOverlay(credentialsCard.getParent()));
cancelButton.setOnAction(e ->
MainController.getInstance().closeOverlay(credentialsCard.getParent()));
overlayBackground.setOnMouseClicked(e ->
MainController.getInstance().closeOverlay(credentialsCard.getParent())
);
// «ذخیره و بستن» مثل ادیت پروفایل
saveButton.setOnAction(e -> saveAndClose());
closeButton.setOnAction(e -> saveAndClose());
cancelButton.setOnAction(e -> saveAndClose());
overlayBackground.setOnMouseClicked(e -> saveAndClose());
// Reset error when typing
usernameField.textProperty().addListener((obs, o, n) -> removeError(usernameField, usernameLabel));
@@ -73,10 +75,19 @@ public class ChangeCredentialsController {
removeError(visibleConfirmPasswordField, confirmPasswordLabel);
});
// Auto_focus password field when overlay opens
Platform.runLater(() -> usernameField.requestFocus());
}
// ← از CheckPasswordController بلافاصله بعد از load صدا بزن
public void prefillFromSession() {
var u = Session.currentUser;
if (u == null) return;
String handle = u.optString("user_id",
u.optString("username",
u.optString("display_id","")));
usernameField.setText(handle);
}
private void togglePasswordVisibility() {
passwordVisible = !passwordVisible;
visiblePasswordField.setText(passwordField.getText());
@@ -101,47 +112,68 @@ public class ChangeCredentialsController {
toggleConfirmBtn.setText(confirmVisible ? "👁" : "👁");
}
private void validateAndSave() {
boolean hasError = false;
// ارسال تغییرات (در صورت پر بودن/تغییر) و بستن
private void saveAndClose() {
boolean anyError = false;
boolean sentSomething = false;
// --- Username check ---
if (usernameField.getText().trim().isEmpty()) {
addError(usernameField, usernameLabel);
hasError = true;
} else {
removeError(usernameField, usernameLabel);
// 1) یوزرنیم
var cu = Session.currentUser;
String oldUsername = cu != null ? cu.optString("user_id",
cu.optString("username",
cu.optString("display_id",""))) : "";
String newUsername = usernameField.getText().trim();
if (!newUsername.isEmpty() && !newUsername.equals(oldUsername)) {
sentSomething = true;
JSONObject req = new JSONObject()
.put("action", "update_username")
.put("current_password", currentPassword) // با اینکه سرور فعلاً چک نمی‌کند، می‌فرستیم
.put("new_username", newUsername);
JSONObject resp = ActionHandler.sendWithResponse(req);
if (resp == null || !"success".equalsIgnoreCase(resp.optString("status"))) {
anyError = true;
showError("Changing username failed",
resp != null ? resp.optString("message","Unknown error") : "No response");
} else {
if (cu != null) cu.put("user_id", newUsername);
}
}
// --- Password check ---
String pwd = passwordVisible ? visiblePasswordField.getText().trim() : passwordField.getText().trim();
if (pwd.isEmpty()) {
addError(passwordField, passwordLabel);
addError(visiblePasswordField, passwordLabel);
hasError = true;
} else {
removeError(passwordField, passwordLabel);
removeError(visiblePasswordField, passwordLabel);
// 2) پسورد
String newPwd = (passwordVisible ? visiblePasswordField.getText() : passwordField.getText()).trim();
String confirm = (confirmVisible ? visibleConfirmPasswordField.getText() : confirmPasswordField.getText()).trim();
if (!newPwd.isEmpty()) {
if (confirm.isEmpty() || !confirm.equals(newPwd)) {
addError(confirmPasswordField, confirmPasswordLabel);
addError(visibleConfirmPasswordField, confirmPasswordLabel);
return; // نذار بسته شه تا کاربر درست کنه
}
sentSomething = true;
JSONObject req = new JSONObject()
.put("action", "update_password")
.put("current_password", currentPassword)
.put("new_password", newPwd);
JSONObject resp = ActionHandler.sendWithResponse(req);
if (resp == null || !"success".equalsIgnoreCase(resp.optString("status"))) {
anyError = true;
showError("Changing password failed",
resp != null ? resp.optString("message","Unknown error") : "No response");
}
}
// --- Confirm password check ---
String confirm = confirmVisible ? visibleConfirmPasswordField.getText().trim() : confirmPasswordField.getText().trim();
if (confirm.isEmpty()) {
addError(confirmPasswordField, confirmPasswordLabel);
addError(visibleConfirmPasswordField, confirmPasswordLabel);
hasError = true;
} else if (!confirm.equals(pwd)) {
addError(confirmPasswordField, confirmPasswordLabel);
addError(visibleConfirmPasswordField, confirmPasswordLabel);
hasError = true;
} else {
removeError(confirmPasswordField, confirmPasswordLabel);
removeError(visibleConfirmPasswordField, confirmPasswordLabel);
// اگر چیزی نفرستادیم یا همه‌چیز OK بود → ببند و رفرش Settings
if (!sentSomething || !anyError) {
var sc = SettingsController.getInstance();
if (sc != null) sc.populateFromSession();
MainController.getInstance().closeOverlay(credentialsCard.getParent());
}
if (hasError) return;
// TODO: send to server
System.out.println("Saving new credentials...");
}
private void addError(TextField field, Label label) {
@@ -157,4 +189,15 @@ public class ChangeCredentialsController {
field.getStyleClass().remove("error-input");
label.getStyleClass().remove("error-label");
}
private void showError(String title, String msg) {
Alert a = new Alert(Alert.AlertType.ERROR);
a.setTitle(title);
a.setHeaderText(null);
a.setContentText(msg);
if (a.getDialogPane().getScene()!=null) {
ThemeManager.getInstance().registerScene(a.getDialogPane().getScene());
}
a.showAndWait();
}
}