|
|
|
@ -4,15 +4,25 @@ import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
|
|
|
|
|
|
|
|
export default class ProfileService implements IProfileService {
|
|
|
|
|
async getProfiles(): Promise<Profil[]> {
|
|
|
|
|
const existingProfiles = await AsyncStorage.getItem('profiles');
|
|
|
|
|
return JSON.parse(existingProfiles) || [];
|
|
|
|
|
const results = await AsyncStorage.getItem('profiles');
|
|
|
|
|
const tmp = JSON.parse(results)
|
|
|
|
|
let existingProfiles: Profil[] = []
|
|
|
|
|
for (let item of tmp) {
|
|
|
|
|
existingProfiles.push(new Profil(item._name, item._avatar, item._allergy, item._diets))
|
|
|
|
|
}
|
|
|
|
|
return existingProfiles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addProfile(new_profile : Profil): Promise<void> {
|
|
|
|
|
const list = [new_profile]
|
|
|
|
|
const key_exist = ((await AsyncStorage.getAllKeys()).includes('profiles'))
|
|
|
|
|
if (!key_exist) await AsyncStorage.setItem('profiles', JSON.stringify(list))
|
|
|
|
|
else await AsyncStorage.mergeItem('profiles', JSON.stringify(list))
|
|
|
|
|
async addProfile(new_profile : Profil): Promise<boolean> {
|
|
|
|
|
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]))
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|