diff --git a/client/src/main/java/client/MyModule.java b/client/src/main/java/client/MyModule.java
index f0d95ed..9f27a8c 100644
--- a/client/src/main/java/client/MyModule.java
+++ b/client/src/main/java/client/MyModule.java
@@ -16,6 +16,7 @@
package client;
import client.scenes.FoodpalApplicationCtrl;
+import client.scenes.recipe.IngredientListCtrl;
import client.utils.LocaleManager;
import com.google.inject.Binder;
import com.google.inject.Module;
@@ -31,7 +32,7 @@ public class MyModule implements Module {
binder.bind(MainCtrl.class).in(Scopes.SINGLETON);
binder.bind(AddNameCtrl.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);
}
}
\ No newline at end of file
diff --git a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java
index e2b8abf..7fcf49b 100644
--- a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java
+++ b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java
@@ -7,6 +7,7 @@ import java.util.List;
import java.util.Locale;
import client.exception.UpdateException;
+import client.scenes.recipe.IngredientListCtrl;
import client.utils.DefaultRecipeFactory;
import client.utils.LocaleAware;
import client.utils.LocaleManager;
@@ -31,6 +32,7 @@ public class FoodpalApplicationCtrl implements LocaleAware {
private final MainCtrl mainCtrl;
private final ServerUtils server;
private final LocaleManager localeManager;
+ private final IngredientListCtrl ingredientListCtrl;
public VBox detailsScreen;
public HBox editableTitleArea;
@@ -93,14 +95,35 @@ public class FoodpalApplicationCtrl implements LocaleAware {
private Button addPreparationStepButton;
@Inject
- public FoodpalApplicationCtrl(MainCtrl mainCtrl, ServerUtils server, LocaleManager localeManager) {
+ public FoodpalApplicationCtrl(
+ MainCtrl mainCtrl,
+ ServerUtils server,
+ LocaleManager localeManager,
+ IngredientListCtrl ingredientListCtrl
+ ) {
this.mainCtrl = mainCtrl;
this.server = server;
this.localeManager = localeManager;
+ this.ingredientListCtrl = ingredientListCtrl;
}
@Override
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
recipeList.setCellFactory(list -> new ListCell<>() {
@Override
@@ -140,10 +163,10 @@ public class FoodpalApplicationCtrl implements LocaleAware {
printRecipeButton.setText(getLocaleString("menu.button.print"));
recipesLabel.setText(getLocaleString("menu.label.recipes"));
- ingredientsLabel.setText(getLocaleString("menu.label.ingredients"));
+ // ingredientsLabel.setText(getLocaleString("menu.label.ingredients"));
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"));
}
@@ -154,12 +177,11 @@ public class FoodpalApplicationCtrl implements LocaleAware {
private void showRecipeDetails(Recipe recipe) {
if (recipe == null) {
- ingredientsListView.getItems().clear();
preparationListView.getItems().clear();
return;
}
showName(recipe.getName());
- ingredientsListView.getItems().setAll(recipe.getIngredients());
+ ingredientListCtrl.refetchFromRecipe(recipe);
preparationListView.getItems().setAll(recipe.getPreparationSteps());
}
diff --git a/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java b/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java
index 74193e6..a772913 100644
--- a/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java
+++ b/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java
@@ -5,7 +5,7 @@ import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
-import java.util.function.Function;
+import java.util.function.Consumer;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
@@ -29,7 +29,7 @@ public class IngredientListCtrl implements Initializable {
* As the ingredient list in {@link Recipe} is immutable,
* this copies that list upon initialization to this mutable list.
*
- * 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.
*