feat: integrate ingredient list updates
This commit is contained in:
parent
bc3744aee5
commit
2ce855aa3d
5 changed files with 42 additions and 27 deletions
|
|
@ -16,6 +16,7 @@
|
||||||
package client;
|
package client;
|
||||||
|
|
||||||
import client.scenes.FoodpalApplicationCtrl;
|
import client.scenes.FoodpalApplicationCtrl;
|
||||||
|
import client.scenes.recipe.IngredientListCtrl;
|
||||||
import client.utils.LocaleManager;
|
import client.utils.LocaleManager;
|
||||||
import com.google.inject.Binder;
|
import com.google.inject.Binder;
|
||||||
import com.google.inject.Module;
|
import com.google.inject.Module;
|
||||||
|
|
@ -31,7 +32,7 @@ public class MyModule implements Module {
|
||||||
binder.bind(MainCtrl.class).in(Scopes.SINGLETON);
|
binder.bind(MainCtrl.class).in(Scopes.SINGLETON);
|
||||||
binder.bind(AddNameCtrl.class).in(Scopes.SINGLETON);
|
binder.bind(AddNameCtrl.class).in(Scopes.SINGLETON);
|
||||||
binder.bind(FoodpalApplicationCtrl.class).in(Scopes.SINGLETON);
|
binder.bind(FoodpalApplicationCtrl.class).in(Scopes.SINGLETON);
|
||||||
|
binder.bind(IngredientListCtrl.class).in(Scopes.SINGLETON);
|
||||||
binder.bind(LocaleManager.class).in(Scopes.SINGLETON);
|
binder.bind(LocaleManager.class).in(Scopes.SINGLETON);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -7,6 +7,7 @@ import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import client.exception.UpdateException;
|
import client.exception.UpdateException;
|
||||||
|
import client.scenes.recipe.IngredientListCtrl;
|
||||||
import client.utils.DefaultRecipeFactory;
|
import client.utils.DefaultRecipeFactory;
|
||||||
import client.utils.LocaleAware;
|
import client.utils.LocaleAware;
|
||||||
import client.utils.LocaleManager;
|
import client.utils.LocaleManager;
|
||||||
|
|
@ -31,6 +32,7 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
private final MainCtrl mainCtrl;
|
private final MainCtrl mainCtrl;
|
||||||
private final ServerUtils server;
|
private final ServerUtils server;
|
||||||
private final LocaleManager localeManager;
|
private final LocaleManager localeManager;
|
||||||
|
private final IngredientListCtrl ingredientListCtrl;
|
||||||
|
|
||||||
public VBox detailsScreen;
|
public VBox detailsScreen;
|
||||||
public HBox editableTitleArea;
|
public HBox editableTitleArea;
|
||||||
|
|
@ -93,14 +95,35 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
private Button addPreparationStepButton;
|
private Button addPreparationStepButton;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public FoodpalApplicationCtrl(MainCtrl mainCtrl, ServerUtils server, LocaleManager localeManager) {
|
public FoodpalApplicationCtrl(
|
||||||
|
MainCtrl mainCtrl,
|
||||||
|
ServerUtils server,
|
||||||
|
LocaleManager localeManager,
|
||||||
|
IngredientListCtrl ingredientListCtrl
|
||||||
|
) {
|
||||||
this.mainCtrl = mainCtrl;
|
this.mainCtrl = mainCtrl;
|
||||||
this.server = server;
|
this.server = server;
|
||||||
this.localeManager = localeManager;
|
this.localeManager = localeManager;
|
||||||
|
this.ingredientListCtrl = ingredientListCtrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initializeComponents() {
|
public void initializeComponents() {
|
||||||
|
// Initialize callback for ingredient list updates
|
||||||
|
this.ingredientListCtrl.setUpdateCallback(newList -> {
|
||||||
|
Recipe focusedRecipe = recipeList.getFocusModel().getFocusedItem();
|
||||||
|
if (focusedRecipe == null) {
|
||||||
|
// edge case error for NPE.
|
||||||
|
throw new NullPointerException("Null recipe whereas ingredients are edited");
|
||||||
|
}
|
||||||
|
focusedRecipe.setIngredients(newList);
|
||||||
|
try {
|
||||||
|
// propagate changes to server
|
||||||
|
server.updateRecipe(focusedRecipe);
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
throw new UpdateException("Unable to update recipe to server for " + focusedRecipe);
|
||||||
|
}
|
||||||
|
});
|
||||||
// Show recipe name in the list
|
// Show recipe name in the list
|
||||||
recipeList.setCellFactory(list -> new ListCell<>() {
|
recipeList.setCellFactory(list -> new ListCell<>() {
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -140,10 +163,10 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
printRecipeButton.setText(getLocaleString("menu.button.print"));
|
printRecipeButton.setText(getLocaleString("menu.button.print"));
|
||||||
|
|
||||||
recipesLabel.setText(getLocaleString("menu.label.recipes"));
|
recipesLabel.setText(getLocaleString("menu.label.recipes"));
|
||||||
ingredientsLabel.setText(getLocaleString("menu.label.ingredients"));
|
// ingredientsLabel.setText(getLocaleString("menu.label.ingredients"));
|
||||||
preparationLabel.setText(getLocaleString("menu.label.preparation"));
|
preparationLabel.setText(getLocaleString("menu.label.preparation"));
|
||||||
|
|
||||||
addIngredientButton.setText(getLocaleString("menu.button.add.ingredient"));
|
// addIngredientButton.setText(getLocaleString("menu.button.add.ingredient"));
|
||||||
addPreparationStepButton.setText(getLocaleString("menu.button.add.step"));
|
addPreparationStepButton.setText(getLocaleString("menu.button.add.step"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -154,12 +177,11 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
|
|
||||||
private void showRecipeDetails(Recipe recipe) {
|
private void showRecipeDetails(Recipe recipe) {
|
||||||
if (recipe == null) {
|
if (recipe == null) {
|
||||||
ingredientsListView.getItems().clear();
|
|
||||||
preparationListView.getItems().clear();
|
preparationListView.getItems().clear();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
showName(recipe.getName());
|
showName(recipe.getName());
|
||||||
ingredientsListView.getItems().setAll(recipe.getIngredients());
|
ingredientListCtrl.refetchFromRecipe(recipe);
|
||||||
preparationListView.getItems().setAll(recipe.getPreparationSteps());
|
preparationListView.getItems().setAll(recipe.getPreparationSteps());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
import java.util.function.Function;
|
import java.util.function.Consumer;
|
||||||
import javafx.collections.FXCollections;
|
import javafx.collections.FXCollections;
|
||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
|
|
@ -29,7 +29,7 @@ public class IngredientListCtrl implements Initializable {
|
||||||
* As the ingredient list in {@link Recipe} is immutable,
|
* As the ingredient list in {@link Recipe} is immutable,
|
||||||
* this copies that list upon initialization to this mutable list.
|
* this copies that list upon initialization to this mutable list.
|
||||||
* <br>
|
* <br>
|
||||||
* Please use the {@link #setUpdateCallback(Function)} function to listen for
|
* Please use the {@link #setUpdateCallback(Consumer)} function to listen for
|
||||||
* changes and update the recipe accordingly.
|
* changes and update the recipe accordingly.
|
||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
|
|
@ -38,15 +38,15 @@ public class IngredientListCtrl implements Initializable {
|
||||||
/**
|
/**
|
||||||
* A callback function that is called when the ingredient list is updated.
|
* A callback function that is called when the ingredient list is updated.
|
||||||
*/
|
*/
|
||||||
private Function<List<String>, Void> updateCallback;
|
private Consumer<List<String>> updateCallback;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
ListView<String> ingredientListView;
|
public ListView<String> ingredientListView;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
Button addIngredientButton;
|
public Button addIngredientButton;
|
||||||
@FXML
|
@FXML
|
||||||
Button deleteIngredientButton;
|
public Button deleteIngredientButton;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the recipe and refetch data from it.
|
* Set the recipe and refetch data from it.
|
||||||
|
|
@ -59,6 +59,7 @@ public class IngredientListCtrl implements Initializable {
|
||||||
if (recipe == null) {
|
if (recipe == null) {
|
||||||
this.ingredients = FXCollections.observableArrayList(new ArrayList<>());
|
this.ingredients = FXCollections.observableArrayList(new ArrayList<>());
|
||||||
} else {
|
} else {
|
||||||
|
System.out.println("refetched");
|
||||||
List<String> ingredientList = recipe.getIngredients();
|
List<String> ingredientList = recipe.getIngredients();
|
||||||
this.ingredients = FXCollections.observableArrayList(ingredientList);
|
this.ingredients = FXCollections.observableArrayList(ingredientList);
|
||||||
}
|
}
|
||||||
|
|
@ -72,7 +73,7 @@ public class IngredientListCtrl implements Initializable {
|
||||||
*
|
*
|
||||||
* @param callback The function to call upon each update.
|
* @param callback The function to call upon each update.
|
||||||
*/
|
*/
|
||||||
public void setUpdateCallback(Function<List<String>, Void> callback) {
|
public void setUpdateCallback(Consumer<List<String>> callback) {
|
||||||
this.updateCallback = callback;
|
this.updateCallback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -91,7 +92,7 @@ public class IngredientListCtrl implements Initializable {
|
||||||
private void handleIngredientAdd(ActionEvent event) {
|
private void handleIngredientAdd(ActionEvent event) {
|
||||||
this.ingredients.add("Ingredient " + (this.ingredients.size() + 1));
|
this.ingredients.add("Ingredient " + (this.ingredients.size() + 1));
|
||||||
this.refresh();
|
this.refresh();
|
||||||
this.updateCallback.apply(this.ingredients);
|
this.updateCallback.accept(this.ingredients);
|
||||||
|
|
||||||
var select = this.ingredientListView.getSelectionModel();
|
var select = this.ingredientListView.getSelectionModel();
|
||||||
select.select(this.ingredients.size() - 1);
|
select.select(this.ingredients.size() - 1);
|
||||||
|
|
@ -108,7 +109,7 @@ public class IngredientListCtrl implements Initializable {
|
||||||
|
|
||||||
this.ingredients.set(index, newValue);
|
this.ingredients.set(index, newValue);
|
||||||
this.refresh();
|
this.refresh();
|
||||||
this.updateCallback.apply(this.ingredients);
|
this.updateCallback.accept(this.ingredients);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -126,7 +127,7 @@ public class IngredientListCtrl implements Initializable {
|
||||||
|
|
||||||
this.ingredients.remove(selectedIndex);
|
this.ingredients.remove(selectedIndex);
|
||||||
this.refresh();
|
this.refresh();
|
||||||
this.updateCallback.apply(this.ingredients);
|
this.updateCallback.accept(this.ingredients);
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
|
|
@ -134,7 +135,6 @@ public class IngredientListCtrl implements Initializable {
|
||||||
// TODO: set up communication with the server
|
// TODO: set up communication with the server
|
||||||
// this would probably be best done with the callback (so this class doesn't
|
// this would probably be best done with the callback (so this class doesn't
|
||||||
// interact with the server at all)
|
// interact with the server at all)
|
||||||
|
|
||||||
this.ingredientListView.setEditable(true);
|
this.ingredientListView.setEditable(true);
|
||||||
this.ingredientListView.setCellFactory(
|
this.ingredientListView.setCellFactory(
|
||||||
list -> {
|
list -> {
|
||||||
|
|
|
||||||
|
|
@ -116,15 +116,7 @@
|
||||||
</HBox>
|
</HBox>
|
||||||
|
|
||||||
<!-- Ingredients -->
|
<!-- Ingredients -->
|
||||||
<VBox spacing="10">
|
<fx:include fx:id="ingredientsListContainer" source="recipe/IngredientList.fxml" />
|
||||||
<Label fx:id="ingredientsLabel" text="Ingredients">
|
|
||||||
<font>
|
|
||||||
<Font name="System Bold" size="14.0" />
|
|
||||||
</font>
|
|
||||||
</Label>
|
|
||||||
<ListView fx:id="ingredientsListView" prefHeight="167.0" prefWidth="912.0" />
|
|
||||||
<Button fx:id="addIngredientButton" onAction="#addIngredient" text="Add Ingredient" />
|
|
||||||
</VBox>
|
|
||||||
|
|
||||||
<!-- Preparation -->
|
<!-- Preparation -->
|
||||||
<Label fx:id="preparationLabel" text="Preparation">
|
<Label fx:id="preparationLabel" text="Preparation">
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
<?import javafx.scene.layout.VBox?>
|
<?import javafx.scene.layout.VBox?>
|
||||||
<?import javafx.scene.text.Font?>
|
<?import javafx.scene.text.Font?>
|
||||||
|
|
||||||
<AnchorPane xmlns="http://javafx.com/javafx/25" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.scenes.recipe.RecipeStepListCtrl">
|
<AnchorPane xmlns="http://javafx.com/javafx/25" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.scenes.recipe.IngredientListCtrl">
|
||||||
<children>
|
<children>
|
||||||
<VBox minHeight="200.0" minWidth="200.0">
|
<VBox minHeight="200.0" minWidth="200.0">
|
||||||
<children>
|
<children>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue