diff --git a/LeftOvers/Models/Profile.tsx b/LeftOvers/Models/Profile.tsx index 94940b0..20bd71f 100644 --- a/LeftOvers/Models/Profile.tsx +++ b/LeftOvers/Models/Profile.tsx @@ -1,30 +1,41 @@ export default class Profile { - private _name: string; - private _avatar: string; - private _allergy: string[]; - private _diets: string[]; + public name: string; + public avatar: string; + public allergies: string[]; + public diets: string[]; + public isActive: string; + public isWaiting: string - constructor( name: string, avatar: string, allergy: string[], diets: string[]) { - this._name = name; - this._avatar = avatar; - this._allergy = allergy; - this._diets = diets; + constructor( name: string, avatar: string, allergies: string[], diets: string[], isActive: string, isWaiting: string) { + this.name = name; + this.avatar = avatar; + this.diets = diets; + this.allergies = allergies; + this.isActive = isActive; + this.isWaiting = isWaiting } - get name(): string { - return this._name; - } + // get name(): string { + // return this._name; + // } + // get avatar(): string{ + // return this._avatar; + // } - get avatar(): string{ - return this._avatar; - } + // get allergies(): string[]{ + // return this._allergies; + // } - get allergy(): string[]{ - return this._allergy; - } + // get diets(): string[]{ + // return this._diets; + // } - get diets(): string[]{ - return this._diets; - } -} \ No newline at end of file + // get isActive(): string{ + // return this._isActive; + // } + + // get isWaiting(): string{ + // return this._isWaiting; + // } + } \ No newline at end of file diff --git a/LeftOvers/Services/Ingredients/IIngredientService.tsx b/LeftOvers/Services/Ingredients/IIngredientService.tsx index 926f9be..687386c 100644 --- a/LeftOvers/Services/Ingredients/IIngredientService.tsx +++ b/LeftOvers/Services/Ingredients/IIngredientService.tsx @@ -5,4 +5,7 @@ export default interface IIngredientService { getIngredientById(id: number): Promise; getIngredientByLetter(id: string): Promise; getfilteredIngredient(prompt: string): Promise; + getAvailableIngredient(): Promise, + addIngredient(newIngredient: Ingredient): Promise, + delIngredient(idIngredient: number): Promise } \ No newline at end of file diff --git a/LeftOvers/Services/Ingredients/IngredientsServices.tsx b/LeftOvers/Services/Ingredients/IngredientsServices.tsx index d50cc89..eb2f5e4 100644 --- a/LeftOvers/Services/Ingredients/IngredientsServices.tsx +++ b/LeftOvers/Services/Ingredients/IngredientsServices.tsx @@ -1,6 +1,8 @@ +import AsyncStorage from "@react-native-async-storage/async-storage"; import Ingredient from "../../Models/Ingredient"; import IIngredientService from "./IIngredientService"; import axios from 'axios'; +import eventEmitter from "../../screens/EventEmitter"; export default class IngredientService implements IIngredientService { private readonly API_URL = "http://leftovers.alwaysdata.net/ingredients"; @@ -41,4 +43,36 @@ export default class IngredientService implements IIngredientService { throw new Error('Erreur lors de la récupération des ingrédients : ' + error.message); } } + + async getAvailableIngredient(): Promise { + const results = await AsyncStorage.getItem('ingredient') + const availableIngredient = JSON.parse(results) + if(availableIngredient.length == 0){ + availableIngredient.push(new Ingredient(-1, "None")) + } + console.log("AvailableIngredient:", availableIngredient) + return availableIngredient; + } + + async addIngredient(newIngredient: Ingredient): Promise { + let selectedIngredients = await this.getAvailableIngredient() + const exists = selectedIngredients.find((ingredient) => ingredient.id === newIngredient.id); + if (!exists) { + let existingAvailableIngredient = await AsyncStorage.getItem('ingredient'); + existingAvailableIngredient = existingAvailableIngredient ? JSON.parse(existingAvailableIngredient) : []; + const updatedAvailableIngredient = [...existingAvailableIngredient, newIngredient]; + await AsyncStorage.setItem('ingredient', JSON.stringify(updatedAvailableIngredient)); + eventEmitter.emit('ingredientAdded'); + return true + } + return false + } + + async delIngredient(idIngredient: number): Promise { + let selectedIngredients = await this.getAvailableIngredient() + const updatedIngredients = selectedIngredients.filter((ingredient) => ingredient.id !== idIngredient); + await AsyncStorage.setItem('ingredient', JSON.stringify(updatedIngredients)); + eventEmitter.emit('ingredientDeleted'); + return true + } } diff --git a/LeftOvers/Services/Profiles/IProfileService.ts b/LeftOvers/Services/Profiles/IProfileService.ts index 425a961..3e628d4 100644 --- a/LeftOvers/Services/Profiles/IProfileService.ts +++ b/LeftOvers/Services/Profiles/IProfileService.ts @@ -1,7 +1,7 @@ -import Profil from "../../Models/Profil"; +import Profile from "../../Models/Profile"; export default interface IProfileService { - getProfiles(): Promise, - addProfile(new_profile: Profil): Promise, - delProfile(profile_name_to_del: string): Promise + getProfiles(): Promise, + addProfile(newProfile: Profile): Promise, + delProfile(index: number): Promise } \ No newline at end of file diff --git a/LeftOvers/Services/Profiles/ProfileService.ts b/LeftOvers/Services/Profiles/ProfileService.ts index f2f3078..8704434 100644 --- a/LeftOvers/Services/Profiles/ProfileService.ts +++ b/LeftOvers/Services/Profiles/ProfileService.ts @@ -1,44 +1,32 @@ import Profile from "../../Models/Profile"; import IProfileService from "./IProfileService"; import AsyncStorage from "@react-native-async-storage/async-storage"; +import eventEmitter from "../../screens/EventEmitter"; export default class ProfileService implements IProfileService { async getProfiles(): Promise { - const results = await AsyncStorage.getItem('profiles'); - if (results == null) { - return [] - } - const tmp = JSON.parse(results) - let existingProfiles: Profile[] = [] - for (let item of tmp) { - existingProfiles.push(new Profile(item._name, item._avatar, item._allergy, item._diets)) + const results = await AsyncStorage.getItem('profiles') + const existingProfiles = JSON.parse(results) + if(existingProfiles.length == 0){ + existingProfiles.push(new Profile("None", "logo.png", [], [], "none", "none")) } return existingProfiles; } - async addProfile(new_profile : Profile): Promise { - const existingProfiles = await this.getProfiles() - for (let current_profile of existingProfiles) { - if (current_profile.name == new_profile.name) { - console.log("Tried to create a profil already existing !") - return false - } - } - await AsyncStorage.setItem('profiles', JSON.stringify([...existingProfiles, new_profile])) + async addProfile(newProfile : Profile): Promise { + let existingProfiles = await AsyncStorage.getItem('profiles') + existingProfiles = existingProfiles ? JSON.parse(existingProfiles) : []; + const updatedProfiles = [...existingProfiles, newProfile]; + await AsyncStorage.setItem('profiles', JSON.stringify(updatedProfiles)); + eventEmitter.emit("profileAdded") return true } - async delProfile(profile_name_to_del: string): Promise { - const existing_profiles = await this.getProfiles() - let key: number = -1 - for (let current_profile of existing_profiles) { - if (current_profile.name == profile_name_to_del) { - let updated_profile = existing_profiles.splice(key, 1) - await AsyncStorage.setItem('profiles', JSON.stringify(updated_profile)) - return true - } - key ++ - } - return false + async delProfile(index: number): Promise { + const existingProfiles = await this.getProfiles() + const updatedProfiles = existingProfiles.filter((profile, i) => i !== index); + await AsyncStorage.setItem('profiles', JSON.stringify(updatedProfiles)); + eventEmitter.emit('profileDeleted'); + return true } } \ No newline at end of file diff --git a/LeftOvers/components/ListWithoutSelect.tsx b/LeftOvers/components/ListWithoutSelect.tsx index a095f3c..1fa93e8 100644 --- a/LeftOvers/components/ListWithoutSelect.tsx +++ b/LeftOvers/components/ListWithoutSelect.tsx @@ -12,6 +12,11 @@ export default function ListWithoutSelect(props: ListProps) { const [selected, setSelected] = React.useState([]); const colors = useContext(ColorContext).colors; + let listContent = [] + props.content.forEach((val) => { + listContent.push({value: val, disabled: true}) + }) + const styles = StyleSheet.create({ titleBar: { flexDirection: "row", @@ -80,7 +85,7 @@ export default function ListWithoutSelect(props: ListProps) { return ( setSelected(val)} - data={props.content} + data={listContent} save="value" search={false} arrowicon={} @@ -97,5 +102,4 @@ export default function ListWithoutSelect(props: ListProps) { placeholder={props.title} label={props.title}/> ); -} - +} \ No newline at end of file diff --git a/LeftOvers/navigation/BottomBar.tsx b/LeftOvers/navigation/BottomBar.tsx index a65c78d..ee552e3 100644 --- a/LeftOvers/navigation/BottomBar.tsx +++ b/LeftOvers/navigation/BottomBar.tsx @@ -40,7 +40,7 @@ export default function BottomBar({ state, descriptors, navigation }) { bottom: 0, right: 0, left: 0, - height: "8%", + height: 60, backgroundColor: theme === 'dark' ? "#3F3C42" : "transparent" }, BottomBarBlurContainer: { diff --git a/LeftOvers/navigation/ProfileStackScreen.tsx b/LeftOvers/navigation/ProfileStackScreen.tsx index d1ccaff..5f55471 100644 --- a/LeftOvers/navigation/ProfileStackScreen.tsx +++ b/LeftOvers/navigation/ProfileStackScreen.tsx @@ -37,7 +37,7 @@ export default function ProfilesStackScreen({ navigation }) { const _handleSearch = () => { console.log('Searching'); } - const _handleHeaderAdd = () => navigation.navigate('ProfileCreation', { name: String }); + const _handleHeaderAdd = () => navigation.navigate('ProfileCreation'); return ( diff --git a/LeftOvers/screens/CreateProfile.tsx b/LeftOvers/screens/CreateProfile.tsx index e69a2a2..2303a20 100644 --- a/LeftOvers/screens/CreateProfile.tsx +++ b/LeftOvers/screens/CreateProfile.tsx @@ -6,9 +6,8 @@ import ValidateButton from '../components/ValidateButton'; import ColorContext from '../theme/ColorContext'; import ListWithoutSelect from '../components/ListWithoutSelect'; import ListSelect from '../components/ListSelect'; -import EventEmitter from './EventEmitter'; import * as ImagePicker from 'expo-image-picker'; -import AsyncStorage from '@react-native-async-storage/async-storage'; +import ProfileService from '../Services/Profiles/ProfileService'; export default function CreateProfile(props) { @@ -19,6 +18,7 @@ export default function CreateProfile(props) { const [avatar, setAvatar] = useState(''); const [selectedDiets, setSelectedDiets] = useState([]); const [selectedAllergies] = useState([]) + const profileService = new ProfileService() const handleSelectedDiets = (selectedValues) => { setSelectedDiets(selectedValues); @@ -48,7 +48,6 @@ export default function CreateProfile(props) { const handleCreateProfile = async () => { try { - // Ton code pour récupérer les profils existants et ajouter un nouveau profil const newProfile = { name: name, avatar: avatar, @@ -57,14 +56,7 @@ export default function CreateProfile(props) { isActive: "flex", isWaiting: "none", }; - - // Mettre à jour AsyncStorage avec le nouveau profil - let existingProfiles = await AsyncStorage.getItem('profiles'); - existingProfiles = existingProfiles ? JSON.parse(existingProfiles) : []; - const updatedProfiles = [...existingProfiles, newProfile]; - await AsyncStorage.setItem('profiles', JSON.stringify(updatedProfiles)); - EventEmitter.emit('profileAdded'); - console.log('Profil créé :', newProfile); + profileService.addProfile(newProfile) props.navigation.goBack(); } catch (error) { console.error('Erreur lors de la création du profil :', error); diff --git a/LeftOvers/screens/FiltersSelection.tsx b/LeftOvers/screens/FiltersSelection.tsx index 494b82b..34a5092 100644 --- a/LeftOvers/screens/FiltersSelection.tsx +++ b/LeftOvers/screens/FiltersSelection.tsx @@ -2,14 +2,14 @@ import React, {useContext, useState, useEffect} from 'react'; import {StyleSheet, View, Text, ScrollView, useWindowDimensions} from 'react-native'; import {LinearGradient} from 'expo-linear-gradient'; import {SafeAreaProvider} from 'react-native-safe-area-context'; - import ValidateButton from '../components/ValidateButton'; import ListSelect from '../components/ListSelect'; import ListWithoutSelect from '../components/ListWithoutSelect'; import ProfileSelection from '../components/ProfileSelection'; import ColorContext from '../theme/ColorContext'; -import EventEmitter from './EventEmitter'; +import eventEmitter from './EventEmitter'; import AsyncStorage from '@react-native-async-storage/async-storage'; +import ProfileService from '../Services/Profiles/ProfileService'; export default function FiltersSelection(props) { const {colors} = useContext(ColorContext); @@ -17,7 +17,7 @@ export default function FiltersSelection(props) { {name: "None", avatar: "logo.png", diets: [], allergies: [], isActive: "none", isWaiting: "none"}, ] const die = [{value: "Dairy free"}, {value: "Gluten free"}, {value: "Porkless"}, {value: "Vegan"}, {value: "Vegetarian"}, {value: "Pescatarian"}] - + const profileService = new ProfileService() const [profiles, setProfiles] = useState(profilesHand); const [dieProfiles, setDieProfiles] = useState([]) const [allProfiles, setAllProfiles] = useState([]) @@ -25,45 +25,31 @@ export default function FiltersSelection(props) { const [allAdd, setAllAdd] = useState([]) const [selectedDiets, setSelectedDiets] = useState([]) - const handleGetProfiles = async () => { - try { - const existingProfiles = await AsyncStorage.getItem('profiles'); - return JSON.parse(existingProfiles) || []; - } catch (error) { - console.log("Error occured during GetProfiles", error); - return []; - } - } - const fetchProfiles = async () => { - const existingProfiles = await handleGetProfiles() - setProfiles(existingProfiles) - }; + setProfiles(await profileService.getProfiles()) + } - const subscription = EventEmitter.addListener('profileAdded', async () => { + const subscriptionAddProfile = eventEmitter.addListener('profileAdded', async () => { fetchProfiles() + subscriptionAddProfile.remove() + eventEmitter.removeAllListeners('profileAdded') + eventEmitter.removeAllListeners('updateDietsAllergies') + eventEmitter.removeAllListeners('selectedProfilesUpdated') }); - let cptSubscription = 1 - - const subscriptionUpdateDietsAllergies = EventEmitter.addListener('updateDietsAllergies', async () => { - updateDiets() - setDieAdd(die.filter(isInProfileDiets)) - console.log("Passage Subsciption:", cptSubscription) - }); - - let cptSubscriptionDiets = 1 - - const subscriptionUpdateDiets = EventEmitter.addListener('updateDiets', async () => { - setDieAdd(die.filter(isInProfileDiets)) - console.log("Passage SubsciptionDiets:", cptSubscriptionDiets) - }); + const subscriptionUpdateDietsAllergies = eventEmitter.addListener('updateDietsAllergies', async() => { + setDieAdd(die.filter(isInProfileDiets)) + setAllAdd([]) + subscriptionUpdateDietsAllergies.remove() + subscriptionUpdateProfiles.remove(); + eventEmitter.removeAllListeners('updateDietsAllergies') + }) useEffect(() => { fetchProfiles() }, []); - const handleSaveSelectedProfiles = async () => { + async function handleSaveSelectedProfiles(){ try { profiles.forEach((val) => { if(val.isWaiting == "flex"){ @@ -78,19 +64,20 @@ export default function FiltersSelection(props) { }) await AsyncStorage.setItem('profiles', JSON.stringify(profiles)); fetchProfiles() - updateDiets() - setDieAdd(die.filter(isInProfileDiets)) - updateAllergies() - setAllAdd([]) + eventEmitter.emit("selectedProfilesUpdated") } catch (error) { - console.error('Error occured when updating active profiles:', error); + console.error('Error occured when updating active profiles:', error, selectedDiets); } }; - const subscriptionUpdateSelectedProfiles = EventEmitter.addListener('selectedProfilesUpdated', async () => { - fetchProfiles() + const subscriptionUpdateProfiles = eventEmitter.addListener('selectedProfilesUpdated', async () => { updateDiets() - setDieAdd(die.filter(isInProfileDiets)) + updateAllergies() + eventEmitter.emit("updateDietsAllergies") + subscriptionUpdateProfiles.remove(); + eventEmitter.removeAllListeners('profileAdded') + eventEmitter.removeAllListeners('updateDietsAllergies') + eventEmitter.removeAllListeners('selectedProfilesUpdated') }); const updateDiets = () => { @@ -101,13 +88,12 @@ export default function FiltersSelection(props) { profile.diets.forEach((diet) => { retType = true dieTemp.forEach((val) => { - console.log("Value DieTemp:",val) - if(val.value == diet){ + if(val == diet){ retType = false } }) if(retType){ - dieTemp.push({value: diet}) + dieTemp.push(diet) } }) } @@ -123,18 +109,17 @@ export default function FiltersSelection(props) { profile.allergies.forEach((allergy) => { retType = true allTemp.forEach((val) => { - if(val.value == allergy){ + if(val == allergy){ retType = false } }) if(retType){ - allTemp.push({value: allergy}) + allTemp.push(allergy) } }) } }) setAllProfiles(allTemp) - console.log("Technique de Shinobi Anti-CodeSmell", selectedDiets) } const changeStatusWaiting = (cpt) => { @@ -177,8 +162,8 @@ export default function FiltersSelection(props) { function isInProfileDiets(element) { let retType = true - dieProfiles.forEach(function (diet) { - if(diet.value==element.value){ + dieProfiles.forEach((diet) => { + if(diet==element.value){ retType = false } }) @@ -281,9 +266,9 @@ export default function FiltersSelection(props) { Additional Filters {dieAdd.length} available - + - + props.navigation.navigate("IngredientSelection")}> diff --git a/LeftOvers/screens/HomePage.tsx b/LeftOvers/screens/HomePage.tsx index ad8dc40..7fed9f9 100644 --- a/LeftOvers/screens/HomePage.tsx +++ b/LeftOvers/screens/HomePage.tsx @@ -2,17 +2,15 @@ import React, { useContext, useState, useEffect } from 'react'; import { StyleSheet, View, Text, Pressable, Image, ScrollView } from 'react-native'; import {LinearGradient} from 'expo-linear-gradient'; import {SafeAreaProvider} from 'react-native-safe-area-context'; - import ValidateButton from '../components/ValidateButton'; import ProfileSelection from '../components/ProfileSelection'; import FoodElementText from '../components/FoodElementText'; import ColorContext from '../theme/ColorContext'; - import bracketLeft from '../assets/images/angle_bracket_left.png'; import bracketRight from '../assets/images/angle_bracket_right.png'; - import AsyncStorage from '@react-native-async-storage/async-storage'; -import EventEmitter from './EventEmitter'; +import eventEmitter from './EventEmitter'; +import ProfileService from '../Services/Profiles/ProfileService'; export default function HomePage({ navigation, props }) { const colors = useContext(ColorContext).colors @@ -25,35 +23,21 @@ export default function HomePage({ navigation, props }) { const [profiles, setProfiles] = useState(profilesHand); const [ingredientList, setIngredientList] = useState(ingredientListHand) - const handleGetProfiles = async () => { - try { - const existingProfiles = await AsyncStorage.getItem('profiles'); - return JSON.parse(existingProfiles) || []; - } catch (error) { - console.log(error); - return []; - } - } + const profileService = new ProfileService() const handleGetAvailableIngredient = async () => { try { const existingAvailableIngredient = await AsyncStorage.getItem('ingredient'); return JSON.parse(existingAvailableIngredient) || []; } catch (error) { - console.log(error); + console.error(error); return []; } } const fetchProfiles = async () => { - const existingProfiles = await handleGetProfiles(); - if (existingProfiles.length != 0){ - setProfiles(existingProfiles); - } - else{ - setProfiles(profilesHand) - } - }; + setProfiles(await profileService.getProfiles()) + } const fetchAvailableIngredient = async () => { const existingAvailableIngredient = await handleGetAvailableIngredient(); @@ -65,34 +49,64 @@ export default function HomePage({ navigation, props }) { } }; - const subscriptionAddProfile = EventEmitter.addListener('profileAdded', async () => { + const subscriptionAddProfile = eventEmitter.addListener('profileAdded', async () => { fetchProfiles(); + subscriptionAddProfile.remove(); + eventEmitter.removeAllListeners('profileAdded') + eventEmitter.removeAllListeners('profileDeleted') + eventEmitter.removeAllListeners('ingredientAdded') + eventEmitter.removeAllListeners('ingredientDeleted') + eventEmitter.removeAllListeners('selectedProfilesUpdated') }); - const subscriptionDeleteProfile = EventEmitter.addListener('profileDeleted', async () => { + const subscriptionDeleteProfile = eventEmitter.addListener('profileDeleted', async () => { if (profiles.length == 1){ setProfiles(profilesHand) } else{ fetchProfiles(); } + subscriptionDeleteProfile.remove(); + eventEmitter.removeAllListeners('profileAdded') + eventEmitter.removeAllListeners('profileDeleted') + eventEmitter.removeAllListeners('ingredientAdded') + eventEmitter.removeAllListeners('ingredientDeleted') + eventEmitter.removeAllListeners('selectedProfilesUpdated') }); - const subscriptionAddIngredient = EventEmitter.addListener('ingredientAdded', async () => { + const subscriptionAddIngredient = eventEmitter.addListener('ingredientAdded', async () => { fetchAvailableIngredient(); + subscriptionAddIngredient.remove(); + eventEmitter.removeAllListeners('profileAdded') + eventEmitter.removeAllListeners('profileDeleted') + eventEmitter.removeAllListeners('ingredientAdded') + eventEmitter.removeAllListeners('ingredientDeleted') + eventEmitter.removeAllListeners('selectedProfilesUpdated') }); - const subscriptionDeleteIngredient = EventEmitter.addListener('ingredientDeleted', async () => { + const subscriptionDeleteIngredient = eventEmitter.addListener('ingredientDeleted', async () => { if (ingredientList.length == 1){ setIngredientList(ingredientListHand) } else{ fetchAvailableIngredient(); } + subscriptionDeleteIngredient.remove(); + eventEmitter.removeAllListeners('profileAdded') + eventEmitter.removeAllListeners('profileDeleted') + eventEmitter.removeAllListeners('ingredientAdded') + eventEmitter.removeAllListeners('ingredientDeleted') + eventEmitter.removeAllListeners('selectedProfilesUpdated') }); - const subscriptionUpdateSelectedProfile = EventEmitter.addListener('selectedProfilesUpdated', async () => { + const subscriptionUpdateSelectedProfile = eventEmitter.addListener('selectedProfilesUpdated', async () => { fetchProfiles(); + subscriptionUpdateSelectedProfile.remove(); + eventEmitter.removeAllListeners('profileAdded') + eventEmitter.removeAllListeners('profileDeleted') + eventEmitter.removeAllListeners('ingredientAdded') + eventEmitter.removeAllListeners('ingredientDeleted') + eventEmitter.removeAllListeners('selectedProfilesUpdated') }); useEffect(() => { @@ -233,7 +247,7 @@ export default function HomePage({ navigation, props }) { {nbActiveProfiles()} selected - console.log("Ignorer", val)}/> + val += 1}/> navigation.navigate('FiltersSelection')}/> diff --git a/LeftOvers/screens/IngredientSelection.tsx b/LeftOvers/screens/IngredientSelection.tsx index 15e180e..ec589a2 100644 --- a/LeftOvers/screens/IngredientSelection.tsx +++ b/LeftOvers/screens/IngredientSelection.tsx @@ -9,7 +9,7 @@ import IngredientService from '../Services/Ingredients/IngredientsServices'; import ColorContext from '../theme/ColorContext'; import ValidateButton from '../components/ValidateButton'; import AsyncStorage from '@react-native-async-storage/async-storage'; -import EventEmitter from './EventEmitter'; +import eventEmitter from './EventEmitter'; import plus from '../assets/images/plus.png'; import moins from '../assets/images/minus.png'; @@ -37,7 +37,7 @@ export default function IngredientSelection(props) { setResponse(filtered); } } catch (error) { - console.log(error); + console.error(error); } finally { setIsLoading(false); } @@ -50,10 +50,9 @@ export default function IngredientSelection(props) { const loadIngredients = async () => { try { - const ingredients = await ingredientService.getAllIngredient(); - setResponse(ingredients); + setResponse(await ingredientService.getAllIngredient()); } catch (error) { - console.log(error); + console.error(error); } finally { setIsLoading(false); } @@ -96,7 +95,7 @@ const loadIngredients = async () => { const existingAvailableIngredient = await AsyncStorage.getItem('ingredient'); return JSON.parse(existingAvailableIngredient) || []; } catch (error) { - console.log(error); + console.error(error); return []; } } @@ -107,7 +106,7 @@ const fetchAvailableIngredient = async () => { setSelectedIngredients(existingAvailableIngredient); } else{ - setSelectedIngredients([{value: "None"}]) + setSelectedIngredients([new Ingredient(-1, "None")]) } }; @@ -119,13 +118,14 @@ const fetchAvailableIngredient = async () => { existingAvailableIngredient = existingAvailableIngredient ? JSON.parse(existingAvailableIngredient) : []; const updatedAvailableIngredient = [...existingAvailableIngredient, newIngredient]; await AsyncStorage.setItem('ingredient', JSON.stringify(updatedAvailableIngredient)); - EventEmitter.emit('ingredientAdded'); + eventEmitter.emit('ingredientAdded'); + fetchAvailableIngredient(); console.log('Ingredient Added:', newIngredient); ChangeAvailableSize(false) } } catch(error){ - console.log("Error occured during the addition of Ingredient:", error) + console.error("Error occured during the addition of Ingredient:", error) } }; @@ -133,28 +133,16 @@ const fetchAvailableIngredient = async () => { try{ const updatedIngredients = selectedIngredients.filter((ingredient) => ingredient.id !== idIngredient); await AsyncStorage.setItem('ingredient', JSON.stringify(updatedIngredients)); - EventEmitter.emit('ingredientDeleted'); + eventEmitter.emit('ingredientDeleted'); fetchAvailableIngredient(); setSelectedIngredients(updatedIngredients); ChangeAvailableSize(true) } catch (error){ - console.log("Error occured during the suppression of Ingredient:", error) + console.error("Error occured during the suppression of Ingredient:", error) } }; - const subscriptionAddIngredient = EventEmitter.addListener('ingredientAdded', async () => { - fetchAvailableIngredient(); - }); - const subscriptionDeleteIngredient = EventEmitter.addListener('ingredientDeleted', async () => { - if (selectedIngredients.length == 1){ - setSelectedIngredients([{title: "None"}]) - } - else{ - fetchAvailableIngredient(); - } - }); - const ChangeAvailableSize = (remove: boolean) => { if(remove){ if (selectedIngredients.length == 1){ @@ -178,7 +166,7 @@ const fetchAvailableIngredient = async () => { setAvailableSize(90) } else if (selectedIngredients.length == 1){ - if(selectedIngredients[0].value == "None"){ + if(selectedIngredients[0].name == "None"){ setAvailableSize(90) } else{ @@ -199,7 +187,7 @@ const fetchAvailableIngredient = async () => { const ingredientsByLetter = await ingredientService.getIngredientByLetter(letter); setResponse(ingredientsByLetter); } catch (error) { - console.log(error); + console.error(error); } finally { setIsLoading(false); } diff --git a/LeftOvers/screens/ModifyProfile.tsx b/LeftOvers/screens/ModifyProfile.tsx index f243f3f..771c502 100644 --- a/LeftOvers/screens/ModifyProfile.tsx +++ b/LeftOvers/screens/ModifyProfile.tsx @@ -15,27 +15,23 @@ export default function ModifyProfile(props) { const [profile, setProfile] = useState(null); const route = useRoute(); - - - const handleGetProfileByName = async (profileName) => { try { const existingProfiles = await AsyncStorage.getItem('profiles'); const profiles = JSON.parse(existingProfiles) || []; const matchedProfile = profiles.find(profile => profile.name === profileName); - console.log("Le profil choisit : " + matchedProfile); return matchedProfile || null; } catch (error) { - console.log("Erreur lors de la récupération du profil :", error); + console.error("Erreur lors de la récupération du profil :", error); return null; } }; const fetchProfiles = async () => { - const selectedProfil = await handleGetProfileByName(route.params); - setProfile(selectedProfil); -}; + const selectedProfil = await handleGetProfileByName(route.params); + setProfile(selectedProfil); + }; useEffect(() => { fetchProfiles(); @@ -48,7 +44,7 @@ useEffect(() => { - (console.log("Profile Modified"))}> + console.log("")}> diff --git a/LeftOvers/screens/Profiles.tsx b/LeftOvers/screens/Profiles.tsx index e763b8f..b572319 100644 --- a/LeftOvers/screens/Profiles.tsx +++ b/LeftOvers/screens/Profiles.tsx @@ -3,23 +3,21 @@ import { StyleSheet, View, Modal, Pressable, Text, Image, ScrollView, useWindowD import { LinearGradient } from 'expo-linear-gradient'; import { SafeAreaProvider } from 'react-native-safe-area-context'; - +import ProfileService from '../Services/Profiles/ProfileService'; import ProfileDetails from '../components/ProfileDetails'; import ColorContext from '../theme/ColorContext'; -import AsyncStorage from '@react-native-async-storage/async-storage'; -import EventEmitter from './EventEmitter'; +import eventEmitter from './EventEmitter'; import { PaperProvider, Portal } from 'react-native-paper'; export default function Profiles({navigation, props}) { const colors = useContext(ColorContext).colors - + const profileService = new ProfileService() const [visible, setVisible] = useState(false); const [profiles, setProfiles] = useState([]); const [selectedProfileIndex, setSelectedProfileIndex] = useState(null); const goDetails = (name: string) => navigation.navigate('ProfileCreation', name); - const raisePopUp = (index) => { setSelectedProfileIndex(index) setVisible(true) @@ -30,10 +28,7 @@ export default function Profiles({navigation, props}) { const handleDeleteProfile = async (index) => { try { - const updatedProfiles = profiles.filter((profile, i) => i !== index); - await AsyncStorage.setItem('profiles', JSON.stringify(updatedProfiles)); - EventEmitter.emit('profileDeleted'); - fetchProfiles(); + profileService.delProfile(index) setSelectedProfileIndex(index); erasePopUp(); } catch (error) { @@ -41,23 +36,22 @@ export default function Profiles({navigation, props}) { } }; - const handleGetProfiles = async () => { - try { - const existingProfiles = await AsyncStorage.getItem('profiles'); - return JSON.parse(existingProfiles) || []; - } catch (error) { - console.log("Error occured during GetProfiles", error); - return []; - } + const fetchProfiles = async () => { + setProfiles(await profileService.getProfiles()) } - const fetchProfiles = async () => { - const existingProfiles = await handleGetProfiles(); - setProfiles(existingProfiles); - }; + const subscription = eventEmitter.addListener('profileAdded', async () => { + fetchProfiles(); + subscription.remove(); + eventEmitter.removeAllListeners('profileAdded') + eventEmitter.removeAllListeners('profileDeleted') + }); - const subscription = EventEmitter.addListener('profileAdded', async () => { + const subscriptionDeletedProfile = eventEmitter.addListener('profileDeleted', async () => { fetchProfiles(); + subscriptionDeletedProfile.remove(); + eventEmitter.removeAllListeners('profileAdded') + eventEmitter.removeAllListeners('profileDeleted') }); useEffect(() => { diff --git a/LeftOvers/screens/RecipeDetails.tsx b/LeftOvers/screens/RecipeDetails.tsx index 33a0a72..553584c 100644 --- a/LeftOvers/screens/RecipeDetails.tsx +++ b/LeftOvers/screens/RecipeDetails.tsx @@ -102,7 +102,6 @@ export default function RecipeDetails({ route }) { return categories[0]; } - console.log("LA LISTE DES CATEGORY : " + categories) let bestMatch = { category: '', similarity: 0 }; for (const [name, categoriesList] of Object.entries(categoryMappings)) { @@ -119,20 +118,15 @@ export default function RecipeDetails({ route }) { 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': @@ -179,10 +173,9 @@ export default function RecipeDetails({ route }) { const loadRecipe = async () => { try { const recipe = await recipesService.getRecipeById(recipeId); - console.log("Recipe.name: "+recipe.name) setResponse(recipe); } catch (error) { - console.log(error); + console.error(error); } finally{ setIsLoading(false) } diff --git a/LeftOvers/screens/RecipeSuggestion.tsx b/LeftOvers/screens/RecipeSuggestion.tsx index 2e6cb14..02a0149 100644 --- a/LeftOvers/screens/RecipeSuggestion.tsx +++ b/LeftOvers/screens/RecipeSuggestion.tsx @@ -99,7 +99,7 @@ export default function RecipeSuggestion({ route, navigation }) { } } catch (error) { - console.log(error) + console.error(error) } };