Merge branch 'master' into WORK-RHA2
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
commit
cd3db218f8
@ -0,0 +1,30 @@
|
|||||||
|
export default class Profile {
|
||||||
|
private _name: string;
|
||||||
|
private _avatar: string;
|
||||||
|
private _allergy: string[];
|
||||||
|
private _diets: string[];
|
||||||
|
|
||||||
|
constructor( name: string, avatar: string, allergy: string[], diets: string[]) {
|
||||||
|
this._name = name;
|
||||||
|
this._avatar = avatar;
|
||||||
|
this._allergy = allergy;
|
||||||
|
this._diets = diets;
|
||||||
|
}
|
||||||
|
|
||||||
|
get name(): string {
|
||||||
|
return this._name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
get avatar(): string{
|
||||||
|
return this._avatar;
|
||||||
|
}
|
||||||
|
|
||||||
|
get allergy(): string[]{
|
||||||
|
return this._allergy;
|
||||||
|
}
|
||||||
|
|
||||||
|
get diets(): string[]{
|
||||||
|
return this._diets;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
import Profil from "../../Models/Profil";
|
||||||
|
|
||||||
|
export default interface IProfileService {
|
||||||
|
getProfiles(): Promise<Profil[]>,
|
||||||
|
addProfile(new_profile: Profil): Promise<boolean>,
|
||||||
|
delProfile(profile_name_to_del: string): Promise<boolean>
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
import Profil from "../../Models/Profil";
|
||||||
|
import IProfileService from "./IProfileService";
|
||||||
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||||
|
|
||||||
|
export default class ProfileService implements IProfileService {
|
||||||
|
async getProfiles(): Promise<Profil[]> {
|
||||||
|
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<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
|
||||||
|
}
|
||||||
|
|
||||||
|
async delProfile(profile_name_to_del: string): Promise<boolean> {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue