diff --git a/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java b/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java index cffdd05..c8bfa7b 100644 --- a/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java +++ b/client/src/main/java/client/scenes/recipe/IngredientListCtrl.java @@ -5,86 +5,109 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; +import java.util.function.Function; import javafx.collections.FXCollections; import javafx.collections.ObservableList; +import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.ListView; +import javafx.scene.control.ListView.EditEvent; public class IngredientListCtrl implements Initializable { - private Recipe recipe; - private ObservableList ingredients; + private ObservableList ingredients; + private Function, Void> updateCallback; - @FXML - ListView ingredientListView; + @FXML ListView ingredientListView; - @FXML - Button addIngredientButton; - @FXML - Button deleteIngredientButton; + @FXML Button addIngredientButton; + @FXML Button deleteIngredientButton; - public void setRecipe(Recipe recipe) { - this.recipe = recipe; - this.fetchFromRecipe(); + /** + * Set the recipe and refetch data from it. + * + * @param recipe The recipe to fetch data from. + */ + public void refetchFromRecipe(Recipe recipe) { + if (recipe == null) { + this.ingredients = FXCollections.observableArrayList(new ArrayList<>()); + } else { + List ingredientList = recipe.getIngredients(); + this.ingredients = FXCollections.observableArrayList(ingredientList); } - private void fetchFromRecipe() { - if (recipe == null) { - this.ingredients = FXCollections.observableArrayList(new ArrayList<>()); - } else { - List ingredientList = recipe.getIngredients(); - this.ingredients = FXCollections.observableArrayList(ingredientList); - } + this.ingredientListView.setItems(this.ingredients); + this.refresh(); + } - this.ingredientListView.setItems(this.ingredients); - this.refresh(); + /** + * Set a callback that's called when the ingredient list changes. + * + * @param callback The function to call upon each update. + */ + public void setUpdateCallback(Function, Void> callback) { + this.updateCallback = callback; + } + + private void refresh() { ingredientListView.refresh(); } + + /** + * Handle ingredient addition. Automatically calls update callback. + */ + private void handleIngredientAdd(ActionEvent event) { + this.ingredients.add("Ingredient " + (this.ingredients.size() + 1)); + this.refresh(); + this.updateCallback.apply(this.ingredients); + + var select = this.ingredientListView.getSelectionModel(); + select.select(this.ingredients.size() - 1); + } + + /** + * Handle ingredient edits. Automatically calls update callback. + */ + private void handleIngredientEdit(EditEvent event) { + int index = event.getIndex(); + String newValue = event.getNewValue(); + + this.ingredients.set(index, newValue); + this.refresh(); + this.updateCallback.apply(this.ingredients); + } + + /** + * Handle ingredient deletion. Automatically calls update callback. + */ + private void handleIngredientDelete(ActionEvent event) { + var select = this.ingredientListView.getSelectionModel(); + int selectedIndex = select.getSelectedIndex(); + // No index is selected, don't do anything + if (selectedIndex < 0) { + return; } - private void updateRecipeIngredients() { - if (this.recipe != null) { - List updatedIngredients = new ArrayList<>(this.ingredients); - this.recipe.setIngredients(updatedIngredients); - } - } + this.ingredients.remove(selectedIndex); + this.refresh(); + this.updateCallback.apply(this.ingredients); + } - private void refresh() { - ingredientListView.refresh(); - } + @FXML + public void initialize(URL location, ResourceBundle resources) { + // TODO: set up communication with the server + // this would probably be best done with the callback (so this class doesn't + // interact with the server at all) - @FXML - public void initialize(URL location, ResourceBundle resources) { - this.ingredientListView.setEditable(true); - this.ingredientListView.setCellFactory( - list -> { - return new IngredientListCell(); - }); + this.ingredientListView.setEditable(true); + this.ingredientListView.setCellFactory( + list -> { return new IngredientListCell(); }); - this.ingredientListView.setOnEditCommit(event -> { - int index = event.getIndex(); - String newValue = event.getNewValue(); - this.ingredients.set(index, newValue); - this.updateRecipeIngredients(); - }); + this.ingredientListView.setOnEditCommit( + event -> handleIngredientEdit(event)); + this.addIngredientButton.setOnAction(event -> handleIngredientAdd(event)); + this.deleteIngredientButton.setOnAction( + event -> handleIngredientDelete(event)); - // TODO: actually communicate with the server :) - this.addIngredientButton.setOnAction(event -> { - this.ingredients.add("Ingredient " + (this.ingredients.size() + 1)); - this.updateRecipeIngredients(); - - this.ingredientListView.getSelectionModel().select( - this.ingredients.size() - 1); - }); - - this.deleteIngredientButton.setOnAction(event -> { - int selectedIndex = this.ingredientListView.getSelectionModel().getSelectedIndex(); - if (selectedIndex >= 0) { - this.ingredients.remove(selectedIndex); - } - - this.updateRecipeIngredients(); - }); - - this.refresh(); - } + this.refresh(); + } }