From 81ae52515c6b60a75dfa7d14ccb65e8e986bdf61 Mon Sep 17 00:00:00 2001 From: Rayhan Hassou Date: Thu, 7 Dec 2023 11:17:23 +0100 Subject: [PATCH] bind recipeDetail with new images --- LeftOvers/components/FoodElementText.tsx | 3 +- LeftOvers/components/RecipeElement.tsx | 152 +++++++++++-------- LeftOvers/components/RecipeElementReduce.tsx | 3 +- LeftOvers/screens/IngredientSelection.tsx | 18 +-- LeftOvers/screens/RecipeDetails.tsx | 134 +++++++++++++++- 5 files changed, 235 insertions(+), 75 deletions(-) diff --git a/LeftOvers/components/FoodElementText.tsx b/LeftOvers/components/FoodElementText.tsx index abd64a1..f499023 100644 --- a/LeftOvers/components/FoodElementText.tsx +++ b/LeftOvers/components/FoodElementText.tsx @@ -14,7 +14,8 @@ const componentHeight = 60; const componentWidth = 280; -export default function FoodElementText(props : FoodElementTextProps) { +export default function FoodElementText(props : any) { + const colors = useContext(ColorContext).colors const styles = StyleSheet.create({ diff --git a/LeftOvers/components/RecipeElement.tsx b/LeftOvers/components/RecipeElement.tsx index 929290d..027a99c 100644 --- a/LeftOvers/components/RecipeElement.tsx +++ b/LeftOvers/components/RecipeElement.tsx @@ -46,86 +46,118 @@ export default function RecipeElement(props: RecipeElementProps) { const meatDictionary: string[] = ["beef", "chicken", "turkey", "steak", "rabbit", "duck"] const porkDictionary: string[] = ["pork", "lardon", "bacon", "ham"] const fishDictionary: string[] = ["tuna", "whiting", "mullet", "sardine", "mackerel", "salmon", "monkfish", "conger", "bass", "cod", "hake"] - const vegetablesDictionary: string[] = ["artichoke", "aubergine", "beet", "chard", "broccoli", "carrot", "celery", "cabbage", "cauliflower", "zucchini", "spinach", "fennel", "curly", "bean", "lettuce", "apple", "lettuce", "corn", "onion", "parsnips", "leek", "pepper", "potato", "pumpkin", "radish", "scarole", "tomato"] - const riceDictionary: string[] = ['rice', 'quinoa', 'barley'] - const eggDictionary: string[] = ['egg', 'omelette']; - const soupDictionary: string[] = ['soup']; - const pizzaDictionary: string[] = ['pizza']; - + const vegetablesDictionary: string[] = ["vegan", "vegetarian", "lentil", "artichoke", "aubergine", "beet", "chard", "broccoli", "carrot", "celery", "cabbage", "cauliflower", "zucchini", "spinach", "fennel", "curly", "bean", "lettuce", "apple", "corn", "onion", "parsnips", "leek", "pepper", "potato", "pumpkin", "radish", "scarole", "tomato"] + const riceDictionary: string[] = ["ric", "quinoa"] + const eggDictionary: string[] = ["egg", "omelette"]; + const soupDictionary: string[] = ["soup"]; + const pizzaDictionary: string[] = ["pizza"]; const dessertDictionary: string[] = ['cake', 'cupcake', 'muffin', 'cookie', 'brownie', 'pie', 'tart', 'macaron', 'doughnut', 'eclair', 'pancake', 'waffle', 'crepe', 'pudding', 'gelato', 'sorbet', 'ice cream', 'cheesecake']; const imagesDictionary = { meat: meatDictionary, pork: porkDictionary, fish: fishDictionary, + egg: eggDictionary, vegetables: vegetablesDictionary, rice: riceDictionary, - MeatAndVegetables: [...meatDictionary, ...vegetablesDictionary], - FishAndMeat: [...fishDictionary, ...meatDictionary], - egg: eggDictionary, - dessertDictionary: dessertDictionary, - fruitCake : [...vegetablesDictionary, ...dessertDictionary], - riceEgg : [...riceDictionary, ...eggDictionary], - riceMeat : [...riceDictionary, ...meatDictionary], - riceFish : [...riceDictionary, ...fishDictionary], - riceVegetable : [...riceDictionary, ...vegetablesDictionary], - // pizzaMeat : [...pizzaDictionary, ...meatDictionary], - // pizzaVeggie : [...riceDictionary, ...vegetablesDictionary], - // pizzaFish : [...riceDictionary, ...fishDictionary], pizza : pizzaDictionary, - soupMeat : [...soupDictionary, ...meatDictionary], - soupVeggie : [...soupDictionary, ...vegetablesDictionary], - soupFish : [...soupDictionary, ...fishDictionary], + soup : soupDictionary, + dessert : dessertDictionary, unknown: [], }; + function getCategoryFromList(categories: string[]) { + const categoryMappings = { + MeatAndVegetables: ['meat', 'vegetables'], + FishAndMeat: ['fish', 'meat'], + dessertDictionary: ['dessert'], + fruitCake: ['vegetables', 'dessert'], + riceEgg: ['rice', 'egg'], + riceMeat: ['rice', 'meat'], + riceFish: ['rice', 'fish'], + riceVegetable: ['rice', 'vegetables'], + soupMeat: ['soup', 'meat'], + soupVeggie: ['soup', 'vegetables'], + soupFish: ['soup', 'fish'], + }; + + if(categories.length == 1){ + return categories[0]; + } + + console.log("LA LISTE DES CATEGORY : " + categories) + let bestMatch = { category: '', similarity: 0 }; + + for (const [name, categoriesList] of Object.entries(categoryMappings)) { + const matchingCategories = categories.filter(category => categoriesList.includes(category)); + const similarity = matchingCategories.length; + + if (similarity > bestMatch.similarity) { + bestMatch = { category: name, similarity }; + } + } + + return bestMatch.category; + } + function getImageForRecipe(recipeName: string) { + const categories = []; + console.log("NAAAAAME : " + recipeName) + for (const [category, words] of Object.entries(imagesDictionary)) { const matchedWords = words.filter((word) => recipeName.toLowerCase().includes(word)); + console.log("Matched Word : " + matchedWords) if (matchedWords.length > 0) { - switch (category) { - case 'meat': - return meat_with_vegetable; - case 'pork': - return brochette; - case 'fish': - return fish; - case 'vegetables': - return vegan; - case 'MeatAndVegetables': - return meat_with_vegetable; // Choisis l'image appropriée - case 'FishAndMeat': - return fish_meat; // Choisis l'image appropriée - case 'egg': - return egg; // Choisis l'image appropriée - case 'dessert': - return cake; // Choisis l'image appropriée - case 'fruitCake': - return Carrot_Cake; // Choisis l'image appropriée - case 'riceEgg': - return riceEgg; // Choisis l'image appropriée - case 'riceMeat': - return riceMeat; // Choisis l'image appropriée - case 'riceFish': - return riceFish; // Choisis l'image appropriée - case 'riceVegetable': - return riceVegetable; // Choisis l'image appropriée - case 'pizza': - return pizza; - case 'soupVeggie': - return soupVeggie; - case 'soupMeat': - return soupMeat; - case 'soupFish': - return soupFish; - default: - return inconnu; - } + categories.push(category); + console.log(category) } } - return inconnu; + + console.log("ON ENTRE DANS LA 2EME FONCTION"); + const categoryName = getCategoryFromList(categories); + console.log("CategoryName à la fin : " + categoryName); + + switch (categoryName) { + case 'meat': + return brochette; + case 'pork': + return brochette; + case 'fish': + return fish; + case 'vegetables': + return vegan; + case 'MeatAndVegetables': + return meat_with_vegetable; + case 'FishAndMeat': + return fish_meat; + case 'egg': + return egg; + case 'dessert': + return cake; + case 'fruitCake': + return Carrot_Cake; + case 'riceEgg': + return riceEgg; + case 'riceMeat': + return riceMeat; + case 'riceFish': + return riceFish; + case 'riceVegetable': + return riceVegetable; + case 'pizza': + return pizza; + case 'soupVeggie': + return soupVeggie; + case 'soupMeat': + return soupMeat; + case 'soupFish': + return soupFish; + default: + return inconnu; + } } + const styles = StyleSheet.create({ diff --git a/LeftOvers/components/RecipeElementReduce.tsx b/LeftOvers/components/RecipeElementReduce.tsx index bea510c..9a51467 100644 --- a/LeftOvers/components/RecipeElementReduce.tsx +++ b/LeftOvers/components/RecipeElementReduce.tsx @@ -10,6 +10,7 @@ interface RecipeElementReduceProps { title : string image : string | null duration : string + imageRecipe: any } export default function RecipeElementReduce(props: RecipeElementReduceProps) { @@ -65,7 +66,7 @@ export default function RecipeElementReduce(props: RecipeElementReduceProps) { {props.number} {props.title} - + {props.duration} diff --git a/LeftOvers/screens/IngredientSelection.tsx b/LeftOvers/screens/IngredientSelection.tsx index f8f7461..1bd07a7 100644 --- a/LeftOvers/screens/IngredientSelection.tsx +++ b/LeftOvers/screens/IngredientSelection.tsx @@ -3,7 +3,6 @@ import { View, StyleSheet, Text, Image, Pressable, ActivityIndicator, FlatList, import { SafeAreaProvider } from 'react-native-safe-area-context'; import { Searchbar } from 'react-native-paper'; import { LinearGradient } from 'expo-linear-gradient'; - import FoodElementText from '../components/FoodElementText'; import Ingredient from '../Models/Ingredient'; import IngredientService from '../Services/Ingredients/IngredientsServices'; @@ -14,7 +13,6 @@ import EventEmitter from './EventEmitter'; import plus from '../assets/images/plus.png'; import moins from '../assets/images/minus.png'; -import { ScrollView } from 'react-native-virtualized-view'; export default function IngredientSelection(props) { const colors = useContext(ColorContext).colors @@ -67,33 +65,29 @@ const loadIngredients = async () => { ChangeAvailableSize(true) }, []); - const AvailableItem = ({ value }: { value: Ingredient }) => ( + const AvailableItem = React.memo(({ value }: { value: Ingredient }) => ( <> - SelectIngredient(value)}> - - + SelectIngredient(value)}> - ); + )); - const ChooseItem = ({ value }: { value: Ingredient }) => ( + const ChooseItem = React.memo(({ value }: { value: Ingredient }) => ( <> - RemoveIngredient(value.id)}> - - + RemoveIngredient(value.id)}> - ); + )); const handleGetAvailableIngredient = async () => { try { diff --git a/LeftOvers/screens/RecipeDetails.tsx b/LeftOvers/screens/RecipeDetails.tsx index 39282e5..03ce2c3 100644 --- a/LeftOvers/screens/RecipeDetails.tsx +++ b/LeftOvers/screens/RecipeDetails.tsx @@ -1,7 +1,6 @@ import React, { useEffect, useState, useContext } from 'react'; import { View, StyleSheet, Text, ScrollView, useWindowDimensions} from 'react-native'; import { SafeAreaProvider } from 'react-native-safe-area-context'; - import RecipeElementReduce from '../components/RecipeElementReduce'; import RecipesService from '../Services/Recipes/RecipesServices'; import Recipes from '../Models/Recipes'; @@ -9,6 +8,24 @@ import { LinearGradient } from 'expo-linear-gradient'; import ListWithoutSelect from '../components/ListWithoutSelect'; import ColorContext from '../theme/ColorContext'; import Ingredient from '../Models/Ingredient'; +import cake from '../assets/images/cake.png'; +import Carrot_Cake from '../assets/images/Carrot_Cake.png'; +import inconnu from '../assets/images/inconnu.png'; +import fish from '../assets/images/fish.png'; +import fish_meat from '../assets/images/fish_meat.png'; +import vegan from '../assets/images/vegan.png'; +import egg from '../assets/images/egg.png'; +import pizza from '../assets/images/pizza.png'; +import soupVeggie from '../assets/images/soupVeggie.png'; +import soupFish from '../assets/images/soupfish.png' +import soupMeat from '../assets/images/soupMeat.png' +import meat_with_vegetable from '../assets/images/meat_with_vegetables.png'; +import riceVegetable from '../assets/images/rice_vegetables.png'; +import riceMeat from '../assets/images/Meat_and_Rice.png'; +import riceFish from '../assets/images/rice_fish.png'; +import riceEgg from '../assets/images/riceEgg.png'; +import brochette from '../assets/images/brochette.png'; + export default function RecipeDetails({ route }) { @@ -23,6 +40,120 @@ export default function RecipeDetails({ route }) { const { recipeId } = route.params; + const meatDictionary: string[] = ["beef", "chicken", "turkey", "steak", "rabbit", "duck"] + const porkDictionary: string[] = ["pork", "lardon", "bacon", "ham"] + const fishDictionary: string[] = ["tuna", "whiting", "mullet", "sardine", "mackerel", "salmon", "monkfish", "conger", "bass", "cod", "hake"] + const vegetablesDictionary: string[] = ["vegan", "vegetarian", "lentil", "artichoke", "aubergine", "beet", "chard", "broccoli", "carrot", "celery", "cabbage", "cauliflower", "zucchini", "spinach", "fennel", "curly", "bean", "lettuce", "apple", "corn", "onion", "parsnips", "leek", "pepper", "potato", "pumpkin", "radish", "scarole", "tomato"] + const riceDictionary: string[] = ["ric", "quinoa"] + const eggDictionary: string[] = ["egg", "omelette"]; + const soupDictionary: string[] = ["soup"]; + const pizzaDictionary: string[] = ["pizza"]; + const dessertDictionary: string[] = ['cake', 'cupcake', 'muffin', 'cookie', 'brownie', 'pie', 'tart', 'macaron', 'doughnut', 'eclair', 'pancake', 'waffle', 'crepe', 'pudding', 'gelato', 'sorbet', 'ice cream', 'cheesecake']; + + const imagesDictionary = { + meat: meatDictionary, + pork: porkDictionary, + fish: fishDictionary, + egg: eggDictionary, + vegetables: vegetablesDictionary, + rice: riceDictionary, + pizza : pizzaDictionary, + soup : soupDictionary, + dessert : dessertDictionary, + unknown: [], + }; + + function getCategoryFromList(categories: string[]) { + const categoryMappings = { + MeatAndVegetables: ['meat', 'vegetables'], + FishAndMeat: ['fish', 'meat'], + dessertDictionary: ['dessert'], + fruitCake: ['vegetables', 'dessert'], + riceEgg: ['rice', 'egg'], + riceMeat: ['rice', 'meat'], + riceFish: ['rice', 'fish'], + riceVegetable: ['rice', 'vegetables'], + soupMeat: ['soup', 'meat'], + soupVeggie: ['soup', 'vegetables'], + soupFish: ['soup', 'fish'], + }; + + if(categories.length == 1){ + return categories[0]; + } + + console.log("LA LISTE DES CATEGORY : " + categories) + let bestMatch = { category: '', similarity: 0 }; + + for (const [name, categoriesList] of Object.entries(categoryMappings)) { + const matchingCategories = categories.filter(category => categoriesList.includes(category)); + const similarity = matchingCategories.length; + + if (similarity > bestMatch.similarity) { + bestMatch = { category: name, similarity }; + } + } + + return bestMatch.category; + } + + function getImageForRecipe(recipeName: string) { + const categories = []; + console.log("NAAAAAME : " + recipeName) + + for (const [category, words] of Object.entries(imagesDictionary)) { + const matchedWords = words.filter((word) => recipeName.toLowerCase().includes(word)); + console.log("Matched Word : " + matchedWords) + if (matchedWords.length > 0) { + categories.push(category); + console.log(category) + } + } + + console.log("ON ENTRE DANS LA 2EME FONCTION"); + const categoryName = getCategoryFromList(categories); + console.log("CategoryName à la fin : " + categoryName); + + switch (categoryName) { + case 'meat': + return brochette; + case 'pork': + return brochette; + case 'fish': + return fish; + case 'vegetables': + return vegan; + case 'MeatAndVegetables': + return meat_with_vegetable; + case 'FishAndMeat': + return fish_meat; + case 'egg': + return egg; + case 'dessert': + return cake; + case 'fruitCake': + return Carrot_Cake; + case 'riceEgg': + return riceEgg; + case 'riceMeat': + return riceMeat; + case 'riceFish': + return riceFish; + case 'riceVegetable': + return riceVegetable; + case 'pizza': + return pizza; + case 'soupVeggie': + return soupVeggie; + case 'soupMeat': + return soupMeat; + case 'soupFish': + return soupFish; + default: + return inconnu; + } + } + const loadRecipe = async () => { try { const recipe = await recipesService.getRecipeById(recipeId); @@ -98,6 +229,7 @@ export default function RecipeDetails({ route }) {