From 1542117ac61b080f8fd7255cc0d8cbd786c7cd3d Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Thu, 22 Jan 2026 22:55:56 +0100 Subject: [PATCH] fix: refresh the favourites list if any change occurs to the original list --- .../client/scenes/FoodpalApplicationCtrl.java | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java index d553712..7ba5d3d 100644 --- a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java +++ b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java @@ -32,6 +32,10 @@ import commons.ws.messages.Message; import commons.ws.messages.UpdateRecipeMessage; import jakarta.inject.Inject; import javafx.application.Platform; +import javafx.beans.property.ListProperty; +import javafx.beans.property.SimpleListProperty; +import javafx.collections.FXCollections; +import javafx.collections.ListChangeListener; import javafx.fxml.FXML; import javafx.scene.control.Alert; import javafx.scene.control.Button; @@ -66,8 +70,7 @@ public class FoodpalApplicationCtrl implements LocaleAware { @FXML public ListView recipeList; - @FXML - private ListView ingredientListView; + private final ListProperty favouriteRecipeList = new SimpleListProperty<>(); @FXML private Button addRecipeButton; @@ -88,8 +91,6 @@ public class FoodpalApplicationCtrl implements LocaleAware { @FXML private Button manageIngredientsButton; - private List allRecipes = new ArrayList<>(); - @FXML private Label updatedBadge; @@ -287,6 +288,13 @@ public class FoodpalApplicationCtrl implements LocaleAware { openSelectedRecipe(); } }); + recipeList.getItems().addListener((ListChangeListener.Change c) -> { + favouriteRecipeList.set( + FXCollections.observableList( + recipeList.getItems().stream().filter(r -> config.isFavourite(r.getId())).toList() + )); + System.out.println(favouriteRecipeList); + }); this.initializeSearchBar(); refresh(); @@ -325,8 +333,7 @@ public class FoodpalApplicationCtrl implements LocaleAware { logger.severe(msg); printError(msg); } - - allRecipes = new ArrayList<>(recipes); + recipeList.getItems().setAll(recipes); applyRecipeFilterAndKeepSelection(); showUpdatedBadge(); @@ -422,16 +429,20 @@ public class FoodpalApplicationCtrl implements LocaleAware { public void applyRecipeFilterAndKeepSelection() { Recipe selected = recipeList.getSelectionModel().getSelectedItem(); Long selectedId = selected == null ? null : selected.getId(); - - List view = allRecipes; + List view = recipeList.getItems().stream().toList(); if (favouritesOnlyToggle != null && favouritesOnlyToggle.isSelected()) { - view = allRecipes.stream() - .filter(r -> config.isFavourite(r.getId())) - .collect(Collectors.toList()); + view = favouriteRecipeList.get(); + recipeList.getItems().setAll(view); + } + try { + if (favouritesOnlyToggle != null && !favouritesOnlyToggle.isSelected()) { + recipeList.getItems().setAll(server.getRecipes(config.getRecipeLanguages())); + } + } + catch (IOException | InterruptedException e) { + logger.severe(e.getMessage()); } - - recipeList.getItems().setAll(view); // restore selection if possible if (selectedId != null) {