Skip to content
Snippets Groups Projects
Commit ce9d67f4 authored by HSoreide's avatar HSoreide
Browse files

Suggested recipes are shown and update with changes ti ingredients at hand

parent 2f3ec095
No related branches found
No related tags found
2 merge requests!42Hs frontend recipes,!41Hs frontend recipes
Showing
with 176 additions and 92 deletions
......@@ -88,7 +88,7 @@ public class AddIngredientController implements Initializable {
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
ingredientsAtHand = FileHandler.readIngredientsAtHand("AtHandRegister");
ingredientsAtHand = FileHandler.readIngredientsAtHand("Fridge");
/* ObservableList ingredients = FXCollections.observableArrayList(testIngredients);*/
/* ObservableList<FoodItem> ingredients = FXCollections.observableArrayList(
......
package no.ntnu.idatt1002.demo.controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import no.ntnu.idatt1002.demo.data.recipes.Recipe;
public class RecipeTileController {
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class RecipeTileController implements Initializable {
@FXML
private Label nameTag;
......@@ -12,12 +23,30 @@ public class RecipeTileController {
@FXML
private Label missingTag;
private Recipe recipe;
@FXML
private void tileClick(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/Recipe.fxml"));
public void setData(Recipe recipe) {
// nameTag of action tile --> register get recipe(string name) --> Pass on to Controller of Recipe
Parent root = loader.load();
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
this.recipe = recipe;
public void setData(Recipe recipe) {
nameTag.setText(recipe.getName());
missingTag.setText(Integer.toString(recipe.getMissingIngredients()));
System.out.println(missingTag.getText());
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
nameTag.setWrapText(true);
}
}
......@@ -10,11 +10,15 @@ import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
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.List;
import java.util.Optional;
import java.util.ResourceBundle;
......@@ -37,10 +41,16 @@ public class SuggestRecipesController implements Initializable {
@FXML
private ListView<String> fridgeList;
@FXML
private GridPane recipeTiles;
private ObservableList<String> fridge;
private ObservableList<Recipe> recipes;
private final int NUMBER_OF_TILES = 4;
@FXML
private void addIngredient(ActionEvent event) throws IOException {
......@@ -60,9 +70,11 @@ public class SuggestRecipesController implements Initializable {
if (clickedButton.get() == ButtonType.APPLY) {
// Refresh ingredientsAtHand.
ingredientsAtHand = FileHandler.readIngredientsAtHand("Fridge");
//TODO: Duplicate
//TODO: Duplicate code and assertion.
assert ingredientsAtHand != null;
fridge = FXCollections.observableArrayList(ingredientsAtHand.getIngredientsAtHand().stream().map(foodItem -> foodItem.label).toList());
fridgeList.setItems(fridge);
setRecipeTiles();
System.out.println("Clickecked OK!");
}else if(clickedButton.get() == ButtonType.CANCEL) {
......@@ -76,10 +88,12 @@ public class SuggestRecipesController implements Initializable {
System.out.println("Remove the selected from fridge!");
String toRemove = fridgeList.getSelectionModel().getSelectedItem();
ingredientsAtHand.removeIngredient(FoodItem.valueOf(toRemove.replace(" ", "_").toUpperCase()));
FileHandler.writeIngredientsAtHand(ingredientsAtHand, "Fridge");
//TODO: Remove toUppercase solution above.
//TODO: Consider factoring out to a update method.
fridge = FXCollections.observableArrayList(ingredientsAtHand.getIngredientsAtHand().stream().map(foodItem -> foodItem.label).toList());
fridgeList.setItems(fridge);
setRecipeTiles();
}
/* public void addToFridge(String ingredient) {
......@@ -106,10 +120,37 @@ public class SuggestRecipesController implements Initializable {
stage.show();
}
private void setRecipeTiles(int number) {
private void setRecipeTiles() {
// Ingredeints at hand and recipesRegister
Recipe[] recipes = recipeRegister.pickBestFits(4, ingredientsAtHand);
ArrayList<Recipe> recipes = recipeRegister.pickBestFits(NUMBER_OF_TILES, ingredientsAtHand);
int i =0;
int j = 0;
for(Recipe r : recipes) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/RecipeTile.fxml"));
if(i > 1) {
j++;
i=0;
}
try {
VBox vBox = loader.load();
RecipeTileController recipeTileController = loader.getController();
recipeTileController.setData(r);
recipeTiles.add(vBox, j, i);
} catch (IOException e) {
throw new RuntimeException(e);
}
i++;
}
// Include numbering?
}
......@@ -128,7 +169,7 @@ public class SuggestRecipesController implements Initializable {
recipes = FXCollections.observableArrayList(recipeRegister.getRecipes());
// Get the number from FX-grid available?
setRecipeTiles(4);
setRecipeTiles();
}
}
......@@ -4,7 +4,7 @@ public enum FoodItem {
ONION("onion"),
MINCED_MEAT("minced meat"),
POTATO("potatoes"),
POTATO("potato"),
YELLOW_CHEESE("yellow cheese"),
WHEAT_FLOUR("wheat flour"),
MILK("milk"),
......
......@@ -90,39 +90,20 @@ public class Recipe {
*/
public void updateIngredientStatus(IngredientsAtHand ingredientsAtHand) {
// Will need a supporting class for converting between units to be accurate.
if(ingredientsAtHand == null) {
throw new NullPointerException("The ingredients at hand object must exist");
} else if (ingredientsAtHand.getIngredientsAtHand().size() < 1) {
throw new IllegalArgumentException("The collection of ingredients at hand is empty.");
} else {
missingIngredients = 0;
int notMissing = (int) ingredientList.stream().filter((inRecipe) -> ingredientsAtHand.atHand(inRecipe.getFoodType())).count();
ingredientList.forEach((inRecipe) -> {
inRecipe.setAtHand(ingredientsAtHand.atHand(inRecipe.getFoodType()));
});
};
/* ingredientList.forEach((inRecipe) -> {
ingredientsAtHand.getIngredientsAtHand().forEach((atHand) -> {
System.out.println("----");
System.out.println(inRecipe.getFoodType());
System.out.println(atHand);
System.out.println("----");
if(inRecipe.getFoodType() == atHand) {
inRecipe.setAtHand(true);
} else {
inRecipe.setAtHand(false);
missingIngredients += 1;
}
});
});*/
}
if (ingredientsAtHand == null) {
throw new NullPointerException("The ingredients at hand object must exist");
} else if (ingredientsAtHand.getIngredientsAtHand().size() < 1) {
throw new IllegalArgumentException("The collection of ingredients at hand is empty.");
} else {
missingIngredients = 0;
int notMissing = (int) ingredientList.stream().filter((inRecipe) -> ingredientsAtHand.atHand(inRecipe.getFoodType())).count();
ingredientList.forEach((inRecipe) -> {
inRecipe.setAtHand(ingredientsAtHand.atHand(inRecipe.getFoodType()));
});
missingIngredients = ingredientList.size()-notMissing;
}
}
public int getMissingIngredients() {
......
......@@ -31,15 +31,15 @@ public class RecipeRegister {
.findFirst().orElse(null);
}
public Recipe[] pickBestFits(int number, IngredientsAtHand atHand) {
Recipe[] recipes;
public ArrayList<Recipe> pickBestFits(int number, IngredientsAtHand atHand) {
ArrayList<Recipe> recipes;
this.recipes.forEach(r -> r.updateIngredientStatus(atHand));
recipes = (Recipe[]) this.recipes.stream()
recipes = this.recipes.stream()
.sorted(Comparator.comparingInt(Recipe::getMissingIngredients))
.limit(number)
.toArray();
.limit(number).collect(Collectors.toCollection(ArrayList::new));
return recipes;
}
......
POTATO
MILK
LEMON
MINCED_MEAT
YELLOW_CHEESE
ORANGE
SALSA_SAUCE
ONION
POTATO
.recipe-tile {
-fx-background-color: rgb(215, 153, 27);
-fx-background-color: rgba(252, 203, 80, 0.8);
-fx-background-radius: 25;
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="view.Recipe"
prefHeight="400.0" prefWidth="600.0">
<AnchorPane prefHeight="695.0" prefWidth="1130.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.RecipeController">
<children>
<ImageView fitHeight="695.0" fitWidth="1130.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../Images/backgroundMini.jpg" />
</image></ImageView>
<BorderPane prefHeight="695.0" prefWidth="1130.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
<HBox prefHeight="136.0" prefWidth="1130.0" BorderPane.alignment="CENTER">
<children>
<Pane prefHeight="200.0" prefWidth="200.0">
<children>
<Button layoutX="76.0" layoutY="26.0" mnemonicParsing="false" text="Button">
<font>
<Font size="14.0" />
</font></Button>
</children>
</Pane>
<Label fx:id="recipeName" text="RecipeName" textAlignment="CENTER">
<font>
<Font size="36.0" />
</font>
</Label>
</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" />
</children>
</HBox>
</center>
<left>
<Pane prefHeight="493.0" prefWidth="148.0" BorderPane.alignment="CENTER" />
</left>
<bottom>
<Pane prefHeight="102.0" prefWidth="1130.0" BorderPane.alignment="CENTER" />
</bottom>
<right>
<Pane prefHeight="493.0" prefWidth="148.0" BorderPane.alignment="CENTER" />
</right></BorderPane>
</children>
</AnchorPane>
......@@ -5,39 +5,39 @@
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="recipe-tile" fx:id="recipeTile" prefHeight="300.0" prefWidth="400.0" styleClass="recipe-tile" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.RecipeTileController">
<VBox fx:id="recipeTile" onMouseClicked="#tileClick" prefHeight="220.0" prefWidth="280.0" styleClass="recipe-tile" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.RecipeTileController">
<children>
<VBox prefHeight="350.0" prefWidth="500.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<Pane>
<children>
<Pane prefHeight="262.0" prefWidth="500.0">
<children>
<Label id="recipeTitle" fx:id="nameTag" alignment="CENTER" contentDisplay="CENTER" layoutY="3.0" prefHeight="237.0" prefWidth="400.0" text="Name of Recipe">
<font>
<Font name="System Bold" size="36.0" />
</font>
</Label>
</children></Pane>
<HBox prefHeight="88.0" prefWidth="500.0">
<children>
<Label alignment="CENTER" prefHeight="63.0" prefWidth="292.0" text="Ingredients missing:">
<font>
<Font size="24.0" />
</font>
<opaqueInsets>
<Insets left="20.0" right="20.0" />
</opaqueInsets>
</Label>
<Label id="noMissingIngredients" fx:id="missingTag" alignment="CENTER" contentDisplay="CENTER" prefHeight="69.0" prefWidth="127.0" text="# missing">
<font>
<Font size="24.0" />
</font>
</Label>
</children>
<Label id="recipeTitle" fx:id="nameTag" alignment="CENTER" contentDisplay="CENTER" layoutY="3.0" prefHeight="190.0" prefWidth="280.0" text="Name of Recipe" textAlignment="CENTER">
<font>
<Font name="System Bold" size="24.0" />
</font>
<padding>
<Insets left="10.0" />
</padding>
</Label>
</children>
</Pane>
<HBox prefHeight="88.0" prefWidth="500.0">
<children>
<Label alignment="CENTER" prefHeight="63.0" prefWidth="292.0" text="Ingredients missing:">
<font>
<Font size="18.0" />
</font>
<opaqueInsets>
<Insets left="20.0" right="20.0" />
</opaqueInsets>
</HBox>
</Label>
<Label id="noMissingIngredients" fx:id="missingTag" alignment="CENTER" contentDisplay="CENTER" prefHeight="69.0" prefWidth="127.0" text="# missing">
<font>
<Font size="24.0" />
</font>
</Label>
</children>
</VBox>
<opaqueInsets>
<Insets left="20.0" right="20.0" />
</opaqueInsets>
</HBox>
</children>
</AnchorPane>
</VBox>
......@@ -78,7 +78,7 @@
</BorderPane>
</top>
<center>
<GridPane fx:id="recipeTiles" BorderPane.alignment="CENTER">
<GridPane fx:id="recipeTiles" hgap="20.0" prefWidth="603.0" vgap="20.0" BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment