Skip to content
Snippets Groups Projects
MainPageController.java 3.3 KiB
Newer Older
package mi.hdm.controllers;

import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;
import mi.hdm.components.CategoryCheckBox;
import mi.hdm.components.RecipeVbox;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class MainPageController extends BaseController {
    private final static Logger log = LogManager.getLogger(MainPageController.class);

    private final RecipeManager recipeManager;
    private final IngredientManager ingredientManager;
    private final CategoryManager categoryManager;
    private final List<CategoryCheckBox> categoryCheckboxesInGui = new ArrayList<>();
    private final List<RecipeVbox> currentRecipesInGUI = new ArrayList<>(); //keeps track of all Nodes in the GUI that represent a recipe
    @FXML
    private AnchorPane parent;
    @FXML
    private TilePane recipeTilePane;
    @FXML
    private HBox categories;
    public MainPageController(RecipeManager recipeManager, IngredientManager ingredientManager, CategoryManager categoryManager) {
        this.ingredientManager = ingredientManager;
        this.categoryManager = categoryManager;
    public void initialize() {
        loadHeader(parent);
        mapCategories();
        log.debug("Added {} recipes to GUI", recipeManager.getRecipes().size());
        log.info("User is trying to add recipe!");
    private void mapRecipes(List<Recipe> recipes) {
        recipeTilePane.getChildren().removeAll(currentRecipesInGUI);
        currentRecipesInGUI.clear(); //remove all recipes...
            RecipeVbox recipeVbox = new RecipeVbox(recipe, categoryManager);
            recipeVbox.setOnMouseClicked(mouseEvent -> changeScene(View.RECIPE_VIEW, recipeVbox.getAssociatedRecipe())); //TODO: this needs to display the recipe view
        recipeTilePane.getChildren().addAll(currentRecipesInGUI); //... and add the new ones

    private void mapCategories() {
        for (final Category category : categoryManager.getAllCategories()) {
            CategoryCheckBox categoryCheckBox = new CategoryCheckBox(category);
            categoryCheckBox.setOnAction(event -> updateRecipes());

            categoryCheckboxesInGui.add(categoryCheckBox); //map the category to its checkbox UI element
            categories.getChildren().add(categoryCheckBox);
        }
    }

    private void updateRecipes() {
        mapRecipes(fetchSelectedRecipes());
    }

    private List<Recipe> fetchSelectedRecipes() {
        List<Category> currentlySelectedCategories =
                categoryCheckboxesInGui.stream()
                        .filter(CheckBox::isSelected)
                        .map(CategoryCheckBox::getAssociatedCategory)
                        .toList();
        RecipeSearch recipeSearch = new RecipeSearch(recipeManager, ingredientManager);
        return recipeSearch.searchByCategory(recipeManager.getRecipes(), currentlySelectedCategories);