refactor: ingredient list
This commit is contained in:
parent
4c04756920
commit
14b2b2259a
1 changed files with 85 additions and 62 deletions
|
|
@ -5,31 +5,31 @@ 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<String> ingredients;
|
||||
private Function<List<String>, Void> updateCallback;
|
||||
|
||||
@FXML
|
||||
ListView<String> ingredientListView;
|
||||
@FXML ListView<String> ingredientListView;
|
||||
|
||||
@FXML
|
||||
Button addIngredientButton;
|
||||
@FXML
|
||||
Button deleteIngredientButton;
|
||||
@FXML Button addIngredientButton;
|
||||
@FXML Button deleteIngredientButton;
|
||||
|
||||
public void setRecipe(Recipe recipe) {
|
||||
this.recipe = recipe;
|
||||
this.fetchFromRecipe();
|
||||
}
|
||||
|
||||
private void 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 {
|
||||
|
|
@ -41,49 +41,72 @@ public class IngredientListCtrl implements Initializable {
|
|||
this.refresh();
|
||||
}
|
||||
|
||||
private void updateRecipeIngredients() {
|
||||
if (this.recipe != null) {
|
||||
List<String> updatedIngredients = new ArrayList<>(this.ingredients);
|
||||
this.recipe.setIngredients(updatedIngredients);
|
||||
}
|
||||
/**
|
||||
* 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();
|
||||
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;
|
||||
}
|
||||
|
||||
this.ingredients.remove(selectedIndex);
|
||||
this.refresh();
|
||||
this.updateCallback.apply(this.ingredients);
|
||||
}
|
||||
|
||||
@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)
|
||||
|
||||
this.ingredientListView.setEditable(true);
|
||||
this.ingredientListView.setCellFactory(
|
||||
list -> {
|
||||
return new IngredientListCell();
|
||||
});
|
||||
list -> { return new IngredientListCell(); });
|
||||
|
||||
this.ingredientListView.setOnEditCommit(event -> {
|
||||
int index = event.getIndex();
|
||||
String newValue = event.getNewValue();
|
||||
this.ingredients.set(index, newValue);
|
||||
this.updateRecipeIngredients();
|
||||
});
|
||||
|
||||
// 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.ingredientListView.setOnEditCommit(
|
||||
event -> handleIngredientEdit(event));
|
||||
this.addIngredientButton.setOnAction(event -> handleIngredientAdd(event));
|
||||
this.deleteIngredientButton.setOnAction(
|
||||
event -> handleIngredientDelete(event));
|
||||
|
||||
this.refresh();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue