diff --git a/packages/geo/src/lib/offline/geoDB/configFileToGeoDB.service.ts b/packages/geo/src/lib/offline/geoDB/configFileToGeoDB.service.ts new file mode 100644 index 0000000000..6f162e4ea1 --- /dev/null +++ b/packages/geo/src/lib/offline/geoDB/configFileToGeoDB.service.ts @@ -0,0 +1,82 @@ +import { Injectable } from '@angular/core'; +import { MessageService } from '@igo2/core'; +import { HttpClient } from '@angular/common/http'; +import { catchError, concatMap } from 'rxjs/operators'; +import { GeoDBService } from './geoDB.service'; +import { of, zip } from 'rxjs'; +import { DatasToIDB, GeoDBData } from './geoDB.interface'; + +@Injectable({ + providedIn: 'root' +}) +export class ConfigFileToGeoDBService { + + + constructor( + private http: HttpClient, + private geoDBService: GeoDBService, + private messageService: MessageService + ) { } + + + load(url: string) { + let downloadMessage; + this.http.get(url).pipe( + + catchError((error: any): any => { + console.log(`GeoData file ${url} could not be read`); + error.error.caught = true; + throw error; + }), + concatMap((datasToIDB: DatasToIDB) => { + const datas$ = []; + let firstDownload = true; + if (datasToIDB?.geoDatas) { + const currentDate = new Date(); + datasToIDB?.geoDatas.map((geoData) => { + if (typeof geoData.triggerDate === 'string') { + geoData.triggerDate = new Date(Date.parse(geoData.triggerDate.replace(/-/g, ' '))); + } + if (currentDate >= geoData.triggerDate) { + if (geoData.action === 'update') { + const insertEvent = `${geoData.source || 'automatedDataUpdate'} (${geoData.triggerDate})`; + geoData.urls.map((url) => { + datas$.push( + this.geoDBService.getByID(url).pipe(concatMap((res: GeoDBData) => { + if (res?.insertEvent !== insertEvent) { + if (firstDownload) { + downloadMessage = this.messageService + .info('igo.geo.indexedDb.data-download-start', undefined, + { disableTimeOut: true, progressBar: false, closeButton: true, tapToDismiss: false }); + firstDownload = false; + } + return this.http.get(url) + .pipe(concatMap(r => this.geoDBService.update(url, url as any, r, 'system' as any, insertEvent))); + } else { + return of(false); + } + })) + ); + }); + } else if (geoData.action === 'delete') { + geoData.urls.map((url) => { + datas$.push(this.geoDBService.deleteByKey(url)); + }); + } + } + }); + } + return zip(...datas$); + }) + ).subscribe(() => { + if (downloadMessage) { + setTimeout(() => { + this.messageService.remove((downloadMessage as any).toastId); + this.messageService.success('igo.geo.indexedDb.data-download-completed', undefined, { timeOut: 40000 }); + }, 2500); + } + }); + } + + +} diff --git a/packages/geo/src/lib/offline/geoDB/geoDB.interface.ts b/packages/geo/src/lib/offline/geoDB/geoDB.interface.ts index 2b16432ad0..05b4e2ed1c 100644 --- a/packages/geo/src/lib/offline/geoDB/geoDB.interface.ts +++ b/packages/geo/src/lib/offline/geoDB/geoDB.interface.ts @@ -2,9 +2,20 @@ import { InsertSourceInsertDBEnum } from "./geoDB.enums"; export interface GeoDBData { url: string; - regionID: number; + regionID: any; object: any; compressed: boolean; insertSource: InsertSourceInsertDBEnum; insertEvent: string; } + +export interface GeoDataToIDB { + triggerDate: Date | string; + action: "delete" | 'update'; + urls: string[]; + source?: string; +} + +export interface DatasToIDB { + geoDatas: GeoDataToIDB[] +} diff --git a/packages/geo/src/lib/offline/geoDB/geoDB.service.ts b/packages/geo/src/lib/offline/geoDB/geoDB.service.ts index 4060a2babe..1c7b106306 100644 --- a/packages/geo/src/lib/offline/geoDB/geoDB.service.ts +++ b/packages/geo/src/lib/offline/geoDB/geoDB.service.ts @@ -28,7 +28,7 @@ export class GeoDBService { * @param insertEvent Name of the event where the insert has been triggered * @returns */ - update(url: string, regionID: number, object: any, insertSource: InsertSourceInsertDBEnum, insertEvent: string): Observable { + update(url: string, regionID: any, object: any, insertSource: InsertSourceInsertDBEnum, insertEvent: string): Observable { if (!object) { return; } @@ -108,6 +108,14 @@ export class GeoDBService { ); } + getByID(url: string): Observable { + return this.ngxIndexedDBService.getByID(this.dbName, url); + } + + deleteByKey(url: string): Observable { + return this.ngxIndexedDBService.deleteByKey(this.dbName, url); + } + getRegionTileCountByID(id: number): Observable { const subject: Subject = new Subject(); const dbRequest = this.getRegionByID(id) diff --git a/packages/geo/src/lib/offline/geoDB/index.ts b/packages/geo/src/lib/offline/geoDB/index.ts index 872b724d1f..7a985cecba 100644 --- a/packages/geo/src/lib/offline/geoDB/index.ts +++ b/packages/geo/src/lib/offline/geoDB/index.ts @@ -1,3 +1,4 @@ export * from './geoDB.enums'; export * from './geoDB.interface'; export * from './geoDB.service'; +export * from './configFileToGeoDB.service'; diff --git a/packages/geo/src/locale/en.geo.json b/packages/geo/src/locale/en.geo.json index 1c08b282fc..196d6b33bc 100644 --- a/packages/geo/src/locale/en.geo.json +++ b/packages/geo/src/locale/en.geo.json @@ -54,7 +54,7 @@ }, "unreadable": { "text": "The file '{{value}}' is unreadable", - "title": "Unreadable file" + "title": "Unreadable file" }, "tooLarge": { "text": "The file '{{value}}' is too large (Max: {{size}} MB)", @@ -746,6 +746,10 @@ "link": { "message": "Hyperlink copied to clipboard" } + }, + "indexedDb": { + "data-download-start": "Downloading data. Please don't leave the current app.", + "data-download-completed": "Data download completed" } } } diff --git a/packages/geo/src/locale/fr.geo.json b/packages/geo/src/locale/fr.geo.json index 7b345459ea..26ad3850d5 100644 --- a/packages/geo/src/locale/fr.geo.json +++ b/packages/geo/src/locale/fr.geo.json @@ -746,6 +746,10 @@ "link": { "message": "Le lien a été copié dans le presse-papier" } + }, + "indexedDb": { + "data-download-start": "Téléchargement des données. Veuillez ne pas quitter.", + "data-download-completed": "Téléchargement des données terminée." } } }