diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java index 259b48055e64a2fc33929e02132b43a73e321a33..d97a0f5e9108202d06fb495136247fb279e6b1e0 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeController.java @@ -1,104 +1,98 @@ package no.ntnu.idatt1002.demo.controller; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.geometry.Insets; +import javafx.scene.Node; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.GridPane; +import javafx.scene.layout.Pane; +import javafx.scene.layout.VBox; +import javafx.scene.text.Text; +import javafx.stage.Stage; import no.ntnu.idatt1002.demo.data.recipes.*; import java.io.IOException; import java.net.URL; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.ResourceBundle; public class RecipeController implements Initializable { + @FXML + private Label recipeName; @FXML - private GridPane recipeTiles; + private Text instructions; + @FXML + private Button goBackBtn; - private List<Recipe> recipes; + @FXML + private VBox ingredientPane; - // For testing purposes at frontend: + @FXML + private ObservableList<RecipeIngredient> ingredients; - private List<Recipe> getSuggestions() { - RecipeRegister recipeRegister = new RecipeRegister(); - List<Recipe> suggestedRecipes = null; - IngredientsAtHand ingredientsAtHand = new IngredientsAtHand(); - ingredientsAtHand.addIngredient(FoodItem.LEMON); - ingredientsAtHand.addIngredient(FoodItem.MINCED_MEAT); - ingredientsAtHand.addIngredient(FoodItem.POTATO); - ingredientsAtHand.addIngredient(FoodItem.MILK); + public void setData(Recipe recipe) { + recipeName.setText(recipe.getName()); + instructions.setText(recipe.getInstructions()); - Recipe recipe1 = new Recipe("Recipe no. 1", "Description"); - recipe1.addIngredient(FoodItem.LEMON, 5, MeasuringUnit.PC); - recipe1.addIngredient(FoodItem.POTATO, 5, MeasuringUnit.PC); + ingredients = FXCollections.observableArrayList(recipe.getIngredientList()); - Recipe recipe2 = new Recipe("Recipe no. 2", "Description" ); - recipe2.addIngredient(FoodItem.POTATO, 5, MeasuringUnit.PC); - recipe2.addIngredient(FoodItem.TOMATO, 6, MeasuringUnit.PC); - recipe2.addIngredient(FoodItem.ORANGE, 1, MeasuringUnit.PC); + setIngredientTiles(); - recipeRegister.addRecipe(recipe1); - recipeRegister.addRecipe(recipe2); + } - recipeRegister.getRecipes().forEach((recipe) -> recipe.updateIngredientStatus(ingredientsAtHand)); + private void setIngredientTiles() { - recipeRegister.getRecipes().sort(Comparator.comparing(Recipe::getMissingIngredients)); + for(RecipeIngredient ri : ingredients) { + FXMLLoader loader = new FXMLLoader(); + loader.setLocation(getClass().getResource("/view/IngredientTile.fxml")); - List<Recipe> sortedRecipes = recipeRegister.getRecipes(); + try { + Pane pane = loader.load(); + IngredientTileController ingredientTileController = loader.getController(); + ingredientTileController.setData(ri); - for(int i =0; i<4; i++) { - if(sortedRecipes.get(i) != null) { - suggestedRecipes.add(sortedRecipes.get(i)); - } else { - suggestedRecipes.add(null); + ingredientPane.getChildren().add(pane); + } catch (IOException e) { + throw new RuntimeException(e); } - } + } - return suggestedRecipes; + @FXML + private void goBack(ActionEvent event) throws IOException { + FXMLLoader loader = new FXMLLoader(); + loader.setLocation(getClass().getResource("/view/SuggestRecipes.fxml")); + + Parent root = loader.load(); + Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); + Scene scene = new Scene(root); + stage.setScene(scene); + stage.show(); } - @Override public void initialize(URL url, ResourceBundle resourceBundle) { - recipes.addAll(getSuggestions()); - int column = 0; - int row = 0; - - try { - for(int i = 0; i<4; i++) { - FXMLLoader fxmlLoader = new FXMLLoader(); - fxmlLoader.setLocation(getClass().getResource("/src/main/resources/view/RecipeTile.fxml")); - - AnchorPane anchorPane = fxmlLoader.load(); - - RecipeTileController recipeTileController = fxmlLoader.getController(); - - recipeTileController.setData(recipes.get(i)); - - if(column == 3){ - column = 0; - row++; - } - recipeTiles.add(anchorPane, column++, row); - GridPane.setMargin(anchorPane, new Insets(10)); - } - - } catch (IOException e) { - e.printStackTrace(); - } } } diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java index 1248f75a8a08f376ef7672e40f58830c0dd076cc..73025dcbfc8018073b864a651024aa7c5e9ba63c 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java @@ -8,8 +8,11 @@ import javafx.scene.Node; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Label; +import javafx.scene.layout.VBox; import javafx.stage.Stage; +import no.ntnu.idatt1002.demo.data.recipes.FileHandler; import no.ntnu.idatt1002.demo.data.recipes.Recipe; +import no.ntnu.idatt1002.demo.data.recipes.RecipeRegister; import java.io.IOException; import java.net.URL; @@ -23,18 +26,25 @@ public class RecipeTileController implements Initializable { @FXML private Label missingTag; + private RecipeRegister recipeRegister; + @FXML private void tileClick(ActionEvent event) throws IOException { FXMLLoader loader = new FXMLLoader(); loader.setLocation(getClass().getResource("/view/Recipe.fxml")); - // nameTag of action tile --> register get recipe(string name) --> Pass on to Controller of Recipe + String recipeName = this.nameTag.getText(); + Recipe recipeOfInterest = recipeRegister.getRecipe(recipeName); + + RecipeController recipeController = loader.getController(); + recipeController.setData(recipeOfInterest); Parent root = loader.load(); Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); + } @@ -46,7 +56,7 @@ public class RecipeTileController implements Initializable { @Override public void initialize(URL url, ResourceBundle resourceBundle) { - + recipeRegister = FileHandler.readRecipeRegister("Recipes"); nameTag.setWrapText(true); } } diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java index c56853cc65732e464fe43ff23c3feed8477e0450..152bce0f764d799384703d08c548a77688cfd8f0 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java @@ -87,6 +87,7 @@ public class SuggestRecipesController implements Initializable { System.out.println("Remove the selected from fridge!"); String toRemove = fridgeList.getSelectionModel().getSelectedItem(); + //TODO: If anything selected! ingredientsAtHand.removeIngredient(FoodItem.valueOf(toRemove.replace(" ", "_").toUpperCase())); FileHandler.writeIngredientsAtHand(ingredientsAtHand, "Fridge"); //TODO: Remove toUppercase solution above. diff --git a/src/main/resources/view/Recipe.fxml b/src/main/resources/view/Recipe.fxml index d45c6e867306ba6e3596fa6c4ae3e12410876f94..a8fbd3bd0014e8ea197b782d0010818d35a8b5e1 100644 --- a/src/main/resources/view/Recipe.fxml +++ b/src/main/resources/view/Recipe.fxml @@ -17,30 +17,38 @@ <children> <Pane prefHeight="200.0" prefWidth="200.0"> <children> - <Button layoutX="76.0" layoutY="26.0" mnemonicParsing="false" text="Button"> + <Button fx:id="goBackBtn" layoutX="76.0" layoutY="30.0" mnemonicParsing="false" onAction="#goBack" text="Go Back"> <font> <Font size="14.0" /> </font></Button> </children> </Pane> - <Label fx:id="recipeName" text="RecipeName" textAlignment="CENTER"> - <font> - <Font size="36.0" /> - </font> - </Label> + <Pane prefHeight="103.0" prefWidth="853.0"> + <children> + <Label fx:id="recipeName" alignment="CENTER" contentDisplay="CENTER" layoutX="198.0" layoutY="46.0" text="RecipeName" textAlignment="CENTER"> + <font> + <Font size="36.0" /> + </font> + </Label> + </children> + </Pane> </children> </HBox> </top> <center> <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER"> <children> - <VBox prefHeight="457.0" prefWidth="265.0" /> - <TextFlow prefHeight="457.0" prefWidth="625.0" /> + <VBox fx:id="ingredientPane" prefHeight="457.0" prefWidth="265.0" /> + <Pane prefHeight="457.0" prefWidth="582.0"> + <children> + <Text fx:id="instrucctions" layoutX="14.0" layoutY="47.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Instructions" wrappingWidth="531.7294921875" /> + </children> + </Pane> </children> </HBox> </center> <left> - <Pane prefHeight="493.0" prefWidth="148.0" BorderPane.alignment="CENTER" /> + <Pane fx:id="instructions" prefHeight="493.0" prefWidth="148.0" BorderPane.alignment="CENTER" /> </left> <bottom> <Pane prefHeight="102.0" prefWidth="1130.0" BorderPane.alignment="CENTER" />