diff --git a/src/main/java/mi/hdm/controllers/components/NutritionTableController.java b/src/main/java/mi/hdm/controllers/components/NutritionTableController.java new file mode 100644 index 0000000000000000000000000000000000000000..0c4c55751dcd92092b24bbb52e108dd7e30747d6 --- /dev/null +++ b/src/main/java/mi/hdm/controllers/components/NutritionTableController.java @@ -0,0 +1,92 @@ +package mi.hdm.controllers.components; + +import javafx.fxml.FXML; +import javafx.scene.control.ProgressBar; +import javafx.scene.paint.Color; +import javafx.scene.text.Text; +import mi.hdm.recipes.Nutrition; +import mi.hdm.recipes.NutritionTable; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.math.BigDecimal; +import java.util.Map; + +public class NutritionTableController { + private final static Logger log = LogManager.getLogger(NutritionTableController.class); + + private final NutritionTable nutritionTable; + private final String text; + + @FXML + private ProgressBar caloriesProgressBar; + @FXML + private ProgressBar carbsProgressBar; + @FXML + private ProgressBar fatProgressbar; + @FXML + private ProgressBar proteinProgressBar; + @FXML + private ProgressBar fibersProgressBar; + @FXML + private ProgressBar saltProgressBar; + @FXML + private Text caloriesPercentage; + @FXML + private Text carbsPercentage; + @FXML + private Text fatPercentage; + @FXML + private Text proteinPercentage; + @FXML + private Text fiberPercentage; + @FXML + private Text saltPercentage; + @FXML + private Text description; + + public NutritionTableController(NutritionTable nutritionTable, String text) { + this.nutritionTable = nutritionTable; + this.text = text; + } + + @FXML + public void initialize() { + description.setText(text); + drawNutritionTable(); + } + + private void drawNutritionTable() { + log.debug("Drawing nutrition table for meal plan."); + final Map<Nutrition, BigDecimal> weeklyNutrition = nutritionTable.getTable(); + //calculate percentage of weekly recommended dosage + double cals = weeklyNutrition.get(Nutrition.CALORIES).doubleValue() / (NutritionTable.CALORIES_DAILY * 7); + double carbs = weeklyNutrition.get(Nutrition.CARBS).doubleValue() / (NutritionTable.CARBS_DAILY * 7); + double fat = weeklyNutrition.get(Nutrition.FAT).doubleValue() / (NutritionTable.FAT_DAILY * 7); + double protein = weeklyNutrition.get(Nutrition.PROTEINS).doubleValue() / (NutritionTable.DAILY_PROTEIN * 7); + double fiber = weeklyNutrition.get(Nutrition.FIBERS).doubleValue() / (NutritionTable.DAILY_FIBER * 7); + double salt = weeklyNutrition.get(Nutrition.SALT).doubleValue() / (NutritionTable.DAILY_SALT * 7); + //update gui + setPercentage(caloriesProgressBar, caloriesPercentage, cals); + setPercentage(carbsProgressBar, carbsPercentage, carbs); + setPercentage(fatProgressbar, fatPercentage, fat); + setPercentage(proteinProgressBar, proteinPercentage, protein); + setPercentage(fibersProgressBar, fiberPercentage, fiber); + setPercentage(saltProgressBar, saltPercentage, salt); + } + + private void setPercentage(ProgressBar progressBar, Text textObject, double value) { + log.debug("Setting percentage for {}", textObject.getId()); + progressBar.setProgress(value); + textObject.setText(Math.round(value * 100) + "%"); + //set text color for percentages + if (value > 0.9) { + if (value > 1) { + textObject.setFill(Color.valueOf("#D91C1C")); //red + } + textObject.setFill(Color.valueOf("#ff8a0d")); //orange + } else { + textObject.setFill(Color.valueOf("#000000")); //default: black + } + } +} diff --git a/src/main/resources/fxml/components/nutrition-table.fxml b/src/main/resources/fxml/components/nutrition-table.fxml new file mode 100644 index 0000000000000000000000000000000000000000..f1dcd2af1bbdf10bc1e0c9ec3d0f408c00f24ed4 --- /dev/null +++ b/src/main/resources/fxml/components/nutrition-table.fxml @@ -0,0 +1,140 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<?import javafx.geometry.*?> +<?import javafx.scene.control.*?> +<?import javafx.scene.layout.*?> +<?import javafx.scene.text.*?> +<VBox xmlns="http://javafx.com/javafx" + xmlns:fx="http://javafx.com/fxml" + fx:controller="mi.hdm.controllers.components.NutritionTableController" + prefHeight="250.0" prefWidth="500.0"> + <VBox> + <Text fill="#1e1e1e" strokeType="OUTSIDE" strokeWidth="0.0" text="Nutrition"> + <font> + <Font name="Arial Bold" size="25.0"/> + </font> + </Text> + <Text fill="#1e1e1e" strokeType="OUTSIDE" strokeWidth="0.0" + text="Your nutrition score..." fx:id="description"> + <font> + <Font name="Arial" size="17.0"/> + </font> + </Text> + <HBox alignment="CENTER_LEFT" prefHeight="57.0" prefWidth="872.0" spacing="5.0"> + <Text fill="#1e1e1e" strokeType="OUTSIDE" strokeWidth="0.0" text="Calories" + wrappingWidth="83.47006416320801"> + <font> + <Font name="Arial" size="17.0"/> + </font> + </Text> + <ProgressBar fx:id="caloriesProgressBar" prefHeight="24.0" prefWidth="631.0" progress="0.0" + style="-fx-background-color: #d9d9d9; -fx-background-radius: 5; -fx-control-inner-background: a; -fx-accent: #d91c1c;"> + <HBox.margin> + <Insets left="70.0"/> + </HBox.margin> + </ProgressBar> + <Text fx:id="caloriesPercentage" fill="#1e1e1e" strokeType="OUTSIDE" strokeWidth="0.0" text="0%" + wrappingWidth="83.47006416320801"> + <font> + <Font name="Arial" size="17.0"/> + </font> + </Text> + </HBox> + <HBox alignment="CENTER_LEFT" prefHeight="53.0" prefWidth="872.0" spacing="5.0"> + <Text fill="#1e1e1e" strokeType="OUTSIDE" strokeWidth="0.0" text="Carbs" wrappingWidth="83.47006416320801"> + <font> + <Font name="Arial" size="17.0"/> + </font> + </Text> + <ProgressBar fx:id="carbsProgressBar" prefHeight="24.0" prefWidth="631.0" progress="0.0" + style="-fx-background-color: #d9d9d9; -fx-control-inner-background: a; -fx-background-radius: 5; -fx-accent: #d91c1c;"> + <HBox.margin> + <Insets left="70.0"/> + </HBox.margin> + </ProgressBar> + <Text fx:id="carbsPercentage" fill="#1e1e1e" strokeType="OUTSIDE" strokeWidth="0.0" text="0%" + wrappingWidth="83.47006416320801"> + <font> + <Font name="Arial" size="17.0"/> + </font> + </Text> + </HBox> + <HBox alignment="CENTER_LEFT" prefHeight="57.0" prefWidth="872.0" spacing="5.0"> + <Text fill="#1e1e1e" strokeType="OUTSIDE" strokeWidth="0.0" text="Fat" wrappingWidth="83.47006416320801"> + <font> + <Font name="Arial" size="17.0"/> + </font> + </Text> + <ProgressBar fx:id="fatProgressbar" prefHeight="24.0" prefWidth="631.0" progress="0.0" + style="-fx-background-color: #d9d9d9; -fx-background-radius: 5; -fx-control-inner-background: a; -fx-accent: #d91c1c;"> + <HBox.margin> + <Insets left="70.0"/> + </HBox.margin> + </ProgressBar> + <Text fx:id="fatPercentage" fill="#1e1e1e" strokeType="OUTSIDE" strokeWidth="0.0" text="0%" + wrappingWidth="83.47006416320801"> + <font> + <Font name="Arial" size="17.0"/> + </font> + </Text> + </HBox> + <HBox alignment="CENTER_LEFT" prefHeight="53.0" prefWidth="872.0" spacing="5.0"> + <Text fill="#1e1e1e" strokeType="OUTSIDE" strokeWidth="0.0" text="Protein" + wrappingWidth="83.47006416320801"> + <font> + <Font name="Arial" size="17.0"/> + </font> + </Text> + <ProgressBar fx:id="proteinProgressBar" prefHeight="24.0" prefWidth="631.0" progress="0.0" + style="-fx-background-color: #d9d9d9; -fx-control-inner-background: a; -fx-background-radius: 5; -fx-accent: #d91c1c;"> + <HBox.margin> + <Insets left="70.0"/> + </HBox.margin> + </ProgressBar> + <Text fx:id="proteinPercentage" fill="#1e1e1e" strokeType="OUTSIDE" strokeWidth="0.0" text="0%" + wrappingWidth="83.47006416320801"> + <font> + <Font name="Arial" size="17.0"/> + </font> + </Text> + </HBox> + <HBox alignment="CENTER_LEFT" prefHeight="53.0" prefWidth="872.0" spacing="5.0"> + <Text fill="#1e1e1e" strokeType="OUTSIDE" strokeWidth="0.0" text="Fibers" wrappingWidth="83.47006416320801"> + <font> + <Font name="Arial" size="17.0"/> + </font> + </Text> + <ProgressBar fx:id="fibersProgressBar" prefHeight="24.0" prefWidth="631.0" progress="0.0" + style="-fx-background-color: #d9d9d9; -fx-control-inner-background: a; -fx-background-radius: 5; -fx-accent: #d91c1c;"> + <HBox.margin> + <Insets left="70.0"/> + </HBox.margin> + </ProgressBar> + <Text fx:id="fiberPercentage" fill="#1e1e1e" strokeType="OUTSIDE" strokeWidth="0.0" text="0%" + wrappingWidth="83.47006416320801"> + <font> + <Font name="Arial" size="17.0"/> + </font> + </Text> + </HBox> + <HBox alignment="CENTER_LEFT" prefHeight="53.0" prefWidth="872.0" spacing="5.0"> + <Text fill="#1e1e1e" strokeType="OUTSIDE" strokeWidth="0.0" text="Salt" wrappingWidth="83.47006416320801"> + <font> + <Font name="Arial" size="17.0"/> + </font> + </Text> + <ProgressBar fx:id="saltProgressBar" prefHeight="24.0" prefWidth="631.0" progress="0.0" + style="-fx-background-color:#d9d9d9; -fx-control-inner-background: a; -fx-background-radius: 5; -fx-accent: #d91c1c;"> + <HBox.margin> + <Insets left="70.0"/> + </HBox.margin> + </ProgressBar> + <Text fx:id="saltPercentage" fill="#1e1e1e" strokeType="OUTSIDE" strokeWidth="0.0" text="0%" + wrappingWidth="83.47006416320801"> + <font> + <Font name="Arial" size="17.0"/> + </font> + </Text> + </HBox> + </VBox> +</VBox>