diff --git a/client/src/main/java/client/Main.java b/client/src/main/java/client/Main.java index f281bb7..74adf6f 100644 --- a/client/src/main/java/client/Main.java +++ b/client/src/main/java/client/Main.java @@ -50,9 +50,11 @@ public class Main extends Application { var addName = FXML.load(AddNameCtrl.class, "client", "scenes", "AddName.fxml"); var foodpal = FXML.load(FoodpalApplicationCtrl.class, "client", "scenes", "FoodpalApplication.fxml"); var addIngredient = FXML.load(AddIngredientCtrl.class, "client", "scenes", "AddIngredient.fxml"); + var addStep = FXML.load(AddStepsCtrl.class, "client", "scenes", "AddSteps.fxml"); + var mainCtrl = INJECTOR.getInstance(MainCtrl.class); - mainCtrl.initialize(primaryStage, overview, addName, foodpal, addIngredient); + mainCtrl.initialize(primaryStage, overview, addName, foodpal, addIngredient, addStep); } } \ No newline at end of file diff --git a/client/src/main/java/client/scenes/AddIngredientCtrl.java b/client/src/main/java/client/scenes/AddIngredientCtrl.java index 0f3bc7a..11fcb25 100644 --- a/client/src/main/java/client/scenes/AddIngredientCtrl.java +++ b/client/src/main/java/client/scenes/AddIngredientCtrl.java @@ -19,13 +19,13 @@ import client.utils.ServerUtils; import com.google.inject.Inject; import commons.Recipe; import jakarta.ws.rs.WebApplicationException; +import javafx.fxml.FXML; import javafx.scene.control.Alert; import javafx.scene.control.TextField; import javafx.scene.input.KeyEvent; import javafx.stage.Modality; import java.io.IOException; -import java.util.List; public class AddIngredientCtrl { @@ -33,7 +33,8 @@ public class AddIngredientCtrl { private final MainCtrl mainCtrl; private final FoodpalApplicationCtrl foodpalCtrl; - public TextField recipeName; + @FXML + public TextField ingredient; @Inject @@ -51,7 +52,7 @@ public class AddIngredientCtrl { public void ok() throws IOException, InterruptedException { try { Recipe selected = foodpalCtrl.getSelectedRecipe(); - server.addRecipeIngredient(selected, recipeName.getText()); + server.addRecipeIngredient(selected, ingredient.getText()); } catch (WebApplicationException e) { var alert = new Alert(Alert.AlertType.ERROR); @@ -67,7 +68,7 @@ public class AddIngredientCtrl { } private void clearFields() { - recipeName.clear(); + ingredient.clear(); } public void keyPressed(KeyEvent e) throws IOException, InterruptedException { diff --git a/client/src/main/java/client/scenes/AddStepsCtrl.java b/client/src/main/java/client/scenes/AddStepsCtrl.java new file mode 100644 index 0000000..e042f66 --- /dev/null +++ b/client/src/main/java/client/scenes/AddStepsCtrl.java @@ -0,0 +1,86 @@ +/* + * Copyright 2021 Delft University of Technology + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package client.scenes; + +import client.utils.ServerUtils; +import com.google.inject.Inject; +import commons.Recipe; +import jakarta.ws.rs.WebApplicationException; +import javafx.fxml.FXML; +import javafx.scene.control.Alert; +import javafx.scene.control.TextField; +import javafx.scene.input.KeyEvent; +import javafx.stage.Modality; + +import java.io.IOException; + +public class AddStepsCtrl { + + private final ServerUtils server; + private final MainCtrl mainCtrl; + private final FoodpalApplicationCtrl foodpalCtrl; + + @FXML + public TextField preparationStep; + + + @Inject + public AddStepsCtrl(ServerUtils server, MainCtrl mainCtrl, FoodpalApplicationCtrl foodpalCtrl) { + this.mainCtrl = mainCtrl; + this.server = server; + this.foodpalCtrl = foodpalCtrl; + } + + public void cancel() throws IOException, InterruptedException { + clearFields(); + mainCtrl.showFoodpal(); + } + + public void ok() throws IOException, InterruptedException { + try { + Recipe selected = foodpalCtrl.getSelectedRecipe(); + server.addRecipeStep(selected, preparationStep.getText()); + } catch (WebApplicationException e) { + + var alert = new Alert(Alert.AlertType.ERROR); + alert.initModality(Modality.APPLICATION_MODAL); + alert.setContentText(e.getMessage()); + alert.showAndWait(); + return; + } + + + clearFields(); + mainCtrl.showFoodpal(); + } + + private void clearFields() { + preparationStep.clear(); + } + + public void keyPressed(KeyEvent e) throws IOException, InterruptedException { + switch (e.getCode()) { + case ENTER: + ok(); + break; + case ESCAPE: + cancel(); + break; + default: + break; + } + } +} \ No newline at end of file diff --git a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java index 5141305..610c919 100644 --- a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java +++ b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java @@ -168,7 +168,7 @@ public class FoodpalApplicationCtrl { /** * You can add ingredients. you get send to a different scene that creates the ingredient - * It doesn't automaticcaly refresh yet so before you can see the new ingredient you have to first click on a + * It doesn't automatically refresh yet so before you can see the new ingredient you have to first click on a * different recipe and come back after */ @FXML @@ -177,10 +177,16 @@ public class FoodpalApplicationCtrl { mainCtrl.showAddIngredient(); } + /** + * You can add steps. you get send to a different scene that creates the Step + * It doesn't automatically refresh yet so before you can see the new step you have to first click on a + * different recipe and come back after + */ @FXML private void addPreparationStep() { - // TODO: make it possible to add step to current recipe System.out.println("Add preparation step clicked"); + mainCtrl.showAddSteps(); + } // Language buttons @@ -200,11 +206,16 @@ public class FoodpalApplicationCtrl { } + //without caused errors @FXML private void MakePrintable() { System.out.println("Recipe printed"); } + /** + * Get the recipe that was selected + * @return the selected recipe + */ public Recipe getSelectedRecipe() { return recipeList.getSelectionModel().getSelectedItem(); } diff --git a/client/src/main/java/client/scenes/MainCtrl.java b/client/src/main/java/client/scenes/MainCtrl.java index a1345e6..6e24fd1 100644 --- a/client/src/main/java/client/scenes/MainCtrl.java +++ b/client/src/main/java/client/scenes/MainCtrl.java @@ -15,7 +15,6 @@ */ package client.scenes; -import commons.Recipe; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; @@ -36,14 +35,18 @@ public class MainCtrl { private FoodpalApplicationCtrl foodpalCtrl; private Scene foodpal; - private AddIngredientCtrl AddIngredientCtrl; + private AddIngredientCtrl addIngredientCtrl; private Scene addIngredient; + private AddStepsCtrl addStepsCtrl; + private Scene addStep; + public void initialize(Stage primaryStage, Pair overview, Pair addName, Pair foodpal, - Pair addIngredient + Pair addIngredient, + Pair addStep ) throws IOException, InterruptedException { this.primaryStage = primaryStage; @@ -57,9 +60,12 @@ public class MainCtrl { this.foodpalCtrl = foodpal.getKey(); this.foodpal = new Scene(foodpal.getValue()); - this.AddIngredientCtrl = addIngredient.getKey(); + this.addIngredientCtrl = addIngredient.getKey(); this.addIngredient = new Scene(addIngredient.getValue()); + this.addStepsCtrl = addStep.getKey(); + this.addStep = new Scene(addStep.getValue()); + showFoodpal(); primaryStage.show(); @@ -90,11 +96,24 @@ public class MainCtrl { } public void showAddIngredient() { - primaryStage.setTitle("adding ingredients"); + primaryStage.setTitle("To add ingredients"); primaryStage.setScene(addIngredient); - addName.setOnKeyPressed(e -> { + addIngredient.setOnKeyPressed(e -> { try { - AddIngredientCtrl.keyPressed(e); + addIngredientCtrl.keyPressed(e); + } catch (IOException | InterruptedException ex) { + throw new RuntimeException(ex); + } + }); + + } + + public void showAddSteps() { + primaryStage.setTitle("To add preparation steps"); + primaryStage.setScene(addStep); + addStep.setOnKeyPressed(e -> { + try { + addStepsCtrl.keyPressed(e); } catch (IOException | InterruptedException ex) { throw new RuntimeException(ex); } diff --git a/client/src/main/java/client/utils/ServerUtils.java b/client/src/main/java/client/utils/ServerUtils.java index be1eda7..c5f812c 100644 --- a/client/src/main/java/client/utils/ServerUtils.java +++ b/client/src/main/java/client/utils/ServerUtils.java @@ -191,4 +191,12 @@ public class ServerUtils { return objectMapper.readValue(response.body(),Recipe.class); } + + public Recipe addRecipeStep(Recipe recipe, String preparationStep) throws IOException, InterruptedException { + List preparationSteps = new ArrayList<>(recipe.getPreparationSteps()); + preparationSteps.add(preparationStep); + recipe.setPreparationSteps(preparationSteps); + + return updateRecipe(recipe); + } } \ No newline at end of file diff --git a/client/src/main/resources/client/scenes/AddIngredient.fxml b/client/src/main/resources/client/scenes/AddIngredient.fxml index 44b677f..ebb0970 100644 --- a/client/src/main/resources/client/scenes/AddIngredient.fxml +++ b/client/src/main/resources/client/scenes/AddIngredient.fxml @@ -9,7 +9,7 @@