fix: refresh the favourites list if any change occurs to the original list
This commit is contained in:
parent
ad199ea244
commit
1542117ac6
1 changed files with 24 additions and 13 deletions
|
|
@ -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<Recipe> recipeList;
|
||||
|
||||
@FXML
|
||||
private ListView<Ingredient> ingredientListView;
|
||||
private final ListProperty<Recipe> favouriteRecipeList = new SimpleListProperty<>();
|
||||
|
||||
@FXML
|
||||
private Button addRecipeButton;
|
||||
|
|
@ -88,8 +91,6 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
|||
@FXML
|
||||
private Button manageIngredientsButton;
|
||||
|
||||
private List<Recipe> allRecipes = new ArrayList<>();
|
||||
|
||||
@FXML
|
||||
private Label updatedBadge;
|
||||
|
||||
|
|
@ -287,6 +288,13 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
|||
openSelectedRecipe();
|
||||
}
|
||||
});
|
||||
recipeList.getItems().addListener((ListChangeListener.Change<? extends Recipe> 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<Recipe> view = allRecipes;
|
||||
List<Recipe> 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());
|
||||
}
|
||||
|
||||
// restore selection if possible
|
||||
if (selectedId != null) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue