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 commons.ws.messages.UpdateRecipeMessage;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import javafx.application.Platform;
|
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.fxml.FXML;
|
||||||
import javafx.scene.control.Alert;
|
import javafx.scene.control.Alert;
|
||||||
import javafx.scene.control.Button;
|
import javafx.scene.control.Button;
|
||||||
|
|
@ -66,8 +70,7 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
@FXML
|
@FXML
|
||||||
public ListView<Recipe> recipeList;
|
public ListView<Recipe> recipeList;
|
||||||
|
|
||||||
@FXML
|
private final ListProperty<Recipe> favouriteRecipeList = new SimpleListProperty<>();
|
||||||
private ListView<Ingredient> ingredientListView;
|
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private Button addRecipeButton;
|
private Button addRecipeButton;
|
||||||
|
|
@ -88,8 +91,6 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
@FXML
|
@FXML
|
||||||
private Button manageIngredientsButton;
|
private Button manageIngredientsButton;
|
||||||
|
|
||||||
private List<Recipe> allRecipes = new ArrayList<>();
|
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private Label updatedBadge;
|
private Label updatedBadge;
|
||||||
|
|
||||||
|
|
@ -287,6 +288,13 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
openSelectedRecipe();
|
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();
|
this.initializeSearchBar();
|
||||||
refresh();
|
refresh();
|
||||||
|
|
@ -325,8 +333,7 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
logger.severe(msg);
|
logger.severe(msg);
|
||||||
printError(msg);
|
printError(msg);
|
||||||
}
|
}
|
||||||
|
recipeList.getItems().setAll(recipes);
|
||||||
allRecipes = new ArrayList<>(recipes);
|
|
||||||
applyRecipeFilterAndKeepSelection();
|
applyRecipeFilterAndKeepSelection();
|
||||||
|
|
||||||
showUpdatedBadge();
|
showUpdatedBadge();
|
||||||
|
|
@ -422,16 +429,20 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
public void applyRecipeFilterAndKeepSelection() {
|
public void applyRecipeFilterAndKeepSelection() {
|
||||||
Recipe selected = recipeList.getSelectionModel().getSelectedItem();
|
Recipe selected = recipeList.getSelectionModel().getSelectedItem();
|
||||||
Long selectedId = selected == null ? null : selected.getId();
|
Long selectedId = selected == null ? null : selected.getId();
|
||||||
|
List<Recipe> view = recipeList.getItems().stream().toList();
|
||||||
List<Recipe> view = allRecipes;
|
|
||||||
|
|
||||||
if (favouritesOnlyToggle != null && favouritesOnlyToggle.isSelected()) {
|
if (favouritesOnlyToggle != null && favouritesOnlyToggle.isSelected()) {
|
||||||
view = allRecipes.stream()
|
view = favouriteRecipeList.get();
|
||||||
.filter(r -> config.isFavourite(r.getId()))
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
recipeList.getItems().setAll(view);
|
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
|
// restore selection if possible
|
||||||
if (selectedId != null) {
|
if (selectedId != null) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue