diff --git a/src/main/java/stud/ntnu/idatt1005/pantrypal/views/CookbookView.java b/src/main/java/stud/ntnu/idatt1005/pantrypal/views/CookbookView.java index 2e95e32871b2d1972cf207ec6c3d6bb79186d346..bc0f7b3a892dbdc1416541515e2118c685e886f6 100644 --- a/src/main/java/stud/ntnu/idatt1005/pantrypal/views/CookbookView.java +++ b/src/main/java/stud/ntnu/idatt1005/pantrypal/views/CookbookView.java @@ -7,6 +7,7 @@ import javafx.geometry.Pos; import javafx.geometry.Rectangle2D; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; +import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import stud.ntnu.idatt1005.pantrypal.controllers.CookBookController; import stud.ntnu.idatt1005.pantrypal.enums.ButtonEnum; @@ -17,6 +18,8 @@ import stud.ntnu.idatt1005.pantrypal.utils.Sizing; import stud.ntnu.idatt1005.pantrypal.views.components.CookbookRecipeComponent; import stud.ntnu.idatt1005.pantrypal.views.components.StyledButton; +import java.util.ArrayList; + /** * The CookbookView class is responsible for creating and managing the view for the * cookbook in the application. @@ -60,12 +63,13 @@ public class CookbookView extends View { } private void addSearchBar() { - TextField searchField = new TextField(); - searchField.setPromptText("Search"); - NodeUtils.addClasses(searchField, "search-field"); - searchField.setMaxWidth(Sizing.getScreenWidth()); - searchField.setMinWidth(Sizing.getScreenWidth()); - NodeUtils.addChildren(pageContainer, searchField); + TextField searchField = createSearchField(); + StyledButton addRecipe = createAddRecipeButton(); + + StackPane searchBar = new StackPane(); + searchBar.getChildren().addAll(searchField, addRecipe); + + NodeUtils.addChildren(pageContainer, searchBar); searchField.textProperty().addListener((observable, oldValue, newValue) -> this.controller.searchRecipes(newValue)); } @@ -76,12 +80,23 @@ public class CookbookView extends View { * It then adds the CookbookRecipeComponents to the rows and the rows to the container. */ public void render() { + VBox recipeContainer = createRecipeContainer(); + if (pageContainer.getChildren().size() < 2) { + NodeUtils.addChildren(pageContainer, recipeContainer); + } else { + pageContainer.getChildren().set(1, recipeContainer); + } + getBorderPane().setCenter(pageContainer); + } + + private VBox createRecipeContainer() { VBox recipeContainer = new VBox(spacing / 2); - //StyledButton addRecipe = addRecipe(); - //recipeContainer.getChildren().add(addRecipe); recipeContainer.setPadding(new Insets(spacing, 0, spacing, 0)); + HBox row = new HBox(spacing); - for (Recipe recipe : controller.getCurrentSearch()) { + ArrayList<Recipe> recipes = new ArrayList<>(controller.getCurrentSearch()); + recipes.sort((a, b) -> Boolean.compare(b.getIsFavorite(), a.getIsFavorite())); + for (Recipe recipe : recipes) { if (row.getChildren().size() >= RECIPES_PER_ROW) { row.setAlignment(Pos.CENTER); recipeContainer.getChildren().add(row); @@ -90,25 +105,31 @@ public class CookbookView extends View { CookbookRecipeComponent recipeComponent = new CookbookRecipeComponent(recipe); recipeComponent.addObserver(controller); row.getChildren().add(recipeComponent.getComponent()); - } + row.setAlignment(Pos.CENTER); recipeContainer.getChildren().add(row); - if (pageContainer.getChildren().size() < 2) { - NodeUtils.addChildren(pageContainer, recipeContainer); - } else { - pageContainer.getChildren().set(1, recipeContainer); - } - getBorderPane().setCenter(pageContainer); + return recipeContainer; } - - private StyledButton addRecipe() { + private StyledButton createAddRecipeButton() { StyledButton button = new StyledButton("Add Recipe"); button.setOnAction(e -> notifyObservers(ButtonEnum.ADD)); + StackPane.setAlignment(button, Pos.CENTER_RIGHT); return button; } + private TextField createSearchField() { + TextField searchField = new TextField(); + searchField.setPromptText("Search"); + NodeUtils.addClasses(searchField, "search-field"); + searchField.setMaxWidth(Sizing.getScreenWidth()); + searchField.setMinWidth(Sizing.getScreenWidth()); + searchField.textProperty().addListener((observable, oldValue, newValue) -> + this.controller.searchRecipes(newValue)); + return searchField; + } + /** * Calculates the spacing between the recipes based on the width of the screen, * the width of the recipes, and the number of recipes per row.