refactor(client/ws): divorce WS data handler from the class constructor

This commit is contained in:
Zhongheng Liu 2026-01-06 21:32:07 +01:00
commit 9f892e8fb1
Signed by: steven
GPG key ID: F69B980899C1C09D

View file

@ -38,6 +38,7 @@ import javafx.scene.control.ListView;
import javafx.scene.control.ToggleButton;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
public class FoodpalApplicationCtrl implements LocaleAware {
private final ServerUtils server;
@ -91,46 +92,62 @@ public class FoodpalApplicationCtrl implements LocaleAware {
this.configService = configService;
this.dataService = recipeDataService;
recipeDataService.setMessageParser((msg) -> switch (msg) {
setupDataService();
initializeWebSocket();
}
private void setupDataService() {
dataService.setMessageParser((msg) -> switch (msg) {
case CreateRecipeMessage _ -> (m) -> {
CreateRecipeMessage crm = (CreateRecipeMessage) m;
this.recipeList.getItems().add(crm.getRecipe());
dataService.add(crm.getRecipe().getId());
return new ImmutablePair<>(crm.getRecipe().getId(), crm.getRecipe());
return handleCreateRecipeMessage(crm);
};
case UpdateRecipeMessage _ -> (m) -> {
UpdateRecipeMessage upm = (UpdateRecipeMessage) m;
Recipe recipe = upm.getRecipe();
System.out.println("recipe: " + recipe.getId() + " of content: " + recipe);
// Discover the first (and should be ONLY) instance of the recipe with the specified ID.
// if not found, throw new InvalidModificationException with a message.
this.recipeList.getItems().set(
this.recipeList.getItems().indexOf(
findRecipeById(recipe.getId())
.orElseThrow(
() -> new InvalidModificationException("Invalid recipe id during update: " + recipe.getId()))
),
recipe
);
System.out.println(recipe.getId());
dataService.add(recipe.getId());
return new ImmutablePair<>(recipe.getId(), recipe);
UpdateRecipeMessage urm = (UpdateRecipeMessage) m;
return handleUpdateRecipeMessage(urm);
};
case DeleteRecipeMessage _ -> (m) -> {
DeleteRecipeMessage drm = (DeleteRecipeMessage) m;
this.recipeList.getItems().remove(findRecipeById(drm.getRecipeId()).orElseThrow(
() -> new InvalidModificationException("Invalid recipe id during delete: " + drm.getRecipeId())
));
dataService.add(drm.getRecipeId());
// TODO Make it an Optional<Recipe> so that we don't need to touch Null?
return new ImmutablePair<>(drm.getRecipeId(), null);
return handleDeleteRecipeMessage(drm);
};
case FavouriteRecipeMessage _ -> (m) -> {
throw new NotImplementedException("TODO:: implement favourited recipe deletion warning");
FavouriteRecipeMessage frm = (FavouriteRecipeMessage) m;
return handleFavouriteRecipeMessage(frm);
};
default -> throw new IllegalStateException("Unexpected value: " + msg);
});
initializeWebSocket();
}
private Pair<Long, Recipe> handleCreateRecipeMessage(CreateRecipeMessage crm) {
this.recipeList.getItems().add(crm.getRecipe());
dataService.add(crm.getRecipe().getId());
return new ImmutablePair<>(crm.getRecipe().getId(), crm.getRecipe());
}
private Pair<Long, Recipe> handleUpdateRecipeMessage(UpdateRecipeMessage urm) {
Recipe recipe = urm.getRecipe();
// Discover the first (and should be ONLY) instance of the recipe with the specified ID.
// if not found, throw new InvalidModificationException with a message.
this.recipeList.getItems().set(
this.recipeList.getItems().indexOf(
findRecipeById(recipe.getId())
.orElseThrow(
() -> new InvalidModificationException("Invalid recipe id during update: " + recipe.getId()))
),
recipe
);
dataService.add(recipe.getId());
return new ImmutablePair<>(recipe.getId(), recipe);
}
private Pair<Long, Recipe> handleDeleteRecipeMessage(DeleteRecipeMessage drm) {
this.recipeList.getItems().remove(findRecipeById(drm.getRecipeId()).orElseThrow(
() -> new InvalidModificationException("Invalid recipe id during delete: " + drm.getRecipeId())
));
dataService.add(drm.getRecipeId());
// TODO Make it an Optional<Recipe> so that we don't need to touch Null?
return new ImmutablePair<>(drm.getRecipeId(), null);
}
// TODO Implementation
private Pair<Long, Recipe> handleFavouriteRecipeMessage(FavouriteRecipeMessage frm) {
throw new NotImplementedException("TODO:: implement favourited recipe deletion warning");
}
private void initRecipeList() {