From 02920fc657524b8e2b773417381cd299962ba388 Mon Sep 17 00:00:00 2001
From: HSoreide <sofie.scisly@gmail.com>
Date: Wed, 19 Apr 2023 15:43:32 +0200
Subject: [PATCH] Factor out and improve hover effect of suggested recipe tiles

---
 .../controller/AddIngredientController.java   | 50 +++++++++++--------
 .../demo/controller/RecipeTileController.java |  2 -
 .../controller/SuggestRecipesController.java  | 44 ++++++++++------
 src/main/resources/recipes/Fridge.register    | 36 ++++++++-----
 src/main/resources/recipes/Recipes.register   |  2 +-
 src/main/resources/style.css                  | 14 +++---
 src/main/resources/view/AddIngredient.fxml    | 19 ++++---
 src/main/resources/view/SuggestRecipes.fxml   |  7 ++-
 8 files changed, 107 insertions(+), 67 deletions(-)

diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java
index 28e95e9f..5784f342 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/controller/AddIngredientController.java
@@ -1,10 +1,12 @@
 package no.ntnu.idatt1002.demo.controller;
 
+import javafx.animation.FadeTransition;
 import javafx.application.Platform;
 import javafx.collections.FXCollections;
 import javafx.collections.ObservableList;
 import javafx.fxml.FXML;
 import javafx.fxml.Initializable;
+import javafx.scene.control.Label;
 import javafx.scene.control.ListView;
 import javafx.event.ActionEvent;
 import java.io.IOException;
@@ -15,6 +17,7 @@ import java.util.ResourceBundle;
 import java.util.stream.Collectors;
 import javafx.scene.control.Button;
 import javafx.scene.control.TextField;
+import javafx.util.Duration;
 import no.ntnu.idatt1002.demo.data.recipes.FileHandler;
 import no.ntnu.idatt1002.demo.data.recipes.FoodItem;
 import no.ntnu.idatt1002.demo.data.recipes.IngredientsAtHand;
@@ -23,7 +26,6 @@ public class AddIngredientController implements Initializable {
 
     private ObservableList<String> ingredients;
     private String[] ingredientsList;
-    private IngredientsAtHand ingredientsAtHand;
 
     @FXML
     private Button addBtn;
@@ -37,21 +39,37 @@ public class AddIngredientController implements Initializable {
     @FXML
     private Button searchBtn;
 
+    @FXML
+    private Label status;
+
+    private String statusText = "Added: ";
 
     @FXML
     void addToFridge(ActionEvent event) throws IOException {
-        String item = listView.getSelectionModel().getSelectedItem();
-        if(item != null) {
-            ingredientsAtHand.addIngredient(FoodItem.valueOf(item.replace(" ", "_").toUpperCase()));
+        IngredientsAtHand ingredientsAtHand = FileHandler.readIngredientsAtHand("Fridge");
+        FoodItem item = FoodItem.valueOf(listView.getSelectionModel().getSelectedItem().replace(" ", "_").toUpperCase());
+
+        assert ingredientsAtHand != null;
+        if(!ingredientsAtHand.atHand(item)) {
+            ingredientsAtHand.addIngredient(item);
             FileHandler.writeIngredientsAtHand(ingredientsAtHand, "Fridge");
-        }
 
+
+            if(status.isVisible() && status.getText().isBlank()) {
+                statusText += String.format("%s", item.label);
+            } else if (status.isVisible()){
+                statusText += String.format(", %s", item.label);
+            }
+            status.setText(statusText);  // Only if not already in list!!
+
+        }
     }
 
     @FXML
-    void search(ActionEvent event) {
+    void search() {
        listView.getItems().clear();
-       listView.getItems().addAll(searchList(searchBar.getText(), ingredientsList));
+       listView.getItems().addAll(searchList(searchBar.getText(),
+               Arrays.stream(FoodItem.values()).toList().stream().map(value -> value.label).toArray(String[]::new)));   // String[]
 
     }
 
@@ -63,23 +81,15 @@ public class AddIngredientController implements Initializable {
                     in.toLowerCase().contains(word.toLowerCase()));
         }).collect(Collectors.toList());
     }
-    @Override
-    public void initialize(URL url, ResourceBundle resourceBundle) {
 
-        ingredientsAtHand = FileHandler.readIngredientsAtHand("Fridge");
-
-        //TODO: Move into initializer? - Tidy up!
-        List<String> stringIngredients = Arrays.stream(FoodItem.values()).toList().stream().map(value -> value.label).toList();
-        int noLengthOfList = stringIngredients.size();
-        ingredientsList = stringIngredients.stream().toArray(String[] ::new);
-        ingredients = FXCollections.observableArrayList(stringIngredients);
-        // Fill list with ingredients
-        listView.setItems(ingredients);
 
+    @Override
+    public void initialize(URL url, ResourceBundle resourceBundle) {
+        listView.setItems(FXCollections.observableArrayList(Arrays.stream(FoodItem.values()).map(value -> value.label).toList()));
         Platform.runLater(() -> searchBar.requestFocus());
-
+        status.setWrapText(true);
     }
 
-
+//TODO: Add label with status message that fades in 5 sec.
 
 }
diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java
index 16373eea..0e832a02 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/controller/RecipeTileController.java
@@ -8,7 +8,6 @@ import javafx.scene.Node;
 import javafx.scene.Parent;
 import javafx.scene.Scene;
 import javafx.scene.control.*;
-import javafx.scene.input.MouseEvent;
 import javafx.scene.layout.VBox;
 import javafx.stage.Stage;
 import no.ntnu.idatt1002.demo.data.recipes.FileHandler;
@@ -17,7 +16,6 @@ import no.ntnu.idatt1002.demo.data.recipes.RecipeRegister;
 
 import java.io.IOException;
 import java.net.URL;
-import java.util.Optional;
 import java.util.ResourceBundle;
 
 public class RecipeTileController implements Initializable {
diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java
index 4dd67542..49ef7c7f 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/controller/SuggestRecipesController.java
@@ -73,7 +73,7 @@ public class SuggestRecipesController implements Initializable {
 
         Optional<ButtonType> clickedButton = dialog.showAndWait();
 
-        if (clickedButton.isPresent() && clickedButton.get() == ButtonType.APPLY) {
+        if (clickedButton.isPresent() && clickedButton.get() == ButtonType.CLOSE) {
         // Refresh ingredientsAtHand.
            readIngredientsAtHand();
             setRecipeTiles();
@@ -84,9 +84,12 @@ public class SuggestRecipesController implements Initializable {
     private void removeFromFridge(ActionEvent event) throws IOException {
         String toRemove = fridgeList.getSelectionModel().getSelectedItem();
         //TODO: If anything selected!
-        ingredientsAtHand.removeIngredient(FoodItem.valueOf(toRemove.replace(" ", "_").toUpperCase()));
-        storeIngredientsAtHand();
-        setRecipeTiles();
+        if(toRemove != null) {
+            ingredientsAtHand.removeIngredient(FoodItem.valueOf(toRemove.replace(" ", "_").toUpperCase()));
+            storeIngredientsAtHand();
+            setRecipeTiles();
+        }
+
     }
 
 
@@ -120,18 +123,7 @@ public class SuggestRecipesController implements Initializable {
                     currentRecipeTiles.set(counter, vBox);
                 }
 
-                vBox.setOnMouseEntered(event -> {
-                    if(r.getMissingIngredients()==0) {
-                        missingList.setText("");
-                    } else {
-                        missingList.setText("Missing: " + String.join(", ", r.getMissingList()));
-                    }
-
-                });
-
-                vBox.setOnMouseExited(event -> {
-                    missingList.setText("");
-                });
+                setHoverEffect(vBox, r);
 
                 recipeGrid.add(vBox, i, j);
 
@@ -145,6 +137,25 @@ public class SuggestRecipesController implements Initializable {
     }
 
 
+    private void setHoverEffect(VBox vBox, Recipe recipe) {
+        vBox.setOnMouseEntered(event -> {
+            if(recipe.getMissingIngredients()==0) {
+                missingList.setText("");
+                missingList.setVisible(false);
+            } else {
+                missingList.setText("Missing: " + String.join(", ", recipe.getMissingList()));
+                missingList.setVisible(true);
+            }
+
+        });
+
+        vBox.setOnMouseExited(event -> {
+            missingList.setText("");
+            missingList.setVisible(false);
+        });
+    }
+
+
     @FXML
     private void switchScene(ActionEvent event) throws IOException {
         FXMLLoader loader = new FXMLLoader();
@@ -183,6 +194,7 @@ public class SuggestRecipesController implements Initializable {
         readIngredientsAtHand();
          recipeRegister = FileHandler.readRecipeRegister("Recipes");
          recipes = FXCollections.observableArrayList(recipeRegister.getRecipes());
+         missingList.setVisible(false);
 
          // Get the number from FX-grid available?
          setRecipeTiles();
diff --git a/src/main/resources/recipes/Fridge.register b/src/main/resources/recipes/Fridge.register
index fb896692..9d8904bd 100644
--- a/src/main/resources/recipes/Fridge.register
+++ b/src/main/resources/recipes/Fridge.register
@@ -1,21 +1,33 @@
-WHEAT_FLOUR
-EGG
 VINEGAR
-SPAGHETTI
-PASTA
-CREAM
 TOMATO_PASTE
 CHICKPEAS
-SPRING_ROLL
 YELLOW_CHEESE
-POTATO
 BELL_PEPPER
 DRY_THYME
 DRY_BASIL
-PARMESAN
-OLIVE_OIL
 BUTTER
-GARLIC_CLOVE
-HAM
 BROCCOLI
-OIL
+FRESH_BASIL
+MINCED_MEAT
+MILK
+TOMATO
+ORANGE
+ONION
+POTATO
+WHEAT_FLOUR
+SALSA_SAUCE
+SALAD
+SPINACH
+CUCUMBER
+SPRING_ROLL
+SPAGHETTI
+PASTA
+CREAM
+HONEY
+CHILLI
+EGG
+OLIVE_OIL
+HAM
+SNAP_PEA
+MACARONI
+SALMON
diff --git a/src/main/resources/recipes/Recipes.register b/src/main/resources/recipes/Recipes.register
index 70dd0e99..c7ddcb81 100644
--- a/src/main/resources/recipes/Recipes.register
+++ b/src/main/resources/recipes/Recipes.register
@@ -186,7 +186,7 @@ https://www.matprat.no/oppskrifter/familien/skinkepai/
 
 
 
-# French Lamb Stew
+# French Lam Stew
 
 - LAM | 1 | KG
 - BUTTER | 3 | TBS
diff --git a/src/main/resources/style.css b/src/main/resources/style.css
index a669648b..87a78c9e 100644
--- a/src/main/resources/style.css
+++ b/src/main/resources/style.css
@@ -67,17 +67,15 @@
     -fx-scale-y: 1.05;
     -fx-scale-z: 1.05;
 }
-/*
-.instructions-pane {
-    -fx-background-color:transparent;
-}*/
-
-/*.instructions-pane .viewport {
-    -fx-background-color: transparent;
-}*/
 
 .recipe-instructions {
     -fx-font-size: 16;
     -fx-font-style: italic;
     -fx-spacing: 1.5;
+}
+
+.information-label {
+    -fx-background-color: rgba(255, 255, 255, 0.65);
+    -fx-text-fill: black;
+    -fx-border-radius: 20;
 }
\ No newline at end of file
diff --git a/src/main/resources/view/AddIngredient.fxml b/src/main/resources/view/AddIngredient.fxml
index a000e3bf..675a26f9 100644
--- a/src/main/resources/view/AddIngredient.fxml
+++ b/src/main/resources/view/AddIngredient.fxml
@@ -5,14 +5,14 @@
 <?import javafx.scene.layout.*?>
 <?import javafx.scene.text.*?>
 
-<DialogPane id="dialog-pane" prefHeight="524.0" prefWidth="614.0" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.AddIngredientController">
+<DialogPane id="dialog-pane" expanded="true" prefHeight="524.0" prefWidth="614.0" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idatt1002.demo.controller.AddIngredientController">
    <content>
       <AnchorPane prefHeight="400.0" prefWidth="600.0">
          <padding>
             <Insets top="20.0" />
          </padding>
          <children>
-            <VBox layoutX="14.0" prefHeight="348.0" prefWidth="600.0">
+            <VBox prefHeight="406.0" prefWidth="614.0">
                <children>
                   <HBox prefHeight="100.0" prefWidth="200.0" spacing="20.0">
                      <children>
@@ -37,7 +37,7 @@
                         <Insets left="25.0" right="25.0" />
                      </VBox.margin>
                   </HBox>
-                  <ListView id="list-cell" fx:id="listView" prefHeight="311.0" prefWidth="590.0" stylesheets="@../style.css">
+                  <ListView id="list-cell" fx:id="listView" prefHeight="330.0" prefWidth="564.0" stylesheets="@../style.css">
                      <padding>
                         <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
                      </padding>
@@ -45,7 +45,7 @@
                         <Insets left="25.0" right="25.0" />
                      </VBox.margin>
                   </ListView>
-                  <Pane prefHeight="81.0" prefWidth="600.0">
+                  <Pane prefHeight="100.0" prefWidth="614.0">
                      <children>
                         <Button id="button-style" fx:id="addBtn" layoutX="275.0" layoutY="7.0" mnemonicParsing="false" onAction="#addToFridge" styleClass="button-style" stylesheets="@../style.css" text="ADD">
                            <font>
@@ -54,6 +54,14 @@
                         </Button>
                      </children>
                   </Pane>
+                  <Pane prefHeight="100.0" prefWidth="614.0">
+                     <children>
+                        <Label fx:id="status" layoutX="28.0" layoutY="6.0" prefHeight="44.0" prefWidth="558.0" textAlignment="CENTER" wrapText="true">
+                           <font>
+                              <Font size="14.0" />
+                           </font></Label>
+                     </children>
+                  </Pane>
                </children>
             </VBox>
          </children></AnchorPane>
@@ -69,7 +77,6 @@
       </Label>
    </header>
    <buttonTypes>
-      <ButtonType fx:constant="APPLY" />
-      <ButtonType fx:constant="CANCEL" />
+      <ButtonType fx:constant="CLOSE" />
    </buttonTypes>
 </DialogPane>
diff --git a/src/main/resources/view/SuggestRecipes.fxml b/src/main/resources/view/SuggestRecipes.fxml
index da437245..cb8ab943 100644
--- a/src/main/resources/view/SuggestRecipes.fxml
+++ b/src/main/resources/view/SuggestRecipes.fxml
@@ -57,10 +57,13 @@
                <children>
                   <Pane layoutX="72.0" prefHeight="45.0" prefWidth="891.0">
                      <children>
-                        <Label fx:id="missingList" prefHeight="43.0" prefWidth="847.0" text=" ">
+                        <Label fx:id="missingList" styleClass="information-label" stylesheets="@../style.css">
                            <font>
                               <Font name="System Bold" size="14.0" />
-                           </font></Label>
+                           </font>
+                           <padding>
+                              <Insets left="20.0" right="20.0" />
+                           </padding></Label>
                      </children>
                   </Pane>
                </children></Pane>
-- 
GitLab