Skip to content
Snippets Groups Projects
Commit 0537ebf3 authored by Karsch Lukas's avatar Karsch Lukas
Browse files

finalized meal plan

parent f39db1b6
No related branches found
No related tags found
No related merge requests found
...@@ -2,12 +2,13 @@ package mi.hdm.controllers; ...@@ -2,12 +2,13 @@ package mi.hdm.controllers;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.geometry.HPos; import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos; import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.layout.*; import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font; import javafx.scene.text.Font;
import javafx.scene.text.FontWeight; import javafx.scene.text.FontWeight;
import javafx.scene.text.Text; import javafx.scene.text.Text;
...@@ -27,7 +28,6 @@ import java.util.Map; ...@@ -27,7 +28,6 @@ import java.util.Map;
public class MealPlanController extends BaseController { public class MealPlanController extends BaseController {
private final MealPlan mealPlan; private final MealPlan mealPlan;
private final RecipeManager recipeManager; private final RecipeManager recipeManager;
//private final IngredientManager ingredientManager;
private final RecipeSearch recipeSearch; private final RecipeSearch recipeSearch;
private List<Recipe> searchResults; private List<Recipe> searchResults;
...@@ -71,7 +71,6 @@ public class MealPlanController extends BaseController { ...@@ -71,7 +71,6 @@ public class MealPlanController extends BaseController {
public MealPlanController(MealPlan mealPlan, RecipeManager recipeManager, IngredientManager ingredientManager) { public MealPlanController(MealPlan mealPlan, RecipeManager recipeManager, IngredientManager ingredientManager) {
this.mealPlan = mealPlan; this.mealPlan = mealPlan;
this.recipeManager = recipeManager; this.recipeManager = recipeManager;
//this.ingredientManager = ingredientManager;
recipeSearch = new RecipeSearch(recipeManager, ingredientManager); recipeSearch = new RecipeSearch(recipeManager, ingredientManager);
searchResults = new ArrayList<>(); searchResults = new ArrayList<>();
} }
...@@ -79,8 +78,7 @@ public class MealPlanController extends BaseController { ...@@ -79,8 +78,7 @@ public class MealPlanController extends BaseController {
@FXML @FXML
public void initialize() { public void initialize() {
loadHeader(parent); loadHeader(parent);
displayMealPlan(); render();
drawNutritionTable();
} }
private void displayMealPlan() { private void displayMealPlan() {
...@@ -110,20 +108,23 @@ public class MealPlanController extends BaseController { ...@@ -110,20 +108,23 @@ public class MealPlanController extends BaseController {
Label name = new Label(r.getName()); Label name = new Label(r.getName());
name.setFont(font); name.setFont(font);
name.setStyle("-fx-padding: 0 50 10 10;"); name.setStyle("-fx-padding: 0 50 10 10;");
name.setCursor(Cursor.HAND);
name.setTooltip(new Tooltip("Click to display this recipe"));
name.setOnMouseClicked(e -> name.setOnMouseClicked(e ->
changeScene(View.RECIPE_VIEW, r) changeScene(View.RECIPE_VIEW, r)
); );
Button removeRecipeButton = new Button(); Button removeRecipeButton = new Button();
removeRecipeButton.setStyle("-fx-background-color: dedede;" + removeRecipeButton.setStyle("-fx-background-color: dedede;" +
"-fx-background-radius: 5;"); "-fx-background-radius: 5;");
ImageView imageView = new ImageView(String.valueOf(Recipe.class.getResource("/images/Tasty_Pages_X_Icon.png"))); ImageView xIconImage = new ImageView(String.valueOf(Recipe.class.getResource("/images/Tasty_Pages_X_Icon.png")));
imageView.setPreserveRatio(true); xIconImage.setPreserveRatio(true);
imageView.setFitHeight(20); xIconImage.setFitHeight(20);
imageView.setFitWidth(20); xIconImage.setFitWidth(20);
removeRecipeButton.setGraphic(imageView); removeRecipeButton.setGraphic(xIconImage);
removeRecipeButton.setOnAction(e -> { removeRecipeButton.setOnAction(e -> {
mealPlan.clear(LocalDate.now().plusDays(J)); mealPlan.clear(LocalDate.now().plusDays(J));
render();
}); });
recipeHBox.getChildren().addAll(name, removeRecipeButton); recipeHBox.getChildren().addAll(name, removeRecipeButton);
...@@ -134,7 +135,7 @@ public class MealPlanController extends BaseController { ...@@ -134,7 +135,7 @@ public class MealPlanController extends BaseController {
recipeImage.fitHeightProperty().bind(mealPlanGrid.heightProperty().subtract(60)); recipeImage.fitHeightProperty().bind(mealPlanGrid.heightProperty().subtract(60));
}, () -> { }, () -> {
Button addRecipeButton = new Button(); Button addRecipeButton = new Button();
addRecipeButton.setStyle("-fx-background-color: dedede;" + addRecipeButton.setStyle("-fx-background-color: #dedede;" +
"-fx-background-radius: 10;"); "-fx-background-radius: 10;");
ImageView imageView = new ImageView(String.valueOf(Recipe.class.getResource("/images/Tasty_Pages_Plus_Icon.png"))); ImageView imageView = new ImageView(String.valueOf(Recipe.class.getResource("/images/Tasty_Pages_Plus_Icon.png")));
imageView.setPreserveRatio(true); imageView.setPreserveRatio(true);
...@@ -147,8 +148,6 @@ public class MealPlanController extends BaseController { ...@@ -147,8 +148,6 @@ public class MealPlanController extends BaseController {
); );
GridPane.setHalignment(addRecipeButton, HPos.CENTER); GridPane.setHalignment(addRecipeButton, HPos.CENTER);
}); });
} }
} }
...@@ -185,8 +184,7 @@ public class MealPlanController extends BaseController { ...@@ -185,8 +184,7 @@ public class MealPlanController extends BaseController {
mealPlan.addRecipeToMealPlan(result, date); mealPlan.addRecipeToMealPlan(result, date);
stackPane.getChildren().remove(searchVBox); stackPane.getChildren().remove(searchVBox);
displayMealPlan(); render();
drawNutritionTable();
}); });
resultContainer.getChildren().add(resultLabel); resultContainer.getChildren().add(resultLabel);
} }
...@@ -195,7 +193,7 @@ public class MealPlanController extends BaseController { ...@@ -195,7 +193,7 @@ public class MealPlanController extends BaseController {
} }
@FXML @FXML
public void clearMealplan() { public void clearMealPlan() {
mealPlan.clear(); mealPlan.clear();
displayMealPlan(); displayMealPlan();
} }
...@@ -223,14 +221,22 @@ public class MealPlanController extends BaseController { ...@@ -223,14 +221,22 @@ public class MealPlanController extends BaseController {
log.debug("Setting percentage for {}", textObject.getId()); log.debug("Setting percentage for {}", textObject.getId());
progressBar.setProgress(value); progressBar.setProgress(value);
textObject.setText(Math.round(value * 100) + "%"); textObject.setText(Math.round(value * 100) + "%");
//TODO: set color based on value (below code doesnt work somehow) //set text color for percentages
if (value > 0.9) { if (value > 0.9) {
System.out.println("value larger than .9");
if (value > 1) { if (value > 1) {
System.out.println("value larger than 1"); textObject.setFill(Color.valueOf("#D91C1C")); //red
textObject.setStyle("-fx-text-fill: #D91C1C;"); }
} else textObject.setFill(Color.valueOf("#ff8a0d")); //orange
textObject.setStyle("-fx-text-fill: #ff8a0d;"); } else {
textObject.setFill(Color.valueOf("#000000")); //default: black
} }
} }
/**
* Draws the meal plan and the nutrition table
*/
private void render() {
displayMealPlan();
drawNutritionTable();
}
} }
...@@ -10,6 +10,7 @@ import mi.hdm.components.CategoryPreviewLabel; ...@@ -10,6 +10,7 @@ import mi.hdm.components.CategoryPreviewLabel;
import mi.hdm.components.IngredientSearchResultLabel; import mi.hdm.components.IngredientSearchResultLabel;
import mi.hdm.components.SelectedIngredientLabel; import mi.hdm.components.SelectedIngredientLabel;
import mi.hdm.exceptions.InvalidRecipeException; import mi.hdm.exceptions.InvalidRecipeException;
import mi.hdm.helpers.Validation;
import mi.hdm.recipes.*; import mi.hdm.recipes.*;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
...@@ -75,13 +76,13 @@ public class RecipeEditorController extends BaseController { ...@@ -75,13 +76,13 @@ public class RecipeEditorController extends BaseController {
searchResults = new ArrayList<>(); searchResults = new ArrayList<>();
selectedIngredients = new ArrayList<>(); selectedIngredients = new ArrayList<>();
ingredientManager.getIngredientsFromKeys(recipe.getIngredients()).forEach((i, amount) -> { recipe.getIngredients()
if (i instanceof Ingredient) { .forEach((code, amount) -> {
selectedIngredients.add(i); ingredientManager.getIngredient(code).ifPresentOrElse(
} else { selectedIngredients::add,
log.error("No ingredient with code {}", i.getUniqueCode()); () -> log.error("No ingredient with code '{}'", code)
} );
}); });
recipeSearch = new RecipeSearch(recipeManager, ingredientManager); recipeSearch = new RecipeSearch(recipeManager, ingredientManager);
selectedIngredientsVbox = new VBox(); selectedIngredientsVbox = new VBox();
selectedIngredientLabels = new ArrayList<>(); selectedIngredientLabels = new ArrayList<>();
...@@ -208,11 +209,10 @@ public class RecipeEditorController extends BaseController { ...@@ -208,11 +209,10 @@ public class RecipeEditorController extends BaseController {
ingredients.put(label.getComponent(), label.getAmount()); ingredients.put(label.getComponent(), label.getAmount());
}); });
try { try {
recipe.setIngredientFromRecipeComponents(ingredients); recipe.setIngredientFromRecipeComponents(ingredients);
recipe.setDescription(descriptionTextArea.getText()); recipe.setDescription(descriptionTextArea.getText());
recipe.setPreparation(List.of(preparationTextArea.getText().split("\n"))); recipe.setPreparation(Validation.listNoEmptyLines(preparationTextArea.getText()));
recipe.setCategoriesFromObjects(List.copyOf(selectedCategories)); recipe.setCategoriesFromObjects(List.copyOf(selectedCategories));
String prepTime = prepTimeTextField.getText(); String prepTime = prepTimeTextField.getText();
recipe.setPreparationTimeMins( isInteger(prepTime) ? Integer.parseInt(prepTime) : null); recipe.setPreparationTimeMins( isInteger(prepTime) ? Integer.parseInt(prepTime) : null);
...@@ -239,7 +239,7 @@ public class RecipeEditorController extends BaseController { ...@@ -239,7 +239,7 @@ public class RecipeEditorController extends BaseController {
@FXML @FXML
public void incrementPageCounter() { public void incrementPageCounter() {
//increment the current page and re-render //increment the current page of search results and re-render
currentPage = Math.min(++currentPage, maxPages); currentPage = Math.min(++currentPage, maxPages);
log.debug("User selected page {} / {}", currentPage, maxPages); log.debug("User selected page {} / {}", currentPage, maxPages);
drawIngredientSearchResults(); drawIngredientSearchResults();
...@@ -247,7 +247,7 @@ public class RecipeEditorController extends BaseController { ...@@ -247,7 +247,7 @@ public class RecipeEditorController extends BaseController {
@FXML @FXML
public void decrementPageCounter() { public void decrementPageCounter() {
//decrement the current page and re-render //decrement the current page of search results and re-render
currentPage = Math.max(0, --currentPage); currentPage = Math.max(0, --currentPage);
log.debug("User selected page {} / {}", currentPage, maxPages); log.debug("User selected page {} / {}", currentPage, maxPages);
drawIngredientSearchResults(); drawIngredientSearchResults();
......
package mi.hdm.helpers; package mi.hdm.helpers;
import java.util.List;
import java.util.stream.Stream;
public class Validation { public class Validation {
/** /**
* Validate Strings before passing them to methods like Integer.parseInt() * Validate Strings before passing them to methods like Integer.parseInt()
...@@ -34,4 +37,11 @@ public class Validation { ...@@ -34,4 +37,11 @@ public class Validation {
} }
return true; return true;
} }
/**
* @return a list from the lines of the provided string with no empty lines
*/
public static List<String> listNoEmptyLines(String input) {
return Stream.of(input.split("\n")).filter(s -> !s.isBlank()).toList();
}
} }
This diff is collapsed.
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