Adding preparation steps is possible, but no automatically refresh

This commit is contained in:
Mei Chang van der Werff 2025-12-02 17:52:11 +01:00
commit 1414393aad
8 changed files with 158 additions and 16 deletions

View file

@ -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);
}
}

View file

@ -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 {

View file

@ -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;
}
}
}

View file

@ -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();
}

View file

@ -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<QuoteOverviewCtrl, Parent> overview,
Pair<AddNameCtrl, Parent> addName,
Pair<FoodpalApplicationCtrl, Parent> foodpal,
Pair<AddIngredientCtrl, Parent> addIngredient
Pair<AddIngredientCtrl, Parent> addIngredient,
Pair<AddStepsCtrl, Parent> 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);
}

View file

@ -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<String> preparationSteps = new ArrayList<>(recipe.getPreparationSteps());
preparationSteps.add(preparationStep);
recipe.setPreparationSteps(preparationSteps);
return updateRecipe(recipe);
}
}

View file

@ -9,7 +9,7 @@
<children>
<Button layoutX="417.0" layoutY="54.0" mnemonicParsing="false" onAction="#ok" text="Ok" />
<Button layoutX="349.0" layoutY="54.0" mnemonicParsing="false" onAction="#cancel" text="Cancel" />
<TextField fx:id="recipeName" layoutX="109.0" layoutY="24.0" prefHeight="18.0" prefWidth="292.0" />
<Label layoutX="28.0" layoutY="29.0" text="Recipe Name" />
<TextField fx:id="ingredient" layoutX="120.0" layoutY="24.0" prefHeight="18.0" prefWidth="292.0" />
<Label layoutX="28.0" layoutY="29.0" text="Ingredient Name" />
</children>
</AnchorPane>

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="94.0" prefWidth="470.0" xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.scenes.AddStepsCtrl">
<children>
<Button layoutX="417.0" layoutY="54.0" mnemonicParsing="false" onAction="#ok" text="Ok" />
<Button layoutX="349.0" layoutY="54.0" mnemonicParsing="false" onAction="#cancel" text="Cancel" />
<TextField fx:id="preparationStep" layoutX="120.0" layoutY="24.0" prefHeight="18.0" prefWidth="292.0" />
<Label layoutX="28.0" layoutY="29.0" text="Preparation step" />
</children>
</AnchorPane>