feat(client/kcal): ui logic for kcal eval

This commit is contained in:
Zhongheng Liu 2026-01-18 18:19:11 +01:00
commit 7e5137b317
Signed by: steven
GPG key ID: F69B980899C1C09D
4 changed files with 19 additions and 3 deletions

View file

@ -66,7 +66,7 @@ public class NutritionDetailsCtrl implements LocaleAware {
this.proteinInputElement.textProperty().bindBidirectional(vm.proteinProperty(), new NumberStringConverter()); this.proteinInputElement.textProperty().bindBidirectional(vm.proteinProperty(), new NumberStringConverter());
this.carbInputElement.textProperty().bindBidirectional(vm.carbsProperty(), new NumberStringConverter()); this.carbInputElement.textProperty().bindBidirectional(vm.carbsProperty(), new NumberStringConverter());
this.estimatedKcalLabel.textProperty().bind(Bindings.createStringBinding( this.estimatedKcalLabel.textProperty().bind(Bindings.createStringBinding(
() -> String.format("Estimated energy value: %.1f", vm.getKcal()), vm.kcalProperty() () -> String.format("Estimated energy value: %.1f kcal/100g", vm.getKcal()), vm.kcalProperty()
)); ));
}); });
this.nutritionValueContainer.addEventHandler(KeyEvent.KEY_RELEASED, event -> { this.nutritionValueContainer.addEventHandler(KeyEvent.KEY_RELEASED, event -> {

View file

@ -18,6 +18,8 @@ import java.nio.file.Path;
import java.util.Optional; import java.util.Optional;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Consumer; import java.util.function.Consumer;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.ComboBox; import javafx.scene.control.ComboBox;
@ -46,6 +48,7 @@ public class RecipeDetailCtrl implements LocaleAware {
private final WebSocketDataService<Long, Recipe> webSocketDataService; private final WebSocketDataService<Long, Recipe> webSocketDataService;
public Spinner<Double> scaleSpinner; public Spinner<Double> scaleSpinner;
public Label inferredKcalLabel;
@FXML @FXML
private IngredientListCtrl ingredientListController; private IngredientListCtrl ingredientListController;
@ -156,10 +159,12 @@ public class RecipeDetailCtrl implements LocaleAware {
// Prevents issues from first startup // Prevents issues from first startup
if (scaleSpinner.getValue() != null) { if (scaleSpinner.getValue() != null) {
Double scale = scaleSpinner.getValue(); Double scale = scaleSpinner.getValue();
// see impl. creates a scaled context for the recipe such that its non-scaled value is kept as a reference. // see impl. creates a scaled context for the recipe such that its non-scaled value is kept as a reference.
this.recipeView = new ScalableRecipeView(recipe, scale); this.recipeView = new ScalableRecipeView(recipe, scale);
// TODO i18n
inferredKcalLabel.textProperty().bind(Bindings.createStringBinding(() ->
String.format("Inferred %.1f kcal/100g for this recipe", this.recipeView.scaledKcalProperty().get())
, this.recipeView.scaledKcalProperty()));
// expose the scaled view to list controllers // expose the scaled view to list controllers
this.ingredientListController.refetchFromRecipe(this.recipeView.getScaled()); this.ingredientListController.refetchFromRecipe(this.recipeView.getScaled());
this.stepListController.refetchFromRecipe(this.recipeView.getScaled()); this.stepListController.refetchFromRecipe(this.recipeView.getScaled());

View file

@ -12,6 +12,7 @@ public class ScalableRecipeView {
private final ObjectProperty<Recipe> recipe = new SimpleObjectProperty<>(); private final ObjectProperty<Recipe> recipe = new SimpleObjectProperty<>();
private final ObjectProperty<Recipe> scaled = new SimpleObjectProperty<>(); private final ObjectProperty<Recipe> scaled = new SimpleObjectProperty<>();
private final DoubleProperty scale = new SimpleDoubleProperty(); private final DoubleProperty scale = new SimpleDoubleProperty();
private final SimpleDoubleProperty scaledKcal = new SimpleDoubleProperty();
public ScalableRecipeView( public ScalableRecipeView(
Recipe recipe, Recipe recipe,
Double scale Double scale
@ -22,6 +23,7 @@ public class ScalableRecipeView {
() -> Recipe.getScaled(this.recipe.get(), this.scale.get()), () -> Recipe.getScaled(this.recipe.get(), this.scale.get()),
this.recipe, this.scale); this.recipe, this.scale);
this.scaled.bind(binding); this.scaled.bind(binding);
this.scaledKcal.bind(Bindings.createDoubleBinding(() -> this.scaled.get().kcal(), this.scaled));
} }
public double getScale() { public double getScale() {
@ -36,6 +38,10 @@ public class ScalableRecipeView {
return scaled.get(); return scaled.get();
} }
public double getScaledKcal() {
return scaledKcal.get();
}
public DoubleProperty scaleProperty() { public DoubleProperty scaleProperty() {
return scale; return scale;
} }
@ -47,4 +53,8 @@ public class ScalableRecipeView {
public ObjectProperty<Recipe> recipeProperty() { public ObjectProperty<Recipe> recipeProperty() {
return recipe; return recipe;
} }
public SimpleDoubleProperty scaledKcalProperty() {
return scaledKcal;
}
} }

View file

@ -37,4 +37,5 @@
<!-- Preparation --> <!-- Preparation -->
<fx:include source="RecipeStepList.fxml" fx:id="stepList" <fx:include source="RecipeStepList.fxml" fx:id="stepList"
VBox.vgrow="ALWAYS" maxWidth="Infinity" /> VBox.vgrow="ALWAYS" maxWidth="Infinity" />
<Label fx:id="inferredKcalLabel" />
</VBox> </VBox>