Skip to content

Commit

Permalink
feat(tips): enregistrer consultations medias
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoblanc committed Aug 13, 2019
1 parent 92c0332 commit 8a7bdb9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { Router } from '@angular/router';
templateUrl: 'app.component.html'
})
export class AppComponent implements OnInit {
private static DISPLAY_TUTO = 'DISPLAY_TUTO';

constructor(
private platform: Platform,
Expand Down Expand Up @@ -80,12 +79,13 @@ export class AppComponent implements OnInit {
});


this.storage.get(AppComponent.DISPLAY_TUTO).subscribe((alreadyDisplayed) => {
this.storage.get(StorageService.DISPLAY_TUTO).subscribe((alreadyDisplayed) => {
if (alreadyDisplayed) {
return;
}
this.router.navigate(['/tuto']);
this.storage.set(AppComponent.DISPLAY_TUTO, true);
this.storage.set(StorageService.DISPLAY_TUTO, true);
this.storage.set(StorageService.INSTALLATION_DATE, new Date());
});
});

Expand Down
12 changes: 11 additions & 1 deletion src/app/provider/helper/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ import { Storage } from '@ionic/storage';
})
export class StorageService {

constructor(private storage: Storage) { }
public static COUNT_KEY = 'COUNT_KEY';
public static INSTALLATION_DATE = 'INSTALLATION_DATE';
public static DISPLAY_TUTO = 'DISPLAY_TUTO';

constructor(private storage: Storage) { }

/**
* Cette methode se charge de changer une propriété d'objet en storage: key.objectKey = value
* @param key le nom en storage de l'objet
* @param objectKey La clé de la propriété de l'objet
* @param value la valeur que l'on veut doner à l'objet
*/
public editObject(key: string, objectKey: string, value: any): Observable<any> {
return this.get<any>(key).pipe(tap((object: any) => {
if (object == null) {
Expand Down Expand Up @@ -63,4 +72,5 @@ export class StorageService {
}));
}


}
62 changes: 59 additions & 3 deletions src/app/provider/meta-media/meta-media.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
import { Injectable } from '@angular/core';
import { Observable, BehaviorSubject } from 'rxjs';
import { tap } from 'rxjs/operators';
import { tap, count } from 'rxjs/operators';
import { ListMetaMedias } from '../../models/meta-media/list-meta-medias';
import { MetaMedia } from '../../models/meta-media/meta-media';
import listMetaMediaData from '../../../assets/data/listMetaMediaData.json';
import { HttpService } from '../helper/http.service';
import { StorageService } from '../helper/storage.service';
import { AppComponent } from '../../app.component';

@Injectable({
providedIn: 'root'
})
export class MetaMediaService {

constructor(private http: HttpService) {
constructor(private http: HttpService, private storage: StorageService) {

this.getMetaMediaList()
.subscribe((listMetaMedia) => {
console.log('Meta media récupéré');
});
this.storage.get<Date>(StorageService.INSTALLATION_DATE)
.subscribe((dateInstall) => {
try {
this.installDate = new Date(dateInstall);
} catch (error) {
//
}
});

}
public static BASE_URL = 'https://athena-api.caprover.athena-app.fr/list-meta-media';

installDate: Date;

public listMetaMedia: ListMetaMedias[] = listMetaMediaData;
public listMetaMedia$ = new BehaviorSubject(this.listMetaMedia);

Expand All @@ -40,11 +51,56 @@ export class MetaMediaService {
for (const lstMetaMedia of this.listMetaMedia) {
this.currentMetaMedia = lstMetaMedia.metaMedias.find((metaMedia) => metaMedia.key === key);
if (this.currentMetaMedia != null) {
this.dealWithTips();
return this.currentMetaMedia;
}
}
}

/**
* La methode qui ira chercher et mettre a jour le nombre de count d'un media
* le nombre de count correspond au nombre de fois au l'utilisateur va aller
* voir le contenu du media en question
* On s'en sert pour la proposition de dons
* Car si on met le lien en direct, on se fait kicker par les stores
* Pour non prespect de la privacy policy
*/
public dealWithTips() {
this.storage.get<any>(StorageService.COUNT_KEY)
.subscribe((counts: any) => {
// S'il n'y a auncune données en storage
if (counts == null) {
// On init l'objet
counts = {};
// Et on init le champ relatif au edia courant
counts[this.currentMetaMedia.key] = { count: 1 };
this.storage.set(StorageService.COUNT_KEY, counts);
// Si l'objet n'est pas nul mais qu'il n'y a rien pour cette clé
} else if (counts[this.currentMetaMedia.key] == null) {
// On init la clé
counts[this.currentMetaMedia.key] = { count: 1 };
// on set en storage
this.storage.set(StorageService.COUNT_KEY, counts);
} else {
// Dans le cas ou il y a déjà une valeur
const metamediaCount = counts[this.currentMetaMedia.key];
metamediaCount.count++;
counts[this.currentMetaMedia.key] = metamediaCount;
this.storage.set(StorageService.COUNT_KEY, counts);
}
// Ici on vérifie la date, elle doit être plus petite d'au moins 5 jour
let a = new Date();
a = new Date(a.getTime() + 432000000);
if (this.installDate != null && this.installDate < a
&& counts[this.currentMetaMedia.key].count > 10) {


}


});
}



}

0 comments on commit 8a7bdb9

Please sign in to comment.