added highlighting fav recipes + list
This commit is contained in:
parent
2566eb450f
commit
086b8539c3
2 changed files with 70 additions and 20 deletions
|
|
@ -2,8 +2,10 @@ package client.scenes;
|
||||||
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import client.exception.UpdateException;
|
import client.exception.UpdateException;
|
||||||
import client.scenes.recipe.IngredientListCtrl;
|
import client.scenes.recipe.IngredientListCtrl;
|
||||||
|
|
@ -29,6 +31,7 @@ import javafx.scene.control.Label;
|
||||||
import javafx.scene.control.ListCell;
|
import javafx.scene.control.ListCell;
|
||||||
import javafx.scene.control.ListView;
|
import javafx.scene.control.ListView;
|
||||||
import javafx.scene.control.TextField;
|
import javafx.scene.control.TextField;
|
||||||
|
import javafx.scene.control.ToggleButton;
|
||||||
import javafx.scene.input.KeyCode;
|
import javafx.scene.input.KeyCode;
|
||||||
import javafx.scene.layout.HBox;
|
import javafx.scene.layout.HBox;
|
||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.layout.VBox;
|
||||||
|
|
@ -65,6 +68,7 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
@FXML
|
@FXML
|
||||||
private Button favouriteButton;
|
private Button favouriteButton;
|
||||||
|
|
||||||
|
// stores the users favourites and load and saves them
|
||||||
private Config config;
|
private Config config;
|
||||||
private final ConfigService configService;
|
private final ConfigService configService;
|
||||||
|
|
||||||
|
|
@ -80,6 +84,12 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
@FXML
|
@FXML
|
||||||
public Button printRecipeButton;
|
public Button printRecipeButton;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private ToggleButton favouritesOnlyToggle;
|
||||||
|
|
||||||
|
private List<Recipe> allRecipes = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public FoodpalApplicationCtrl(
|
public FoodpalApplicationCtrl(
|
||||||
ServerUtils server,
|
ServerUtils server,
|
||||||
|
|
@ -105,7 +115,12 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
@Override
|
@Override
|
||||||
protected void updateItem(Recipe item, boolean empty) {
|
protected void updateItem(Recipe item, boolean empty) {
|
||||||
super.updateItem(item, empty);
|
super.updateItem(item, empty);
|
||||||
setText(empty || item == null ? "" : item.getName());
|
if (empty || item == null) {
|
||||||
|
setText("");
|
||||||
|
return;
|
||||||
|
} //gives star in front fav items
|
||||||
|
boolean fav = config.isFavourite(item.getId());
|
||||||
|
setText((fav ? "★ " : "") + item.getName());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// When your selection changes, update details in the panel
|
// When your selection changes, update details in the panel
|
||||||
|
|
@ -172,13 +187,11 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
// Initialize callback for ingredient list updates
|
// Initialize callback for ingredient list updates
|
||||||
this.ingredientListCtrl.setUpdateCallback(newList -> {
|
this.ingredientListCtrl.setUpdateCallback(newList -> {
|
||||||
Recipe selectedRecipe = recipeList.getSelectionModel().getSelectedItem();
|
Recipe selectedRecipe = recipeList.getSelectionModel().getSelectedItem();
|
||||||
if (selectedRecipe == null) {
|
if (selectedRecipe == null) { // edge case error for NPE.
|
||||||
// edge case error for NPE.
|
|
||||||
throw new NullPointerException("Null recipe whereas ingredients are edited");
|
throw new NullPointerException("Null recipe whereas ingredients are edited");
|
||||||
}
|
}
|
||||||
selectedRecipe.setIngredients(newList);
|
selectedRecipe.setIngredients(newList);
|
||||||
try {
|
try { // propagate changes to server
|
||||||
// propagate changes to server
|
|
||||||
server.updateRecipe(selectedRecipe);
|
server.updateRecipe(selectedRecipe);
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
throw new UpdateException("Unable to update recipe to server for " + selectedRecipe);
|
throw new UpdateException("Unable to update recipe to server for " + selectedRecipe);
|
||||||
|
|
@ -186,13 +199,11 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
});
|
});
|
||||||
this.stepListCtrl.setUpdateCallback(newList -> {
|
this.stepListCtrl.setUpdateCallback(newList -> {
|
||||||
Recipe selectedRecipe = recipeList.getSelectionModel().getSelectedItem();
|
Recipe selectedRecipe = recipeList.getSelectionModel().getSelectedItem();
|
||||||
if (selectedRecipe == null) {
|
if (selectedRecipe == null) { // edge case error for NPE.
|
||||||
// edge case error for NPE.
|
|
||||||
throw new NullPointerException("Null recipe whereas ingredients are edited");
|
throw new NullPointerException("Null recipe whereas ingredients are edited");
|
||||||
}
|
}
|
||||||
selectedRecipe.setPreparationSteps(newList);
|
selectedRecipe.setPreparationSteps(newList);
|
||||||
try {
|
try { // propagate changes to server
|
||||||
// propagate changes to server
|
|
||||||
server.updateRecipe(selectedRecipe);
|
server.updateRecipe(selectedRecipe);
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
throw new UpdateException("Unable to update recipe to server for " + selectedRecipe);
|
throw new UpdateException("Unable to update recipe to server for " + selectedRecipe);
|
||||||
|
|
@ -216,7 +227,6 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
refresh();
|
refresh();
|
||||||
updateFavouriteButton(recipeList.getSelectionModel().getSelectedItem());
|
updateFavouriteButton(recipeList.getSelectionModel().getSelectedItem());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showName(String name) {
|
private void showName(String name) {
|
||||||
final int NAME_FONT_SIZE = 20;
|
final int NAME_FONT_SIZE = 20;
|
||||||
editableTitleArea.getChildren().clear();
|
editableTitleArea.getChildren().clear();
|
||||||
|
|
@ -224,17 +234,14 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
nameLabel.setFont(new Font("System Bold", NAME_FONT_SIZE));
|
nameLabel.setFont(new Font("System Bold", NAME_FONT_SIZE));
|
||||||
editableTitleArea.getChildren().add(nameLabel);
|
editableTitleArea.getChildren().add(nameLabel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateText() {
|
public void updateText() {
|
||||||
addRecipeButton.setText(getLocaleString("menu.button.add.recipe"));
|
addRecipeButton.setText(getLocaleString("menu.button.add.recipe"));
|
||||||
removeRecipeButton.setText(getLocaleString("menu.button.remove.recipe"));
|
removeRecipeButton.setText(getLocaleString("menu.button.remove.recipe"));
|
||||||
cloneRecipeButton.setText(getLocaleString("menu.button.clone"));
|
cloneRecipeButton.setText(getLocaleString("menu.button.clone"));
|
||||||
|
|
||||||
editRecipeTitleButton.setText(getLocaleString("menu.button.edit"));
|
editRecipeTitleButton.setText(getLocaleString("menu.button.edit"));
|
||||||
removeRecipeButton2.setText(getLocaleString("menu.button.remove.recipe"));
|
removeRecipeButton2.setText(getLocaleString("menu.button.remove.recipe"));
|
||||||
printRecipeButton.setText(getLocaleString("menu.button.print"));
|
printRecipeButton.setText(getLocaleString("menu.button.print"));
|
||||||
|
|
||||||
recipesLabel.setText(getLocaleString("menu.label.recipes"));
|
recipesLabel.setText(getLocaleString("menu.label.recipes"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -263,13 +270,8 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
System.err.println("Failed to load recipes: " + e.getMessage());
|
System.err.println("Failed to load recipes: " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
recipeList.getItems().setAll(recipes);
|
allRecipes = new ArrayList<>(recipes);
|
||||||
|
applyRecipeFilterAndKeepSelection();
|
||||||
// Select first recipe in the list by default
|
|
||||||
if (!recipes.isEmpty()) {
|
|
||||||
recipeList.getSelectionModel().selectFirst();
|
|
||||||
}
|
|
||||||
detailsScreen.visibleProperty().set(!recipes.isEmpty());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void printError(String msg) {
|
private void printError(String msg) {
|
||||||
|
|
@ -408,12 +410,58 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
}
|
}
|
||||||
|
|
||||||
configService.save();
|
configService.save();
|
||||||
|
|
||||||
|
//instant ui update
|
||||||
|
applyRecipeFilterAndKeepSelection();
|
||||||
|
recipeList.refresh();
|
||||||
updateFavouriteButton(selected);
|
updateFavouriteButton(selected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private void toggleFavouritesView() {
|
||||||
|
applyRecipeFilterAndKeepSelection();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void applyRecipeFilterAndKeepSelection() {
|
||||||
|
Recipe selected = recipeList.getSelectionModel().getSelectedItem();
|
||||||
|
Long selectedId = selected == null ? null : selected.getId();
|
||||||
|
|
||||||
|
List<Recipe> view = allRecipes;
|
||||||
|
|
||||||
|
if (favouritesOnlyToggle != null && favouritesOnlyToggle.isSelected()) {
|
||||||
|
view = allRecipes.stream()
|
||||||
|
.filter(r -> config.isFavourite(r.getId()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
recipeList.getItems().setAll(view);
|
||||||
|
|
||||||
|
// restore selection if possible
|
||||||
|
if (selectedId != null) {
|
||||||
|
Recipe match = view.stream()
|
||||||
|
.filter(r -> r.getId().equals(selectedId))
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
|
||||||
|
if (match != null) {
|
||||||
|
recipeList.getSelectionModel().select(match);
|
||||||
|
} else if (!view.isEmpty()) {
|
||||||
|
recipeList.getSelectionModel().selectFirst();
|
||||||
|
}
|
||||||
|
} else if (!view.isEmpty()) {
|
||||||
|
recipeList.getSelectionModel().selectFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
updateFavouriteButton(recipeList.getSelectionModel().getSelectedItem());
|
||||||
|
detailsScreen.visibleProperty().set(!recipeList.getItems().isEmpty());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
<?import javafx.scene.layout.RowConstraints?>
|
<?import javafx.scene.layout.RowConstraints?>
|
||||||
<?import javafx.scene.layout.VBox?>
|
<?import javafx.scene.layout.VBox?>
|
||||||
<?import javafx.scene.text.Font?>
|
<?import javafx.scene.text.Font?>
|
||||||
|
<?import javafx.scene.control.ToggleButton?>
|
||||||
|
|
||||||
<?import javafx.scene.control.ComboBox?>
|
<?import javafx.scene.control.ComboBox?>
|
||||||
<BorderPane prefHeight="800.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/25" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.scenes.FoodpalApplicationCtrl">
|
<BorderPane prefHeight="800.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/25" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.scenes.FoodpalApplicationCtrl">
|
||||||
|
|
@ -53,6 +54,7 @@
|
||||||
<Button fx:id="addRecipeButton" onAction="#addRecipe" text="Add Recipe" />
|
<Button fx:id="addRecipeButton" onAction="#addRecipe" text="Add Recipe" />
|
||||||
<Button fx:id="removeRecipeButton" onAction="#removeSelectedRecipe" text="Remove Recipe" />
|
<Button fx:id="removeRecipeButton" onAction="#removeSelectedRecipe" text="Remove Recipe" />
|
||||||
<Button fx:id= "cloneRecipeButton" mnemonicParsing="false" onAction="#cloneRecipe" text="Clone" />
|
<Button fx:id= "cloneRecipeButton" mnemonicParsing="false" onAction="#cloneRecipe" text="Clone" />
|
||||||
|
<ToggleButton fx:id="favouritesOnlyToggle" text="Favourites" onAction="#toggleFavouritesView"/>
|
||||||
</HBox>
|
</HBox>
|
||||||
</VBox>
|
</VBox>
|
||||||
</left>
|
</left>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue