Merge branch 'master' into WORK-RHA2
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
commit
14a0587c1b
@ -1,30 +1,41 @@
|
|||||||
export default class Profile {
|
export default class Profile {
|
||||||
private _name: string;
|
public name: string;
|
||||||
private _avatar: string;
|
public avatar: string;
|
||||||
private _allergy: string[];
|
public allergies: string[];
|
||||||
private _diets: string[];
|
public diets: string[];
|
||||||
|
public isActive: string;
|
||||||
|
public isWaiting: string
|
||||||
|
|
||||||
constructor( name: string, avatar: string, allergy: string[], diets: string[]) {
|
constructor( name: string, avatar: string, allergies: string[], diets: string[], isActive: string, isWaiting: string) {
|
||||||
this._name = name;
|
this.name = name;
|
||||||
this._avatar = avatar;
|
this.avatar = avatar;
|
||||||
this._allergy = allergy;
|
this.diets = diets;
|
||||||
this._diets = diets;
|
this.allergies = allergies;
|
||||||
|
this.isActive = isActive;
|
||||||
|
this.isWaiting = isWaiting
|
||||||
}
|
}
|
||||||
|
|
||||||
get name(): string {
|
// get name(): string {
|
||||||
return this._name;
|
// return this._name;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
// get avatar(): string{
|
||||||
|
// return this._avatar;
|
||||||
|
// }
|
||||||
|
|
||||||
get avatar(): string{
|
// get allergies(): string[]{
|
||||||
return this._avatar;
|
// return this._allergies;
|
||||||
}
|
// }
|
||||||
|
|
||||||
get allergy(): string[]{
|
// get diets(): string[]{
|
||||||
return this._allergy;
|
// return this._diets;
|
||||||
}
|
// }
|
||||||
|
|
||||||
get diets(): string[]{
|
// get isActive(): string{
|
||||||
return this._diets;
|
// return this._isActive;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
// get isWaiting(): string{
|
||||||
|
// return this._isWaiting;
|
||||||
|
// }
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
import Profil from "../../Models/Profil";
|
import Profile from "../../Models/Profile";
|
||||||
|
|
||||||
export default interface IProfileService {
|
export default interface IProfileService {
|
||||||
getProfiles(): Promise<Profil[]>,
|
getProfiles(): Promise<Profile[]>,
|
||||||
addProfile(new_profile: Profil): Promise<boolean>,
|
addProfile(newProfile: Profile): Promise<boolean>,
|
||||||
delProfile(profile_name_to_del: string): Promise<boolean>
|
delProfile(index: number): Promise<boolean>
|
||||||
}
|
}
|
@ -1,41 +1,32 @@
|
|||||||
import Profil from "../../Models/Profil";
|
import Profile from "../../Models/Profile";
|
||||||
import IProfileService from "./IProfileService";
|
import IProfileService from "./IProfileService";
|
||||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||||
|
import eventEmitter from "../../screens/EventEmitter";
|
||||||
|
|
||||||
export default class ProfileService implements IProfileService {
|
export default class ProfileService implements IProfileService {
|
||||||
async getProfiles(): Promise<Profil[]> {
|
async getProfiles(): Promise<Profile[]> {
|
||||||
const results = await AsyncStorage.getItem('profiles');
|
const results = await AsyncStorage.getItem('profiles')
|
||||||
const tmp = JSON.parse(results)
|
const existingProfiles = JSON.parse(results)
|
||||||
let existingProfiles: Profil[] = []
|
if(existingProfiles.length == 0){
|
||||||
for (let item of tmp) {
|
existingProfiles.push(new Profile("None", "logo.png", [], [], "none", "none"))
|
||||||
existingProfiles.push(new Profil(item._name, item._avatar, item._allergy, item._diets))
|
|
||||||
}
|
}
|
||||||
return existingProfiles;
|
return existingProfiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
async addProfile(new_profile : Profil): Promise<boolean> {
|
async addProfile(newProfile : Profile): Promise<boolean> {
|
||||||
const existingProfiles = await this.getProfiles()
|
let existingProfiles = await AsyncStorage.getItem('profiles')
|
||||||
for (let current_profile of existingProfiles) {
|
existingProfiles = existingProfiles ? JSON.parse(existingProfiles) : [];
|
||||||
if (current_profile.name == new_profile.name) {
|
const updatedProfiles = [...existingProfiles, newProfile];
|
||||||
console.log("Tried to create a profil already existing !")
|
await AsyncStorage.setItem('profiles', JSON.stringify(updatedProfiles));
|
||||||
return false
|
eventEmitter.emit("profileAdded")
|
||||||
}
|
|
||||||
}
|
|
||||||
await AsyncStorage.setItem('profiles', JSON.stringify([...existingProfiles, new_profile]))
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in new issue