From e42b1b16e589be5c8f7e21a6426114b420932130 Mon Sep 17 00:00:00 2001 From: Konstantin Schulz Date: Tue, 25 Feb 2020 12:31:51 +0100 Subject: [PATCH] the exercise repository now uses caching, application configuration is more easily accessible --- README.md | 2 +- package.json | 2 +- src/app/author/author.page.ts | 10 +- src/app/corpus.service.ts | 156 ++++++++++-------- src/app/exercise-list/exercise-list.page.html | 6 +- src/app/exercise-list/exercise-list.page.ts | 83 ++++++---- .../exercise-parameters.page.ts | 127 +++++++------- src/app/exercise/exercise.page.ts | 61 ++++--- src/app/helper.service.ts | 46 +++--- src/app/models/applicationState.ts | 2 + src/app/models/updateInfo.ts | 7 + src/app/preview/preview.page.html | 6 +- src/app/preview/preview.page.ts | 75 ++++----- src/app/show-text/show-text.page.ts | 13 +- src/app/test/test.page.html | 4 +- src/app/test/test.page.ts | 81 +++++---- src/app/text-range/text-range.page.html | 29 ++-- .../vocabulary-check/vocabulary-check.page.ts | 2 +- src/app/vocabulary.service.ts | 24 ++- src/assets/config.json | 35 ---- src/configMC.ts | 35 ++++ 21 files changed, 404 insertions(+), 402 deletions(-) create mode 100644 src/app/models/updateInfo.ts delete mode 100644 src/assets/config.json create mode 100644 src/configMC.ts diff --git a/README.md b/README.md index a5b90d9..bf3b5af 100644 --- a/README.md +++ b/README.md @@ -39,4 +39,4 @@ To change the URL for the backend, use the `ionic.config.json` file (proxies > p ## Frontend URL Use the `--host 0.0.0.0 --disable-host-check` flag for `ng serve` if you want to use it in a production environment with an Nginx server using proxy_pass. ## Other -For all other kinds of configuration, use `src/assets/config.json`. +For all other kinds of configuration, use `src/configMC.ts`. diff --git a/package.json b/package.json index 913e239..89c71f8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mc_frontend", - "version": "1.5.2", + "version": "1.5.4", "author": "Ionic Framework", "homepage": "https://ionicframework.com/", "scripts": { diff --git a/src/app/author/author.page.ts b/src/app/author/author.page.ts index 8511d0d..d330184 100644 --- a/src/app/author/author.page.ts +++ b/src/app/author/author.page.ts @@ -7,7 +7,6 @@ import {HttpClient} from '@angular/common/http'; import {HelperService} from '../helper.service'; import {ExerciseService} from '../exercise.service'; import {ApplicationState} from '../models/applicationState'; -import {Observable} from 'rxjs'; import {take} from 'rxjs/operators'; /** @@ -30,10 +29,13 @@ export class AuthorPage { public http: HttpClient, public exerciseService: ExerciseService, public helperService: HelperService) { - if (!this.corpusService.availableAuthors) { - this.corpusService.loadCorporaFromLocalStorage(); + if (!this.corpusService.availableAuthors.length) { + this.corpusService.loadCorporaFromLocalStorage().then(() => { + this.toggleTreebankAuthors(); + }); + } else { + this.toggleTreebankAuthors(); } - this.toggleTreebankAuthors(); } public authorsDisplayed: Author[]; diff --git a/src/app/corpus.service.ts b/src/app/corpus.service.ts index d06047c..56dc986 100644 --- a/src/app/corpus.service.ts +++ b/src/app/corpus.service.ts @@ -32,6 +32,8 @@ import {ApplicationState} from './models/applicationState'; import {take} from 'rxjs/operators'; import {TextData} from './models/textData'; import {Storage} from '@ionic/storage'; +import {UpdateInfo} from './models/updateInfo'; +import configMC from '../configMC'; @Injectable({ providedIn: 'root' @@ -39,7 +41,7 @@ import {Storage} from '@ionic/storage'; export class CorpusService { public annisResponse: AnnisResponse; public availableCorpora: CorpusMC[]; - public availableAuthors: Author[]; + public availableAuthors: Author[] = []; public baseUrn: string; public citationsUnavailableString: string; public corporaUnavailableString: string; @@ -81,13 +83,15 @@ export class CorpusService { this.helperService.initApplicationState(); this.initCurrentCorpus(); this.initCurrentTextRange(); - this.checkForUpdates().finally(() => { - this.checkAnnisResponse().then(() => { - this.restoreLastCorpus().then(() => { + this.initUpdateInfo().then(() => { + this.checkForUpdates().finally(() => { + this.checkAnnisResponse().then(() => { + this.restoreLastCorpus().then(() => { + this.isMostRecentSetupLoaded = true; + }); + }, () => { this.isMostRecentSetupLoaded = true; }); - }, () => { - this.isMostRecentSetupLoaded = true; }); }); this.initPhenomenonMap(); @@ -147,17 +151,15 @@ export class CorpusService { checkForUpdates(): Promise { return new Promise((resolve, reject) => { - HelperService.config.pipe(take(1)).subscribe((config: object) => { - this.storage.get(config['localStorageKeyUpdateInfo']).then((jsonString: string) => { - // check local storage for necessary updates - const updateInfoJSON: object = JSON.parse(jsonString); - this.getCorpora(updateInfoJSON ? new Date(updateInfoJSON['corpora'].lastAccessTime).getTime() : 0) - .then(() => { - return resolve(); - }, () => { - return reject(); - }); - }); + this.storage.get(configMC.localStorageKeyUpdateInfo).then((jsonString: string) => { + // check local storage for necessary updates + const updateInfo: UpdateInfo = JSON.parse(jsonString) as UpdateInfo; + this.getCorpora(updateInfo ? updateInfo.corpora : 0) + .then(() => { + return resolve(); + }, () => { + return reject(); + }); }); }); } @@ -166,60 +168,52 @@ export class CorpusService { return new Promise((resolve, reject) => { this.availableCorpora = []; this.availableAuthors = []; - HelperService.config.pipe(take(1)).subscribe((config: object) => { - // get corpora from REST API - const url: string = config['backendBaseUrl'] + config['backendApiCorporaPath']; - const params: HttpParams = new HttpParams().set('last_update_time', lastUpdateTimeMS.toString()); - HelperService.makeGetRequest(this.http, this.toastCtrl, url, params).then((data: object) => { - if (data) { - const corpusList: CorpusMC[] = data['corpora'] as CorpusMC[]; - this.storage.set(config['localStorageKeyCorpora'], JSON.stringify(corpusList)).then(); - const updateInfo: object = {corpora: {lastAccessTime: new Date().getTime()}}; - this.storage.set(config['localStorageKeyUpdateInfo'], JSON.stringify(updateInfo)).then(); - this.processCorpora(corpusList); - return resolve(); - } else { - this.loadCorporaFromLocalStorage(); + // get corpora from REST API + const url: string = configMC.backendBaseUrl + configMC.backendApiCorporaPath; + const params: HttpParams = new HttpParams().set('last_update_time', lastUpdateTimeMS.toString()); + HelperService.makeGetRequest(this.http, this.toastCtrl, url, params, this.corporaUnavailableString).then((data: object) => { + if (data) { + const corpusList: CorpusMC[] = data['corpora'] as CorpusMC[]; + this.storage.set(configMC.localStorageKeyCorpora, JSON.stringify(corpusList)).then(); + this.storage.get(configMC.localStorageKeyUpdateInfo).then((jsonString: string) => { + const updateInfo: UpdateInfo = JSON.parse(jsonString) as UpdateInfo; + updateInfo.corpora = new Date().getTime(); + this.storage.set(configMC.localStorageKeyUpdateInfo, JSON.stringify(updateInfo)).then(); + }); + this.processCorpora(corpusList); + return resolve(); + } else { + this.loadCorporaFromLocalStorage().then(() => { return resolve(); - } - }, async (error: HttpErrorResponse) => { - this.loadCorporaFromLocalStorage(); - const toast = await this.toastCtrl.create({ - message: this.corporaUnavailableString, - duration: 3000, - position: 'top' }); - toast.present().then(); - return reject(error); - }); + } + }, async (error: HttpErrorResponse) => { + this.loadCorporaFromLocalStorage(); + return reject(error); }); }); } getCTStextPassage(urn: string): Promise { return new Promise(((resolve, reject) => { - HelperService.config.pipe(take(1)).subscribe((config: object) => { - const url: string = config['backendBaseUrl'] + config['backendApiRawtextPath']; - const params: HttpParams = new HttpParams().set('urn', urn); - HelperService.makeGetRequest(this.http, this.toastCtrl, url, params).then((ar: AnnisResponse) => { - return resolve(ar); - }, (error: HttpErrorResponse) => { - return reject(error); - }); + const url: string = configMC.backendBaseUrl + configMC.backendApiRawtextPath; + const params: HttpParams = new HttpParams().set('urn', urn); + HelperService.makeGetRequest(this.http, this.toastCtrl, url, params).then((ar: AnnisResponse) => { + return resolve(ar); + }, (error: HttpErrorResponse) => { + return reject(error); }); })); } getCTSvalidReff(urn: string): Promise { return new Promise((resolve, reject) => { - HelperService.config.pipe(take(1)).subscribe((config: object) => { - const fullUrl: string = config['backendBaseUrl'] + config['backendApiValidReffPath']; - const params: HttpParams = new HttpParams().set('urn', urn); - HelperService.makeGetRequest(this.http, this.toastCtrl, fullUrl, params).then((reff: string[]) => { - resolve(reff); - }, (error: HttpErrorResponse) => { - reject(error); - }); + const fullUrl: string = configMC.backendBaseUrl + configMC.backendApiValidReffPath; + const params: HttpParams = new HttpParams().set('urn', urn); + HelperService.makeGetRequest(this.http, this.toastCtrl, fullUrl, params).then((reff: string[]) => { + resolve(reff); + }, (error: HttpErrorResponse) => { + reject(error); }); }); } @@ -229,20 +223,18 @@ export class CorpusService { if (this.annisResponse.frequency_analysis.length) { return resolve(); } else { - HelperService.config.pipe(take(1)).subscribe((config: object) => { - const url: string = config['backendBaseUrl'] + config['backendApiFrequencyPath']; - const params: HttpParams = new HttpParams().set('urn', this.currentUrn); - HelperService.makeGetRequest(this.http, this.toastCtrl, url, params).then((fis: FrequencyItem[]) => { - HelperService.isLoading = false; - this.annisResponse.frequency_analysis = fis; - HelperService.applicationState.pipe(take(1)).subscribe((as: ApplicationState) => { - as.mostRecentSetup.annisResponse = this.annisResponse; - this.helperService.saveApplicationState(as).then(); - }); - return resolve(); - }, () => { - return reject(); + const url: string = configMC.backendBaseUrl + configMC.backendApiFrequencyPath; + const params: HttpParams = new HttpParams().set('urn', this.currentUrn); + HelperService.makeGetRequest(this.http, this.toastCtrl, url, params).then((fis: FrequencyItem[]) => { + HelperService.isLoading = false; + this.annisResponse.frequency_analysis = fis; + HelperService.applicationState.pipe(take(1)).subscribe((state: ApplicationState) => { + state.mostRecentSetup.annisResponse = this.annisResponse; + this.helperService.saveApplicationState(state).then(); }); + return resolve(); + }, () => { + return reject(); }); } }); @@ -344,17 +336,35 @@ export class CorpusService { }); } + initUpdateInfo(): Promise { + return new Promise(resolve => { + this.storage.get(configMC.localStorageKeyUpdateInfo).then((jsonString: string) => { + if (jsonString) { + return resolve(); + } + const ui: UpdateInfo = new UpdateInfo({ + corpora: new Date().getTime(), + exerciseList: new Date().getTime() + }); + this.storage.set(configMC.localStorageKeyUpdateInfo, JSON.stringify(ui)).then(() => { + return resolve(); + }); + }); + }); + } + isTreebank(corpus: CorpusMC) { return corpus.source_urn.includes('proiel'); } - public loadCorporaFromLocalStorage() { - HelperService.config.pipe(take(1)).subscribe((config: object) => { - this.storage.get(config['localStorageKeyCorpora']).then((jsonString: string) => { + public loadCorporaFromLocalStorage(): Promise { + return new Promise(resolve => { + this.storage.get(configMC.localStorageKeyCorpora).then((jsonString: string) => { if (jsonString) { const corpusList: CorpusMC[] = JSON.parse(jsonString) as CorpusMC[]; this.processCorpora(corpusList); } + return resolve(); }); }); } diff --git a/src/app/exercise-list/exercise-list.page.html b/src/app/exercise-list/exercise-list.page.html index 4bf6de2..dcac6e1 100644 --- a/src/app/exercise-list/exercise-list.page.html +++ b/src/app/exercise-list/exercise-list.page.html @@ -54,8 +54,8 @@