From 653b3cf301dab115a5171b430189771a475b3552 Mon Sep 17 00:00:00 2001 From: Louison PARANT Date: Fri, 8 Dec 2023 14:25:36 +0100 Subject: [PATCH] ProfileService (delProfile) --- .../Services/Profiles/IProfileService.ts | 2 +- LeftOvers/Services/Profiles/ProfileService.ts | 20 +++++++------------ LeftOvers/screens/Profiles.tsx | 18 ++++++++++++----- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/LeftOvers/Services/Profiles/IProfileService.ts b/LeftOvers/Services/Profiles/IProfileService.ts index 2c648f6..3e628d4 100644 --- a/LeftOvers/Services/Profiles/IProfileService.ts +++ b/LeftOvers/Services/Profiles/IProfileService.ts @@ -3,5 +3,5 @@ import Profile from "../../Models/Profile"; export default interface IProfileService { getProfiles(): Promise, addProfile(newProfile: Profile): Promise, - delProfile(profile_name_to_del: string): 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 e38b5e8..8704434 100644 --- a/LeftOvers/Services/Profiles/ProfileService.ts +++ b/LeftOvers/Services/Profiles/ProfileService.ts @@ -14,7 +14,7 @@ export default class ProfileService implements IProfileService { } async addProfile(newProfile : Profile): Promise { - let existingProfiles = await AsyncStorage.getItem('profiles'); + let existingProfiles = await AsyncStorage.getItem('profiles') existingProfiles = existingProfiles ? JSON.parse(existingProfiles) : []; const updatedProfiles = [...existingProfiles, newProfile]; await AsyncStorage.setItem('profiles', JSON.stringify(updatedProfiles)); @@ -22,17 +22,11 @@ export default class ProfileService implements IProfileService { 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/screens/Profiles.tsx b/LeftOvers/screens/Profiles.tsx index 0acc28a..c313ff7 100644 --- a/LeftOvers/screens/Profiles.tsx +++ b/LeftOvers/screens/Profiles.tsx @@ -29,10 +29,11 @@ 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(); + // const updatedProfiles = profiles.filter((profile, i) => i !== index); + // await AsyncStorage.setItem('profiles', JSON.stringify(updatedProfiles)); + // eventEmitter.emit('profileDeleted'); + + profileService.delProfile(index) setSelectedProfileIndex(index); erasePopUp(); } catch (error) { @@ -46,9 +47,16 @@ export default function Profiles({navigation, props}) { const subscription = eventEmitter.addListener('profileAdded', async () => { fetchProfiles(); - console.log("Profiles sub Added: ------------------------------------") subscription.remove(); eventEmitter.removeAllListeners('profileAdded') + eventEmitter.removeAllListeners('profileDeleted') + }); + + const subscriptionDeletedProfile = eventEmitter.addListener('profileDeleted', async () => { + fetchProfiles(); + subscriptionDeletedProfile.remove(); + eventEmitter.removeAllListeners('profileAdded') + eventEmitter.removeAllListeners('profileDeleted') }); useEffect(() => {