-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Expanded and implemented profile user.
- Loading branch information
AdrianBalunan
committed
Apr 19, 2026
1 parent
f1b0c56
commit b5993b9
Showing
2 changed files
with
219 additions
and
75 deletions.
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
...on/src/main/java/ntnu/systemutvikling/team6/controller/profileUserSettingsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| package ntnu.systemutvikling.team6.controller; | ||
|
|
||
| import javafx.application.Platform; | ||
| import javafx.event.ActionEvent; | ||
| import javafx.fxml.FXML; | ||
| import javafx.scene.control.*; | ||
| import javafx.stage.Stage; | ||
| import ntnu.systemutvikling.team6.controller.components.BaseController; | ||
| import ntnu.systemutvikling.team6.controller.components.LoaderScene; | ||
| import ntnu.systemutvikling.team6.controller.components.NavbarController; | ||
| import ntnu.systemutvikling.team6.database.DAO.UserDAO; | ||
| import ntnu.systemutvikling.team6.database.DatabaseConnection; | ||
| import ntnu.systemutvikling.team6.models.user.*; | ||
|
|
||
| public class profileUserSettingsController extends BaseController { | ||
| @FXML private NavbarController navbarController; | ||
| @FXML private ComboBox<String> languageComboBox; | ||
| @FXML private ComboBox<String> themeComboBox; | ||
| @FXML private ToggleButton anonymousToggle; | ||
| @FXML private Label anonymousLabel; | ||
|
|
||
| @FXML private TextField NameField; | ||
| @FXML private TextField EmailField; | ||
| @FXML private PasswordField PassWordField; | ||
| @FXML private TextField ConfirmPasswordField; | ||
|
|
||
| @FXML private Button ConfirmChangeSettings; | ||
| @FXML private Button ConfirmChangeSettings1; | ||
|
|
||
|
|
||
|
|
||
| @Override | ||
| protected void authTokenisSet() { | ||
| if (!isLoggedin()){ | ||
| Stage stage = (Stage) anonymousLabel.getScene().getWindow(); | ||
| LoaderScene.LoadScene("FrontPage", stage,null,null,authToken); | ||
| } | ||
| navbarController.setAuthToken(authToken); | ||
| } | ||
| @FXML | ||
| public void initialize() { | ||
| languageComboBox.getItems().addAll("English", "Norwegian"); | ||
| themeComboBox.getItems().addAll("Light mode", "Dark mode"); | ||
|
|
||
| if (authToken != null && authToken.getCurrentUser().getSettings() != null) { | ||
| User user = authToken.getCurrentUser(); | ||
| Settings settings = user.getSettings(); | ||
| languageComboBox.setValue(settings.getLanguage() == Language.NORWEGIAN ? "Norwegian" : "English"); | ||
| themeComboBox.setValue(settings.isLightMode() ? "Light mode" : "Dark mode"); | ||
| anonymousToggle.setSelected(settings.isAnonymous()); | ||
| anonymousLabel.setText(settings.isAnonymous() ? "On" : "Off"); | ||
|
|
||
|
|
||
| NameField.setText(user.getUsername()); | ||
| EmailField.setText(user.getEmail()); | ||
| PassWordField.setText("Sett new password here"); | ||
| } else { | ||
| languageComboBox.setValue("English"); | ||
| themeComboBox.setValue("Light mode"); | ||
| } | ||
| } | ||
|
|
||
| @FXML | ||
| private void handleAnonymousToggle(ActionEvent event) { | ||
| boolean on = anonymousToggle.isSelected(); | ||
| anonymousLabel.setText(on ? "On" : "Off"); | ||
| anonymousToggle.setStyle( | ||
| "-fx-background-color: " + (on ? "#2f8f8b" : "#ccc") + ";" + | ||
| "-fx-background-radius: 14; -fx-min-width: 52; -fx-min-height: 28;" + | ||
| "-fx-max-width: 52; -fx-max-height: 28;" | ||
| ); | ||
| // save to settings | ||
| } | ||
|
|
||
| @FXML | ||
| private void handleNewProfile(ActionEvent event){ | ||
| String nameText = NameField.getText(); | ||
| String emailText = EmailField.getText(); | ||
| String password = PassWordField.getText(); | ||
| String confirmPassword = ConfirmPasswordField.getText(); | ||
|
|
||
| if (nameText.isBlank() || emailText.isBlank() || password.isBlank() || confirmPassword.isBlank()) { | ||
| showAlert(Alert.AlertType.ERROR, "Empty input", "Please fill out all fields"); | ||
| return; | ||
| } | ||
|
|
||
| if (emailText == null || emailText.isBlank() || !emailText.contains("@") || !emailText.contains(".")) { | ||
| showAlert(Alert.AlertType.ERROR, "Invalid Email", "Please enter a valid email"); | ||
| return; | ||
| } | ||
|
|
||
| if (!password.equals(confirmPassword)) { | ||
| showAlert(Alert.AlertType.ERROR, "Mismatch of password", "Password do not match"); | ||
| return; | ||
| } | ||
| User newUserCredentials= new User(nameText, emailText, password, Role.NORMAL_USER, new Settings(), new Inbox()); | ||
|
|
||
| boolean updateSuccess; | ||
| DatabaseConnection conn = new DatabaseConnection(); | ||
| UserDAO userDataObject = new UserDAO(conn); | ||
| try { | ||
| updateSuccess = userDataObject.updateUserDetails(newUserCredentials); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| showAlert(Alert.AlertType.ERROR, "Unexpected Error", "Unexpected error ocurred"); | ||
| return; | ||
| } | ||
| if (updateSuccess) { | ||
| showAlert( | ||
| Alert.AlertType.INFORMATION, | ||
| "Settings updated", | ||
| "You have successfully changed your settings"); | ||
| authToken.getCurrentUser().setUsername(nameText); | ||
| authToken.getCurrentUser().setPassword(password); | ||
| authToken.getCurrentUser().setEmail(emailText); | ||
| LoaderScene.LoadScene("profile_user_settings", event, null, null, authToken); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @FXML | ||
| private void handleNewPreferences(ActionEvent event) { | ||
| String selectedLanguage = languageComboBox.getValue(); | ||
| String selectedTheme = themeComboBox.getValue(); | ||
| boolean isAnonymous = anonymousToggle.isSelected(); | ||
|
|
||
| Settings updatedSettings = new Settings( | ||
| isAnonymous, | ||
| selectedLanguage.equals("Norwegian") ? Language.NORWEGIAN : Language.ENGLISH, | ||
| selectedTheme.equals("Light mode") | ||
| ); | ||
|
|
||
| DatabaseConnection conn = new DatabaseConnection(); | ||
| UserDAO userDataObject = new UserDAO(conn); | ||
| boolean updateSettingsSuccess; | ||
| try { | ||
| updateSettingsSuccess = userDataObject.updateUserSettings(authToken.getCurrentUser(), updatedSettings); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| showAlert(Alert.AlertType.ERROR, "Unexpected Error", "Unexpected error ocurred"); | ||
| return; | ||
| } | ||
| if (updateSettingsSuccess) { | ||
| showAlert( | ||
| Alert.AlertType.INFORMATION, | ||
| "Settings updated", | ||
| "You have successfully changed your settings"); | ||
| authToken.getCurrentUser().setSettings(updatedSettings); | ||
| LoaderScene.LoadScene("profile_user_settings", event, null, null, authToken); | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| private void handleLogout(ActionEvent event){ | ||
| authToken.logout(); | ||
| LoaderScene.LoadScene("FrontPage", event, null, null, authToken); | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters