refactor(client/ws): divorce WS data handler from the class constructor
This commit is contained in:
parent
5de11428a8
commit
9f892e8fb1
1 changed files with 45 additions and 28 deletions
|
|
@ -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,17 +92,38 @@ 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;
|
||||
return handleCreateRecipeMessage(crm);
|
||||
};
|
||||
case UpdateRecipeMessage _ -> (m) -> {
|
||||
UpdateRecipeMessage urm = (UpdateRecipeMessage) m;
|
||||
return handleUpdateRecipeMessage(urm);
|
||||
};
|
||||
case DeleteRecipeMessage _ -> (m) -> {
|
||||
DeleteRecipeMessage drm = (DeleteRecipeMessage) m;
|
||||
return handleDeleteRecipeMessage(drm);
|
||||
};
|
||||
case FavouriteRecipeMessage _ -> (m) -> {
|
||||
FavouriteRecipeMessage frm = (FavouriteRecipeMessage) m;
|
||||
return handleFavouriteRecipeMessage(frm);
|
||||
};
|
||||
default -> throw new IllegalStateException("Unexpected value: " + msg);
|
||||
});
|
||||
}
|
||||
|
||||
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());
|
||||
};
|
||||
case UpdateRecipeMessage _ -> (m) -> {
|
||||
UpdateRecipeMessage upm = (UpdateRecipeMessage) m;
|
||||
Recipe recipe = upm.getRecipe();
|
||||
System.out.println("recipe: " + recipe.getId() + " of content: " + recipe);
|
||||
}
|
||||
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(
|
||||
|
|
@ -112,25 +134,20 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
|||
),
|
||||
recipe
|
||||
);
|
||||
System.out.println(recipe.getId());
|
||||
dataService.add(recipe.getId());
|
||||
return new ImmutablePair<>(recipe.getId(), recipe);
|
||||
};
|
||||
case DeleteRecipeMessage _ -> (m) -> {
|
||||
DeleteRecipeMessage drm = (DeleteRecipeMessage) m;
|
||||
}
|
||||
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);
|
||||
};
|
||||
case FavouriteRecipeMessage _ -> (m) -> {
|
||||
}
|
||||
// TODO Implementation
|
||||
private Pair<Long, Recipe> handleFavouriteRecipeMessage(FavouriteRecipeMessage frm) {
|
||||
throw new NotImplementedException("TODO:: implement favourited recipe deletion warning");
|
||||
};
|
||||
default -> throw new IllegalStateException("Unexpected value: " + msg);
|
||||
});
|
||||
initializeWebSocket();
|
||||
}
|
||||
|
||||
private void initRecipeList() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue