Newer
Older
package no.ntnu.idatt1002.demo.controller;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.DialogPane;
import javafx.scene.control.ListView;
import javafx.event.ActionEvent;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import no.ntnu.idatt1002.demo.data.recipes.FileHandler;
import no.ntnu.idatt1002.demo.data.recipes.FoodItem;
import no.ntnu.idatt1002.demo.data.recipes.IngredientsAtHand;
public class AddIngredientController implements Initializable {
/* ArrayList<String> testIngredients = new ArrayList<>(
Arrays.asList("Tester words", "Another tester word", "tomatoes that are canned", "minced meat", "blueberries")
*//*Arrays.asList(FoodItem.values().toString())*//*
);*/
/*List<String> testIngredients = Arrays.asList("Tester words", "Another tester word", "tomatoes that are canned", "minced meat", "blueberries");*/
String[] testIngredients = {"Tester words", "Another tester word", "tomatoes that are canned", "minced meat", "blueberries"};
private ObservableList<String> ingredients;
private String[] ingredientsList;
private IngredientsAtHand ingredientsAtHand;
@FXML
private Button addBtn;
@FXML
private ListView<String> listView;
@FXML
private TextField searchBar;
@FXML
private Button searchBtn;
void addToFridge(ActionEvent event) throws IOException {
System.out.printf("Add ingredient to fridge");
String item = listView.getSelectionModel().getSelectedItem();
System.out.printf("The item was: %s", item);
ingredientsAtHand.addIngredient(FoodItem.valueOf(item.replace(" ", "_").toUpperCase()));
FileHandler.writeIngredientsAtHand(ingredientsAtHand, "Fridge");
/* FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/view/SuggestRecipesController.fxml"));
loader.load();
// Location is not set.
SuggestRecipesController suggestRecipesController = loader.getController();
suggestRecipesController.addToFridge(item);*/
// Add to existing at hand and write to file. if not null/blank
}
@FXML
void search(ActionEvent event) {
System.out.printf("Searching for ingredient to fridge");
listView.getItems().clear();
listView.getItems().addAll(searchList(searchBar.getText(), ingredientsList));
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
ingredientsAtHand = FileHandler.readIngredientsAtHand("AtHandRegister");
/* ObservableList ingredients = FXCollections.observableArrayList(testIngredients);*/
/* ObservableList<FoodItem> ingredients = FXCollections.observableArrayList(
FoodItem.values());*/
//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);
//private String[] arrayIngredients
ingredients = FXCollections.observableArrayList(stringIngredients);
// Fill list with ingredients
listView.setItems(ingredients);
}
private List<String> searchList(String searchWords, String[] listOfStrings) {
String[] searchWordsArray = searchWords.trim().split(" ");
return Arrays.stream(listOfStrings).filter((in) -> {
return Arrays.stream(searchWordsArray).allMatch((word) ->
in.toLowerCase().contains(word.toLowerCase()));
}).collect(Collectors.toList());
}