diff --git a/client/src/main/java/client/Main.java b/client/src/main/java/client/Main.java index 7a7137b..f281bb7 100644 --- a/client/src/main/java/client/Main.java +++ b/client/src/main/java/client/Main.java @@ -20,12 +20,9 @@ import static com.google.inject.Guice.createInjector; import java.io.IOException; import java.net.URISyntaxException; -import client.scenes.FoodpalApplicationCtrl; +import client.scenes.*; import com.google.inject.Injector; -import client.scenes.AddQuoteCtrl; -import client.scenes.MainCtrl; -import client.scenes.QuoteOverviewCtrl; import client.utils.ServerUtilsExample; import javafx.application.Application; import javafx.stage.Stage; @@ -50,10 +47,12 @@ public class Main extends Application { } var overview = FXML.load(QuoteOverviewCtrl.class, "client", "scenes", "QuoteOverview.fxml"); - var add = FXML.load(AddQuoteCtrl.class, "client", "scenes", "AddQuote.fxml"); + 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 mainCtrl = INJECTOR.getInstance(MainCtrl.class); - mainCtrl.initialize(primaryStage, overview, add, foodpal); + mainCtrl.initialize(primaryStage, overview, addName, foodpal, addIngredient); } } \ No newline at end of file diff --git a/client/src/main/java/client/MyModule.java b/client/src/main/java/client/MyModule.java index 2f6678b..c842dde 100644 --- a/client/src/main/java/client/MyModule.java +++ b/client/src/main/java/client/MyModule.java @@ -15,11 +15,12 @@ */ package client; +import client.scenes.FoodpalApplicationCtrl; import com.google.inject.Binder; import com.google.inject.Module; import com.google.inject.Scopes; -import client.scenes.AddQuoteCtrl; +import client.scenes.AddNameCtrl; import client.scenes.MainCtrl; import client.scenes.QuoteOverviewCtrl; @@ -28,7 +29,8 @@ public class MyModule implements Module { @Override public void configure(Binder binder) { binder.bind(MainCtrl.class).in(Scopes.SINGLETON); - binder.bind(AddQuoteCtrl.class).in(Scopes.SINGLETON); + binder.bind(AddNameCtrl.class).in(Scopes.SINGLETON); + binder.bind(FoodpalApplicationCtrl.class).in(Scopes.SINGLETON); binder.bind(QuoteOverviewCtrl.class).in(Scopes.SINGLETON); } } \ 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 new file mode 100644 index 0000000..0f3bc7a --- /dev/null +++ b/client/src/main/java/client/scenes/AddIngredientCtrl.java @@ -0,0 +1,85 @@ +/* + * 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.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 { + + private final ServerUtils server; + private final MainCtrl mainCtrl; + private final FoodpalApplicationCtrl foodpalCtrl; + + public TextField recipeName; + + + @Inject + public AddIngredientCtrl(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.addRecipeIngredient(selected, recipeName.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() { + recipeName.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/AddQuoteCtrl.java b/client/src/main/java/client/scenes/AddNameCtrl.java similarity index 87% rename from client/src/main/java/client/scenes/AddQuoteCtrl.java rename to client/src/main/java/client/scenes/AddNameCtrl.java index 4d7a82f..354c653 100644 --- a/client/src/main/java/client/scenes/AddQuoteCtrl.java +++ b/client/src/main/java/client/scenes/AddNameCtrl.java @@ -19,7 +19,6 @@ import client.utils.ServerUtils; import com.google.inject.Inject; import jakarta.ws.rs.WebApplicationException; -import javafx.fxml.FXML; import javafx.scene.control.Alert; import javafx.scene.control.TextField; import javafx.scene.input.KeyEvent; @@ -27,29 +26,22 @@ import javafx.stage.Modality; import java.io.IOException; -public class AddQuoteCtrl { +public class AddNameCtrl { private final ServerUtils server; private final MainCtrl mainCtrl; public TextField recipeName; - @FXML - private TextField firstName; - @FXML - private TextField lastName; - - @FXML - private TextField quote; @Inject - public AddQuoteCtrl(ServerUtils server, MainCtrl mainCtrl) { + public AddNameCtrl(ServerUtils server, MainCtrl mainCtrl) { this.mainCtrl = mainCtrl; this.server = server; } - public void cancel() { + public void cancel() throws IOException, InterruptedException { clearFields(); - mainCtrl.showOverview(); + mainCtrl.showFoodpal(); } public void ok() throws IOException, InterruptedException { diff --git a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java index 95247a7..5141305 100644 --- a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java +++ b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java @@ -107,6 +107,10 @@ public class FoodpalApplicationCtrl { // till the all the code from everyone is implemented for now to not have errors private void showRecipeDetails(Recipe newRecipe) { + recipeNameLabel.setText(newRecipe.getName()); + ingredientsListView.getItems().setAll(newRecipe.getIngredients()); + preparationListView.getItems().setAll(newRecipe.getPreparationSteps()); + } // Button handlers @@ -124,23 +128,25 @@ public class FoodpalApplicationCtrl { } + /** + * Adds a recipe, by going to a different scene, there you insert the title and bam recipe created + */ @FXML - private void addRecipe(ActionEvent e) { - + private void addRecipe() { // Navigate to "create recipe" screen (should be like a pop-up or new screen or just another place in the app) - mainCtrl.showAdd(); + mainCtrl.showAddName(); } @FXML - private void removeSelectedRecipe() { + private void removeSelectedRecipe() throws IOException, InterruptedException { Recipe selected = recipeList.getSelectionModel().getSelectedItem(); if (selected == null) { return; } - // TODO: someone else todo + server.deleteRecipe(selected.getId()); + recipeList.getItems().remove(selected); - showRecipeDetails(null); } @FXML @@ -150,7 +156,7 @@ public class FoodpalApplicationCtrl { return; } // Let MainCtrl open the full detail screen - mainCtrl.showAdd(); //I had showrecipedetail but intelij says showoverview + mainCtrl.showAddName(); //I had showrecipedetail but intelij says showoverview } @FXML @@ -160,10 +166,15 @@ public class FoodpalApplicationCtrl { openSelectedRecipe(); } + /** + * 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 + * different recipe and come back after + */ @FXML private void addIngredient() { - // TODO: make it possible to add ingredient to current recipe System.out.println("Add ingredient clicked"); + mainCtrl.showAddIngredient(); } @FXML @@ -193,6 +204,10 @@ public class FoodpalApplicationCtrl { private void MakePrintable() { System.out.println("Recipe printed"); } + + 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 a368e16..a1345e6 100644 --- a/client/src/main/java/client/scenes/MainCtrl.java +++ b/client/src/main/java/client/scenes/MainCtrl.java @@ -15,6 +15,7 @@ */ package client.scenes; +import commons.Recipe; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; @@ -29,28 +30,37 @@ public class MainCtrl { private QuoteOverviewCtrl overviewCtrl; private Scene overview; - private AddQuoteCtrl addCtrl; - private Scene add; + private AddNameCtrl addNameCtrl; + private Scene addName; private FoodpalApplicationCtrl foodpalCtrl; private Scene foodpal; + private AddIngredientCtrl AddIngredientCtrl; + private Scene addIngredient; + public void initialize(Stage primaryStage, Pair overview, - Pair add, - Pair foodpal) throws IOException, InterruptedException { + Pair addName, + Pair foodpal, + Pair addIngredient + ) throws IOException, InterruptedException { this.primaryStage = primaryStage; this.overviewCtrl = overview.getKey(); this.overview = new Scene(overview.getValue()); - this.addCtrl = add.getKey(); - this.add = new Scene(add.getValue()); + this.addNameCtrl = addName.getKey(); + this.addName = new Scene(addName.getValue()); this.foodpalCtrl = foodpal.getKey(); this.foodpal = new Scene(foodpal.getValue()); + this.AddIngredientCtrl = addIngredient.getKey(); + this.addIngredient = new Scene(addIngredient.getValue()); + + showFoodpal(); primaryStage.show(); } @@ -61,12 +71,12 @@ public class MainCtrl { overviewCtrl.refresh(); } - public void showAdd() { + public void showAddName() { primaryStage.setTitle("Naming recipes"); - primaryStage.setScene(add); - add.setOnKeyPressed(e -> { + primaryStage.setScene(addName); + addName.setOnKeyPressed(e -> { try { - addCtrl.keyPressed(e); + addNameCtrl.keyPressed(e); } catch (IOException | InterruptedException ex) { throw new RuntimeException(ex); } @@ -78,4 +88,17 @@ public class MainCtrl { primaryStage.setScene(foodpal); foodpalCtrl.refresh(); } + + public void showAddIngredient() { + primaryStage.setTitle("adding ingredients"); + primaryStage.setScene(addIngredient); + addName.setOnKeyPressed(e -> { + try { + AddIngredientCtrl.keyPressed(e); + } catch (IOException | InterruptedException ex) { + throw new RuntimeException(ex); + } + }); + + } } \ No newline at end of file diff --git a/client/src/main/java/client/scenes/QuoteOverviewCtrl.java b/client/src/main/java/client/scenes/QuoteOverviewCtrl.java index 11b64c6..47e241b 100644 --- a/client/src/main/java/client/scenes/QuoteOverviewCtrl.java +++ b/client/src/main/java/client/scenes/QuoteOverviewCtrl.java @@ -60,7 +60,7 @@ public class QuoteOverviewCtrl implements Initializable { } public void addQuote() { - mainCtrl.showAdd(); + mainCtrl.showAddName(); } public void refresh() { diff --git a/client/src/main/java/client/utils/ServerUtils.java b/client/src/main/java/client/utils/ServerUtils.java index f7df0b4..2a65168 100644 --- a/client/src/main/java/client/utils/ServerUtils.java +++ b/client/src/main/java/client/utils/ServerUtils.java @@ -16,6 +16,7 @@ import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.util.ArrayList; import java.util.List; import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; @@ -165,4 +166,12 @@ public class ServerUtils { return addRecipe(newRecipe); } + + public Recipe addRecipeIngredient(Recipe recipe, String ingredient) throws IOException, InterruptedException { + List ingredients = new ArrayList<>(recipe.getIngredients()); + ingredients.add(ingredient); + recipe.setIngredients(ingredients); + + return addRecipe(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 new file mode 100644 index 0000000..44b677f --- /dev/null +++ b/client/src/main/resources/client/scenes/AddIngredient.fxml @@ -0,0 +1,15 @@ + + + + + + + + + +