From 3301c75354f6478607b3c0eed35b2332e077bed3 Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Tue, 16 Dec 2025 13:24:11 +0100 Subject: [PATCH] feat: init ui definition nutrition view --- .../client/scenes/FoodpalApplicationCtrl.java | 2 +- .../nutrition/NutritionDetailsCtrl.java | 38 +++++++++++ .../scenes/nutrition/NutritionViewCtrl.java | 68 +++++++++++++++++++ .../scenes/nutrition/NutritionDetails.fxml | 32 +++++++++ .../scenes/nutrition/NutritionView.fxml | 19 ++++++ 5 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 client/src/main/java/client/scenes/nutrition/NutritionDetailsCtrl.java create mode 100644 client/src/main/java/client/scenes/nutrition/NutritionViewCtrl.java create mode 100644 client/src/main/resources/client/scenes/nutrition/NutritionDetails.fxml create mode 100644 client/src/main/resources/client/scenes/nutrition/NutritionView.fxml diff --git a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java index ed97139..9248bb4 100644 --- a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java +++ b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java @@ -56,7 +56,7 @@ public class FoodpalApplicationCtrl implements LocaleAware { public Label recipesLabel; @FXML - private ListView recipeList; + public ListView recipeList; @FXML private Button addRecipeButton; diff --git a/client/src/main/java/client/scenes/nutrition/NutritionDetailsCtrl.java b/client/src/main/java/client/scenes/nutrition/NutritionDetailsCtrl.java new file mode 100644 index 0000000..9a60b4a --- /dev/null +++ b/client/src/main/java/client/scenes/nutrition/NutritionDetailsCtrl.java @@ -0,0 +1,38 @@ +package client.scenes.nutrition; + +import client.utils.LocaleAware; +import client.utils.LocaleManager; +import com.google.inject.Inject; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; + +public class NutritionDetailsCtrl implements LocaleAware { + private final LocaleManager manager; + + public Label ingredientName; + public Label fatInputLabel; + public Label proteinInputLabel; + public Label carbInputLabel; + public Label estimatedKcalLabel; + public Label usageLabel; + + public TextField fatInputElement; + public TextField proteinInputElement; + public TextField carbInputElement; + + @Inject + public NutritionDetailsCtrl( + LocaleManager manager + ) { + this.manager = manager; + } + @Override + public void updateText() { + + } + + @Override + public LocaleManager getLocaleManager() { + return manager; + } +} diff --git a/client/src/main/java/client/scenes/nutrition/NutritionViewCtrl.java b/client/src/main/java/client/scenes/nutrition/NutritionViewCtrl.java new file mode 100644 index 0000000..493db8e --- /dev/null +++ b/client/src/main/java/client/scenes/nutrition/NutritionViewCtrl.java @@ -0,0 +1,68 @@ +package client.scenes.nutrition; + +import client.scenes.FoodpalApplicationCtrl; +import com.google.inject.Inject; +import commons.Recipe; +import javafx.collections.ListChangeListener; +import javafx.collections.ObservableList; +import javafx.scene.control.ListView; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class NutritionViewCtrl { + private ObservableList recipes; + private HashMap ingredientStats; + public ListView nutritionIngredientsView; + private final NutritionDetailsCtrl nutritionDetailsCtrl; + + // TODO into Ingredient class definition + + /** + * Comedically verbose function to count unique appearances of an ingredient by name in each recipe. + * For each recipe: + * 1. Collect unique ingredients that appeared in that recipe. + * 2. For each unique ingredient in said recipe: + * 1. Initialize the appearance for that ingredient to 0. + * 2. For each recipe in list: + * 1. If the name of the ingredient exists in the recipe list, increment the statistic by 1. + * 2. Else maintain the same value for that statistic. + * @param recipeList + */ + private void updateIngredientStats( + List recipeList + ) { + recipeList.forEach(recipe -> { + Set uniqueIngredients = new HashSet<>(recipe.getIngredients()); + nutritionIngredientsView.getItems().setAll(uniqueIngredients); + uniqueIngredients.forEach(ingredient -> { + ingredientStats.put(ingredient, 0); + recipeList.forEach(r -> + ingredientStats.put( + ingredient, + ingredientStats.get(ingredient) + ( + (r.getIngredients().contains(ingredient)) + ? 1 : 0 + ) + ) + ); + }); + }); + } + @Inject + public NutritionViewCtrl( + FoodpalApplicationCtrl foodpalApplicationCtrl, + NutritionDetailsCtrl nutritionDetailsCtrl + ) { + this.recipes = foodpalApplicationCtrl.recipeList.getItems(); + this.recipes.addListener((ListChangeListener) _ -> { + updateIngredientStats(this.recipes); + }); + this.nutritionDetailsCtrl = nutritionDetailsCtrl; + this.nutritionIngredientsView.selectionModelProperty().addListener((observable, oldValue, newValue) -> { + + }); + } +} diff --git a/client/src/main/resources/client/scenes/nutrition/NutritionDetails.fxml b/client/src/main/resources/client/scenes/nutrition/NutritionDetails.fxml new file mode 100644 index 0000000..1820eb4 --- /dev/null +++ b/client/src/main/resources/client/scenes/nutrition/NutritionDetails.fxml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + diff --git a/client/src/main/resources/client/scenes/nutrition/NutritionView.fxml b/client/src/main/resources/client/scenes/nutrition/NutritionView.fxml new file mode 100644 index 0000000..428694e --- /dev/null +++ b/client/src/main/resources/client/scenes/nutrition/NutritionView.fxml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + +