diff --git a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java index 514c8ea..30bc646 100644 --- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java +++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java @@ -28,12 +28,17 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import java.io.IOException; +import java.util.List; /** * Class that controls the dashboard fxml file (the GUI Screen). */ @Controller public class DashBoardController { + public static ObservableList data = FXCollections.observableArrayList(); + public ObservableList friendLeaderData = FXCollections.observableArrayList(); + public ObservableList globalLeaderData = FXCollections.observableArrayList(); + @Autowired UserService userService; @@ -76,6 +81,18 @@ public class DashBoardController { @FXML private TableColumn scoreColumn; @FXML + private TableView globalLeaderboard; + @FXML + private TableColumn globalUser; + @FXML + private TableColumn globalScore; + @FXML + private TableView friendLeaderboard; + @FXML + private TableColumn friendUser; + @FXML + private TableColumn friendScore; + @FXML private PieChart pieChart; @FXML private Label usernameLabel; @@ -83,7 +100,7 @@ public class DashBoardController { /** * Loads the the necessary things before anything else. */ - public void initialize() { + public void initialize() throws InterruptedException { //sets the text of the 'welcome back' text to include the username welcomebacktext.setText("Welcome back, " + userService.currentUser.getName() + "!"); //adds the slide transition to the menu bar @@ -95,8 +112,11 @@ public class DashBoardController { friendsButton.setSkin(new MyButtonSkin(friendsButton)); friendsColumn.setCellValueFactory(new PropertyValueFactory<>("Friend")); scoreColumn.setCellValueFactory(new PropertyValueFactory<>("Score")); - friendsTable.setItems(FriendController.getData()); - if(pieChart != null) { + globalUser.setCellValueFactory(new PropertyValueFactory<>("Friend")); + globalScore.setCellValueFactory(new PropertyValueFactory<>("Score")); + friendUser.setCellValueFactory(new PropertyValueFactory<>("Friend")); + friendScore.setCellValueFactory(new PropertyValueFactory<>("Score")); + if (pieChart != null) { ObservableList pieChartData = FXCollections.observableArrayList( new PieChart.Data("Vegan Meal", 100), @@ -110,6 +130,37 @@ public class DashBoardController { pieChart.setMaxSize(1000, 1000); pieChart.setData(pieChartData); } + List friendList = userService.getFriendNames(userService.currentUser.getName()); + for (int i = 0; i < friendList.size(); i++) { + Friend friend = new Friend(friendList.get(i), + userService.getFootprint(friendList.get(i))); + data.add(friend); + } + friendsTable.setItems(data); + updateLeaderboard(); + } + + /** + * Sorts the scores of users. + * @param users the list of users + */ + public void sortScores(List users) throws InterruptedException { + for (int i = 0; i < users.size(); i++) { + for (int j = 0; j < users.size(); j++) { + Float firstScore = userService.getFootprint(users.get(i)); + Float secondScore = userService.getFootprint(users.get(j)); + if (i > j && firstScore < secondScore) { + String temp = users.get(i); + users.set(i, users.get(j)); + users.set(j, temp); + } + if (i < j && firstScore > secondScore) { + String temp = users.get(i); + users.set(i, users.get(j)); + users.set(j, temp); + } + } + } } /** @@ -117,7 +168,6 @@ public class DashBoardController { * @param node the node on which the transition needs to act */ public void addFadeTransition(Node node) { - fadeTrans = new FadeTransition(Duration.millis(400), node); fadeTrans.setFromValue(0); fadeTrans.setToValue(1.0); @@ -129,14 +179,14 @@ public class DashBoardController { * Displays the dashboard pane. * @param event the event (clicking the button) */ - public void displayDashboard(ActionEvent event) { + public void displayDashboard(ActionEvent event) throws InterruptedException { addFadeTransition(dashboardPane); System.out.println("display dashboard"); dashboardPane.setVisible(true); userPane.setVisible(false); activitiesPane.setVisible(false); friendsPane.setVisible(false); - + updateLeaderboard(); } /** @@ -193,7 +243,7 @@ public class DashBoardController { * Opens the calculator. * @throws IOException if the Application doesn't load. */ - public void openCalculator() throws IOException { + public void openCalculator() throws IOException, InterruptedException { Parent calc = Application.load(this.getClass().getClassLoader() .getResource("fxml/calculator.fxml")); Scene scene = new Scene(calc); @@ -206,6 +256,10 @@ public class DashBoardController { calcStage.show(); } + /** + * method opend addFriend scene. + * @throws IOException when file is not found + */ public void openAddFriend() throws IOException { Parent calc = Application.load(this.getClass().getClassLoader() .getResource("fxml/AddFriend.fxml")); @@ -216,6 +270,37 @@ public class DashBoardController { calcStage.show(); } + /** + * Leaderboard is updaating. + * @throws InterruptedException throws exception + */ + public void updateLeaderboard() throws InterruptedException { + friendLeaderboard.getItems().clear(); + globalLeaderboard.getItems().clear(); + //global leaderboard + globalLeaderData.removeAll(); + List userList = userService.getAllUsers(); + sortScores(userList); + for (int j = 0; j < userList.size(); j++) { + Friend user = new Friend(userList.get(j), userService.getFootprint(userList.get(j))); + globalLeaderData.add(user); + } + globalLeaderboard.setItems(globalLeaderData); + // friend leaderboard + friendLeaderData.removeAll(); + String name = userService.currentUser.getName(); + List friendList = userService.getFriendNames(name); + friendList.add(name); + sortScores(friendList); + for (int i = 0; i < friendList.size(); i++) { + Friend friend = new Friend(friendList.get(i), + userService.getFootprint(friendList.get(i))); + friendLeaderData.add(friend); + } + friendLeaderboard.setItems(friendLeaderData); + } + + //class for the animations on the navigation buttons public class MyButtonSkin extends ButtonSkin { /** diff --git a/src/Client/src/main/java/greenify/client/controller/FriendController.java b/src/Client/src/main/java/greenify/client/controller/FriendController.java index e364f4f..25992cb 100644 --- a/src/Client/src/main/java/greenify/client/controller/FriendController.java +++ b/src/Client/src/main/java/greenify/client/controller/FriendController.java @@ -2,11 +2,11 @@ package greenify.client.controller; import greenify.client.Friend; import greenify.client.rest.UserService; -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.scene.control.*; +import javafx.scene.control.Alert; +import javafx.scene.control.Button; +import javafx.scene.control.TextField; import javafx.stage.Stage; import javafx.stage.Window; import org.springframework.beans.factory.annotation.Autowired; @@ -17,7 +17,8 @@ public class FriendController { @Autowired UserService userService; - public static ObservableList data = FXCollections.observableArrayList(); + @Autowired + DashBoardController controller; @FXML private Button addButton; @@ -29,7 +30,7 @@ public class FriendController { * @param event the click of the sign up button */ @FXML - public void addFriend(ActionEvent event) { + public void addFriend(ActionEvent event) throws InterruptedException { //set the window to the current window (for displaying the alerts) Window owner = addButton.getScene().getWindow(); //check if the username field is empty @@ -38,19 +39,28 @@ public class FriendController { UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Username Error!", "Please enter a username!"); return; + } else if (userNameText.getText().equals(userService.currentUser.getName())) { + UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Error!", + "Cannot add yourself as a friend!"); + return; + } else if (userService.getFriendNames(userService.currentUser.getName()) + .contains(userNameText.getText())) { + UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Error!", + "Cannot add a friend twice!"); + return; + } else if (!userService.getAllUsers().contains(userNameText.getText())) { + UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Error!", + "The user does not exist!"); + return; } //add friend to the current user userService.addFriend(userService.currentUser.getName(), userNameText.getText()); Friend friend = new Friend(userNameText.getText(), userService.getFootprint(userNameText.getText())); - data.add(friend); + DashBoardController.data.add(friend); //close the register window after the user has entered all the credentials Stage current = (Stage) owner; current.close(); + controller.updateLeaderboard(); } - - public static ObservableList getData() { - return data; - } - } diff --git a/src/Client/src/main/java/greenify/client/rest/UserService.java b/src/Client/src/main/java/greenify/client/rest/UserService.java index cd2d035..f07534f 100644 --- a/src/Client/src/main/java/greenify/client/rest/UserService.java +++ b/src/Client/src/main/java/greenify/client/rest/UserService.java @@ -12,6 +12,8 @@ import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; +import java.util.List; + @Service public class UserService { public UserDto currentUser; @@ -117,6 +119,42 @@ public class UserService { return result; } + /** + * Saves the footprint of the user. + * @param name name of the user + * @return returns the footprint score + */ + @SuppressWarnings("Duplicates") + public Float saveFootprint(String name) { + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/saveFootprint") + .queryParam("name", name); + new HttpEntity<>(headers); + System.out.println(builder.build().encode().toUri()); + Float result = this.restTemplate.getForObject(builder + .build().encode().toUri(), Float.class); + return result; + } + + /** + * Gets the friend list of the user. + * @param name name of the user + * @return returns the friend list + */ + @SuppressWarnings("Duplicates") + public List getFriendNames(String name) { + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getFriends") + .queryParam("name", name); + new HttpEntity<>(headers); + System.out.println(builder.build().encode().toUri()); + List result = this.restTemplate.getForObject(builder + .build().encode().toUri(), List.class); + return result; + } + /** * Adds a friend to the user. * @param name the username of the current user. @@ -134,4 +172,19 @@ public class UserService { ResponseEntity authenticateResponse = this.restTemplate.getForEntity(builder.build() .encode().toUri(), String.class); } + + /** + * Gets the list of all users. + */ + @SuppressWarnings("Duplicates") + public List getAllUsers() { + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getAllUsers"); + HttpEntity entity = new HttpEntity<>(headers); + System.out.println(builder.build().encode().toUri()); + List result = this.restTemplate.getForObject(builder + .build().encode().toUri(), List.class); + return result; + } } diff --git a/src/Client/src/main/resources/fxml/dashboard.fxml b/src/Client/src/main/resources/fxml/dashboard.fxml index d10b9eb..92c0dd6 100644 --- a/src/Client/src/main/resources/fxml/dashboard.fxml +++ b/src/Client/src/main/resources/fxml/dashboard.fxml @@ -1,16 +1,20 @@ - - - - - - + + + + + + + + + - + + - + @@ -258,6 +262,28 @@ + + + + + + + + + + + + + +