feat(client/shopping): delegate list view cell rendering and make add element handler

This commit is contained in:
Zhongheng Liu 2026-01-22 19:54:33 +01:00
commit 670de432c5
Signed by: steven
GPG key ID: F69B980899C1C09D

View file

@ -1,5 +1,6 @@
package client.scenes.shopping;
import client.UI;
import client.service.ShoppingListService;
import client.utils.LocaleAware;
import client.utils.LocaleManager;
@ -7,8 +8,12 @@ import com.google.inject.Inject;
import commons.FormalIngredient;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ListCell;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Pair;
import java.util.Optional;
@ -40,33 +45,37 @@ public class ShoppingListCtrl implements LocaleAware {
}
public void initializeComponents() {
this.shoppingListView.setCellFactory(l -> new ListCell<>() {
@Override
protected void updateItem(Pair<FormalIngredient, Optional<String>> item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
this.setText("");
return;
this.shoppingListView.setEditable(true);
this.shoppingListView.setCellFactory(l -> new ShoppingListCell());
this.shoppingListView.getItems().setAll(
this.shopping.getItems()
);
}
String display = item.getKey().toString() +
item.getValue().map(recipe -> {
return " (" + recipe + ")";
}).orElse("");
this.setText(display);
}
});
private void refreshList() {
this.shoppingListView.getItems().setAll(
this.shopping.getItems()
);
}
public void handleAddItem(ActionEvent actionEvent) {
Stage stage = new Stage();
Pair<ShoppingListNewItemPromptCtrl, Parent> root = UI.getFXML().load(ShoppingListNewItemPromptCtrl.class,
"client", "scenes", "shopping", "ShoppingListItemAddModal.fxml");
root.getKey().setNewValueConsumer(fi -> {
this.shopping.putIngredient(fi);
refreshList();
});
stage.setScene(new Scene(root.getValue()));
stage.setTitle("My modal window");
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(
((Node)actionEvent.getSource()).getScene().getWindow() );
stage.show();
}
public void handleRemoveItem(ActionEvent actionEvent) {
var x = this.shoppingListView.getSelectionModel().getSelectedItem();
this.shopping.getItems().remove(x);
refreshList();
}
}