Implemented websockets in foodpal ctrl.

This commit is contained in:
Oskar Rasieński 2025-12-17 16:50:30 +01:00
commit 8bceffa67c

View file

@ -13,10 +13,14 @@ import client.utils.DefaultRecipeFactory;
import client.utils.LocaleAware; import client.utils.LocaleAware;
import client.utils.LocaleManager; import client.utils.LocaleManager;
import client.utils.ServerUtils; import client.utils.ServerUtils;
import client.utils.WebSocketUtils;
import commons.Recipe; import commons.Recipe;
import commons.ws.Topics;
import commons.ws.messages.Message;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import javafx.application.Platform;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Alert; import javafx.scene.control.Alert;
@ -32,6 +36,7 @@ import javafx.scene.text.Font;
public class FoodpalApplicationCtrl implements LocaleAware { public class FoodpalApplicationCtrl implements LocaleAware {
private final ServerUtils server; private final ServerUtils server;
private final WebSocketUtils webSocketUtils;
private final LocaleManager localeManager; private final LocaleManager localeManager;
private final IngredientListCtrl ingredientListCtrl; private final IngredientListCtrl ingredientListCtrl;
private final RecipeStepListCtrl stepListCtrl; private final RecipeStepListCtrl stepListCtrl;
@ -81,15 +86,42 @@ public class FoodpalApplicationCtrl implements LocaleAware {
@Inject @Inject
public FoodpalApplicationCtrl( public FoodpalApplicationCtrl(
ServerUtils server, ServerUtils server,
WebSocketUtils webSocketUtils,
LocaleManager localeManager, LocaleManager localeManager,
IngredientListCtrl ingredientListCtrl, IngredientListCtrl ingredientListCtrl,
RecipeStepListCtrl stepListCtrl RecipeStepListCtrl stepListCtrl
) { ) {
this.server = server; this.server = server;
this.webSocketUtils = webSocketUtils;
this.localeManager = localeManager; this.localeManager = localeManager;
this.ingredientListCtrl = ingredientListCtrl; this.ingredientListCtrl = ingredientListCtrl;
this.stepListCtrl = stepListCtrl; this.stepListCtrl = stepListCtrl;
initializeWebSocket();
} }
private void initializeWebSocket() {
webSocketUtils.connect(() -> {
webSocketUtils.subscribe(Topics.RECIPES, (Message _) -> {
Platform.runLater(() -> {
Recipe selectedRecipe = recipeList.getSelectionModel().getSelectedItem();
refresh(); // refresh the left list
if (selectedRecipe == null) {
return;
}
// select last selected recipe if it still exists, first otherwise (done by refresh())
Recipe recipeInList = recipeList.getItems().stream()
.filter(r -> r.getId().equals(selectedRecipe.getId()))
.findFirst()
.orElse(null);
showRecipeDetails(recipeInList);
}); // runLater as it's on another non-FX thread.
});
});
}
@Override @Override
public void initializeComponents() { public void initializeComponents() {
// TODO Reduce code duplication?? // TODO Reduce code duplication??
@ -146,6 +178,7 @@ public class FoodpalApplicationCtrl implements LocaleAware {
refresh(); refresh();
} }
private void showName(String name) { private void showName(String name) {
final int NAME_FONT_SIZE = 20; final int NAME_FONT_SIZE = 20;
editableTitleArea.getChildren().clear(); editableTitleArea.getChildren().clear();
@ -153,6 +186,7 @@ public class FoodpalApplicationCtrl implements LocaleAware {
nameLabel.setFont(new Font("System Bold", NAME_FONT_SIZE)); nameLabel.setFont(new Font("System Bold", NAME_FONT_SIZE));
editableTitleArea.getChildren().add(nameLabel); editableTitleArea.getChildren().add(nameLabel);
} }
@Override @Override
public void updateText() { public void updateText() {
addRecipeButton.setText(getLocaleString("menu.button.add.recipe")); addRecipeButton.setText(getLocaleString("menu.button.add.recipe"));
@ -206,11 +240,13 @@ public class FoodpalApplicationCtrl implements LocaleAware {
} }
detailsScreen.visibleProperty().set(!recipes.isEmpty()); detailsScreen.visibleProperty().set(!recipes.isEmpty());
} }
private void printError(String msg) { private void printError(String msg) {
Alert alert = new Alert(Alert.AlertType.ERROR); Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setContentText(msg); alert.setContentText(msg);
alert.showAndWait(); alert.showAndWait();
} }
/** /**
* Adds a recipe, by providing a default name "Untitled recipe (n)" * Adds a recipe, by providing a default name "Untitled recipe (n)"
* The UX flow is now: * The UX flow is now:
@ -283,7 +319,7 @@ public class FoodpalApplicationCtrl implements LocaleAware {
try { try {
server.updateRecipe(selected); server.updateRecipe(selected);
refresh(); refresh();
recipeList.getSelectionModel().select(selected); //recipeList.getSelectionModel().select(selected);
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
// throw a nice blanket UpdateException // throw a nice blanket UpdateException
throw new UpdateException("Error occurred when updating recipe name!"); throw new UpdateException("Error occurred when updating recipe name!");
@ -294,7 +330,6 @@ public class FoodpalApplicationCtrl implements LocaleAware {
edit.requestFocus(); edit.requestFocus();
} }
// Language buttons // Language buttons
@FXML @FXML
private void switchLocale(ActionEvent event) { private void switchLocale(ActionEvent event) {