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