refactor: ingredient list

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

View file

@ -5,86 +5,109 @@ 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.
*/
public void refetchFromRecipe(Recipe recipe) {
if (recipe == null) {
this.ingredients = FXCollections.observableArrayList(new ArrayList<>());
} else {
List<String> ingredientList = recipe.getIngredients();
this.ingredients = FXCollections.observableArrayList(ingredientList);
} }
private void fetchFromRecipe() { this.ingredientListView.setItems(this.ingredients);
if (recipe == null) { this.refresh();
this.ingredients = FXCollections.observableArrayList(new ArrayList<>()); }
} else {
List<String> ingredientList = recipe.getIngredients();
this.ingredients = FXCollections.observableArrayList(ingredientList);
}
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<List<String>, 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<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;
} }
private void updateRecipeIngredients() { this.ingredients.remove(selectedIndex);
if (this.recipe != null) { this.refresh();
List<String> updatedIngredients = new ArrayList<>(this.ingredients); this.updateCallback.apply(this.ingredients);
this.recipe.setIngredients(updatedIngredients); }
}
}
private void refresh() { @FXML
ingredientListView.refresh(); 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 this.ingredientListView.setEditable(true);
public void initialize(URL location, ResourceBundle resources) { this.ingredientListView.setCellFactory(
this.ingredientListView.setEditable(true); list -> { return new IngredientListCell(); });
this.ingredientListView.setCellFactory(
list -> {
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.refresh();
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();
}
} }