refactor: ingredient list

This commit is contained in:
Natalia Cholewa 2025-11-28 13:09:12 +01:00
commit 14b2b2259a

View file

@ -5,31 +5,31 @@ 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 javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.ListView; import javafx.scene.control.ListView;
import javafx.scene.control.ListView.EditEvent;
public class IngredientListCtrl implements Initializable { public class IngredientListCtrl implements Initializable {
private Recipe recipe;
private ObservableList<String> ingredients; private ObservableList<String> ingredients;
private Function<List<String>, Void> updateCallback;
@FXML @FXML ListView<String> ingredientListView;
ListView<String> ingredientListView;
@FXML @FXML Button addIngredientButton;
Button addIngredientButton; @FXML Button deleteIngredientButton;
@FXML
Button deleteIngredientButton;
public void setRecipe(Recipe recipe) { /**
this.recipe = recipe; * Set the recipe and refetch data from it.
this.fetchFromRecipe(); *
} * @param recipe The recipe to fetch data from.
*/
private void fetchFromRecipe() { public void refetchFromRecipe(Recipe recipe) {
if (recipe == null) { if (recipe == null) {
this.ingredients = FXCollections.observableArrayList(new ArrayList<>()); this.ingredients = FXCollections.observableArrayList(new ArrayList<>());
} else { } else {
@ -41,49 +41,72 @@ public class IngredientListCtrl implements Initializable {
this.refresh(); this.refresh();
} }
private void updateRecipeIngredients() { /**
if (this.recipe != null) { * Set a callback that's called when the ingredient list changes.
List<String> updatedIngredients = new ArrayList<>(this.ingredients); *
this.recipe.setIngredients(updatedIngredients); * @param callback The function to call upon each update.
} */
public void setUpdateCallback(Function<List<String>, Void> callback) {
this.updateCallback = callback;
} }
private void refresh() { private void refresh() { ingredientListView.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<String> 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;
}
this.ingredients.remove(selectedIndex);
this.refresh();
this.updateCallback.apply(this.ingredients);
} }
@FXML @FXML
public void initialize(URL location, ResourceBundle resources) { 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)
this.ingredientListView.setEditable(true); this.ingredientListView.setEditable(true);
this.ingredientListView.setCellFactory( this.ingredientListView.setCellFactory(
list -> { list -> { return new IngredientListCell(); });
return new IngredientListCell();
});
this.ingredientListView.setOnEditCommit(event -> { this.ingredientListView.setOnEditCommit(
int index = event.getIndex(); event -> handleIngredientEdit(event));
String newValue = event.getNewValue(); this.addIngredientButton.setOnAction(event -> handleIngredientAdd(event));
this.ingredients.set(index, newValue); this.deleteIngredientButton.setOnAction(
this.updateRecipeIngredients(); 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();
} }