ProfileService (delProfile)
continuous-integration/drone/push Build is passing Details

pull/24/head^2
Louison PARANT 1 year ago
parent d1bfdce68d
commit 653b3cf301

@ -3,5 +3,5 @@ import Profile from "../../Models/Profile";
export default interface IProfileService { export default interface IProfileService {
getProfiles(): Promise<Profile[]>, getProfiles(): Promise<Profile[]>,
addProfile(newProfile: Profile): Promise<boolean>, addProfile(newProfile: Profile): Promise<boolean>,
delProfile(profile_name_to_del: string): Promise<boolean> delProfile(index: number): Promise<boolean>
} }

@ -14,7 +14,7 @@ export default class ProfileService implements IProfileService {
} }
async addProfile(newProfile : Profile): Promise<boolean> { async addProfile(newProfile : Profile): Promise<boolean> {
let existingProfiles = await AsyncStorage.getItem('profiles'); let existingProfiles = await AsyncStorage.getItem('profiles')
existingProfiles = existingProfiles ? JSON.parse(existingProfiles) : []; existingProfiles = existingProfiles ? JSON.parse(existingProfiles) : [];
const updatedProfiles = [...existingProfiles, newProfile]; const updatedProfiles = [...existingProfiles, newProfile];
await AsyncStorage.setItem('profiles', JSON.stringify(updatedProfiles)); await AsyncStorage.setItem('profiles', JSON.stringify(updatedProfiles));
@ -22,17 +22,11 @@ export default class ProfileService implements IProfileService {
return true return true
} }
async delProfile(profile_name_to_del: string): Promise<boolean> { async delProfile(index: number): Promise<boolean> {
const existing_profiles = await this.getProfiles() const existingProfiles = await this.getProfiles()
let key: number = -1 const updatedProfiles = existingProfiles.filter((profile, i) => i !== index);
for (let current_profile of existing_profiles) { await AsyncStorage.setItem('profiles', JSON.stringify(updatedProfiles));
if (current_profile.name == profile_name_to_del) { eventEmitter.emit('profileDeleted');
let updated_profile = existing_profiles.splice(key, 1) return true
await AsyncStorage.setItem('profiles', JSON.stringify(updated_profile))
return true
}
key ++
}
return false
} }
} }

@ -29,10 +29,11 @@ export default function Profiles({navigation, props}) {
const handleDeleteProfile = async (index) => { const handleDeleteProfile = async (index) => {
try { try {
const updatedProfiles = profiles.filter((profile, i) => i !== index); // const updatedProfiles = profiles.filter((profile, i) => i !== index);
await AsyncStorage.setItem('profiles', JSON.stringify(updatedProfiles)); // await AsyncStorage.setItem('profiles', JSON.stringify(updatedProfiles));
eventEmitter.emit('profileDeleted'); // eventEmitter.emit('profileDeleted');
fetchProfiles();
profileService.delProfile(index)
setSelectedProfileIndex(index); setSelectedProfileIndex(index);
erasePopUp(); erasePopUp();
} catch (error) { } catch (error) {
@ -46,9 +47,16 @@ export default function Profiles({navigation, props}) {
const subscription = eventEmitter.addListener('profileAdded', async () => { const subscription = eventEmitter.addListener('profileAdded', async () => {
fetchProfiles(); fetchProfiles();
console.log("Profiles sub Added: ------------------------------------")
subscription.remove(); subscription.remove();
eventEmitter.removeAllListeners('profileAdded') eventEmitter.removeAllListeners('profileAdded')
eventEmitter.removeAllListeners('profileDeleted')
});
const subscriptionDeletedProfile = eventEmitter.addListener('profileDeleted', async () => {
fetchProfiles();
subscriptionDeletedProfile.remove();
eventEmitter.removeAllListeners('profileAdded')
eventEmitter.removeAllListeners('profileDeleted')
}); });
useEffect(() => { useEffect(() => {

Loading…
Cancel
Save