diff --git a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java index 7ba5d3d..b6377d7 100644 --- a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java +++ b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java @@ -2,12 +2,10 @@ package client.scenes; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.logging.Logger; -import java.util.stream.Collectors; import client.exception.InvalidModificationException; import client.scenes.nutrition.NutritionViewCtrl; @@ -21,7 +19,6 @@ import client.utils.LocaleManager; import client.utils.server.ServerUtils; import client.utils.WebSocketDataService; import client.utils.WebSocketUtils; -import commons.Ingredient; import commons.Recipe; import commons.ws.Topics; @@ -42,7 +39,6 @@ import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; -import javafx.scene.control.TextInputDialog; import javafx.scene.control.ToggleButton; import javafx.scene.paint.Color; import org.apache.commons.lang3.NotImplementedException; @@ -463,119 +459,6 @@ public class FoodpalApplicationCtrl implements LocaleAware { this.recipeDetailController.refreshFavouriteButton(); this.recipeDetailController.setVisible(!recipeList.getItems().isEmpty()); } - - //Delete Ingredient button click - @FXML - private void handleDeleteIngredient() { - // Get selected ingredient - Ingredient selectedIngredient = ingredientListView.getSelectionModel().getSelectedItem(); - - if (selectedIngredient == null) { - // Show an error message if no ingredient is selected - showError("No ingredient selected", "Please select an ingredient to delete."); - return; - } - - // Check if the ingredient is used in any recipe - checkIngredientUsage(selectedIngredient); - } - - // Check if ingredient is used in any recipe before deleting - private void checkIngredientUsage(Ingredient ingredient) { - try { - long usageCount = server.getIngredientUsage(ingredient.getId()); // Check ingredient usage via ServerUtils - - if (usageCount > 0) { - // If ingredient is used, show a warning dialog - showWarningDialog(ingredient, usageCount); - } else { - // If not used, delete - deleteIngredient(ingredient); - } - } catch (IOException | InterruptedException e) { - showError("Error", "Failed to check ingredient usage: " + e.getMessage()); - } - } - - private void deleteIngredient(Ingredient ingredient) { - try { - server.deleteIngredient(ingredient.getId()); // Call ServerUtils to delete the ingredient - showConfirmation("Success", "Ingredient '" + ingredient.getName() + "' has been deleted."); - refreshIngredientList(); // refresh the ingredient list - } catch (IOException | InterruptedException e) { - showError("Error", "Failed to delete ingredient: " + e.getMessage()); - } - } - - private void showWarningDialog(Ingredient ingredient, long usedInRecipes) { - Alert alert = new Alert(Alert.AlertType.WARNING); - } - - private void showError(String title, String message) { - Alert alert = new Alert(Alert.AlertType.ERROR); - alert.setTitle(title); - alert.setHeaderText(null); - alert.setContentText(message); - alert.showAndWait(); - } - - private void showConfirmation(String title, String message) { - Alert alert = new Alert(Alert.AlertType.INFORMATION); - alert.setTitle(title); - alert.setHeaderText(null); - alert.setContentText(message); - alert.showAndWait(); - } - - private void refreshIngredientList() { - // Refresh - ingredientListView.getItems().clear(); - - } - - - @FXML - private void handleAddIngredient() { - //ask the user for the ingredient name - TextInputDialog dialog = new TextInputDialog(); - dialog.setTitle("Add Ingredient"); - dialog.setHeaderText("Enter the ingredient name:"); - dialog.setContentText("Ingredient:"); - - // Wait for the user to enter a value - Optional result = dialog.showAndWait(); - - result.ifPresent(recipeName ->{ - String trim = recipeName.trim(); - if(trim.isEmpty()){ - showError("Invalid Input", "Cannot have empty ingredient name"); - return; - } - if(ingredientListView.getItems().stream() - .map(Ingredient::getName) - .anyMatch(ingName -> ingName.equalsIgnoreCase(trim))){ - showError("Duplicate Ingredient", "Cannot have duplicate ingredients," + - " Please provide unique Ingredient names"); - } - }); - } - - // display ingredient name - @FXML - private void initializeIngredients() { - ingredientListView.setCellFactory(list -> new ListCell() { - @Override - protected void updateItem(Ingredient item, boolean empty) { - super.updateItem(item, empty); - if (empty || item == null) { - setText(null); - } else { - setText(item.getName()); // Display the ingredient name in the ListView - } - } - }); - } - @FXML private void openIngredientsPopup() { try {