From bdf6f71c017285d8def051215574f25d068abe3d Mon Sep 17 00:00:00 2001 From: Natalia Cholewa Date: Thu, 27 Nov 2025 16:25:44 +0100 Subject: [PATCH] feat: ingredient adding, deletion and editing --- .../scenes/recipe/IngredientListCtrl.java | 69 ++++++++++++++++--- .../client/scenes/recipe/IngredientList.fxml | 27 ++++++-- 2 files changed, 81 insertions(+), 15 deletions(-) diff --git a/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java b/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java index 0accd3b..cffdd05 100644 --- a/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java +++ b/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java @@ -1,39 +1,90 @@ package client.scenes.recipe; import commons.Recipe; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.ResourceBundle; import javafx.collections.FXCollections; +import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.Initializable; +import javafx.scene.control.Button; import javafx.scene.control.ListView; -import java.net.URL; -import java.util.ResourceBundle; - public class IngredientListCtrl implements Initializable { private Recipe recipe; + private ObservableList ingredients; @FXML ListView ingredientListView; - public IngredientListCtrl() {} + @FXML + Button addIngredientButton; + @FXML + Button deleteIngredientButton; public void setRecipe(Recipe recipe) { this.recipe = recipe; + this.fetchFromRecipe(); + } + + private void fetchFromRecipe() { + if (recipe == null) { + this.ingredients = FXCollections.observableArrayList(new ArrayList<>()); + } else { + List ingredientList = recipe.getIngredients(); + this.ingredients = FXCollections.observableArrayList(ingredientList); + } + + this.ingredientListView.setItems(this.ingredients); this.refresh(); } - private void refresh() { - if (recipe == null) { - return; + private void updateRecipeIngredients() { + if (this.recipe != null) { + List updatedIngredients = new ArrayList<>(this.ingredients); + this.recipe.setIngredients(updatedIngredients); } + } - var ingredients = recipe.getIngredients(); - ingredientListView.setItems(FXCollections.observableList(ingredients)); + private void refresh() { ingredientListView.refresh(); } @FXML public void initialize(URL location, ResourceBundle resources) { + this.ingredientListView.setEditable(true); + this.ingredientListView.setCellFactory( + list -> { + return new IngredientListCell(); + }); + + this.ingredientListView.setOnEditCommit(event -> { + int index = event.getIndex(); + String newValue = event.getNewValue(); + this.ingredients.set(index, newValue); + this.updateRecipeIngredients(); + }); + + // TODO: actually communicate with the server :) + this.addIngredientButton.setOnAction(event -> { + this.ingredients.add("Ingredient " + (this.ingredients.size() + 1)); + this.updateRecipeIngredients(); + + this.ingredientListView.getSelectionModel().select( + this.ingredients.size() - 1); + }); + + this.deleteIngredientButton.setOnAction(event -> { + int selectedIndex = this.ingredientListView.getSelectionModel().getSelectedIndex(); + if (selectedIndex >= 0) { + this.ingredients.remove(selectedIndex); + } + + this.updateRecipeIngredients(); + }); + this.refresh(); } } diff --git a/client/src/main/resources/client/scenes/recipe/IngredientList.fxml b/client/src/main/resources/client/scenes/recipe/IngredientList.fxml index 840f55b..1ad02e4 100644 --- a/client/src/main/resources/client/scenes/recipe/IngredientList.fxml +++ b/client/src/main/resources/client/scenes/recipe/IngredientList.fxml @@ -1,9 +1,11 @@ + + @@ -11,14 +13,27 @@ -