import { Settings } from '../../modules/settings/settings.model' ; import { Injectable } from '@angular/core' ; import { LocalStorage } from '@ngx-pwa/local-storage' ; import { NotificationsService } from '../notifications/notifications.service' ; import { ProjectStorage , SourceStorage , FacsimileStorage } from './storage.model' ; import { projectListStorageSchema , projectStorageSchema , settingsStorageSchema , sourceStorageSchema , facsimileStorageSchema } from './storage.schema' ; import { TranslateService } from '@ngx-translate/core' ; @ Injectable ( ) export class StorageService { constructor ( private storage , private notificationsService , private translate ) { } loadProjectList ( ) { return this . storage . getItem ( 'project-list' , projectListStorageSchema ) . toPromise ( ) . then ( ( data ) => { if ( ! data ) { return this . storage . setItem ( 'project-list' , [ ] ) . toPromise ( ) . then ( success => [ ] , err => { this . errorHandler ( err ) ; return [ ] ; } ) ; } return data ; } , err => { this . errorHandler ( err ) ; return [ ] ; } ) ; } saveProjectList ( ids ) { return this . storage . setItem ( 'project-list' , ids ) . toPromise ( ) . catch ( err => { this . errorHandler ( err ) ; return false ; } ) ; } loadProject ( id ) { return this . storage . getItem ( `project:${ id }` , projectStorageSchema ) . toPromise ( ) . then ( ( project ) => { if ( project ) { return project ; } this . errorHandler ( this . translate . instant ( 'CORE.STORAGE.ERROR.PROJECT' , { id : id } ) ) ; return null ; } , err => { this . errorHandler ( err ) ; return null ; } ) ; } saveProject ( project ) { return this . storage . setItem ( `project:${ project . id }` , project ) . toPromise ( ) . catch ( err => { this . errorHandler ( err ) ; return false ; } ) ; } deleteProject ( id ) { return this . storage . removeItem ( `project:${ id }` ) . toPromise ( ) . catch ( err => { this . errorHandler ( err ) ; return false ; } ) ; } loadSource ( id ) { return this . storage . getItem ( `source:${ id }` , sourceStorageSchema ) . toPromise ( ) . then ( ( source ) => { if ( source ) { return source ; } this . errorHandler ( this . translate . instant ( 'CORE.STORAGE.ERROR.SOURCE' , { id : id } ) ) ; return null ; } , err => { this . errorHandler ( err ) ; return null ; } ) ; } saveSource ( source ) { return this . storage . setItem ( `source:${ source . id }` , source ) . toPromise ( ) . catch ( err => { this . errorHandler ( err ) ; return false ; } ) ; } deleteSource ( id ) { return this . storage . removeItem ( `source:${ id }` ) . toPromise ( ) . catch ( err => { this . errorHandler ( err ) ; return false ; } ) ; } loadFacsimile ( id ) { return this . storage . getItem ( `facsimile:${ id }` , facsimileStorageSchema ) . toPromise ( ) . then ( ( facsimile ) => { if ( facsimile ) { return facsimile ; } this . errorHandler ( this . translate . instant ( 'CORE.STORAGE.ERROR.FACSIMILIE' , { id : id } ) ) ; return null ; } , err => { this . errorHandler ( err ) ; return null ; } ) ; } saveFacsimile ( facsimile ) { return this . storage . setItem ( `facsimile:${ facsimile . id }` , facsimile ) . toPromise ( ) . catch ( err => { this . errorHandler ( err ) ; return false ; } ) ; } deleteFacsimile ( id ) { return this . storage . removeItem ( `facsimile:${ id }` ) . toPromise ( ) . catch ( err => { this . errorHandler ( err ) ; return false ; } ) ; } loadSettings ( ) { return this . storage . getItem ( `settings` , settingsStorageSchema ) . toPromise ( ) . then ( ( settings ) => { if ( settings ) { return settings ; } return null ; } , err => { this . errorHandler ( err ) ; return null ; } ) ; } saveSettings ( settings ) { return this . storage . setItem ( `settings` , settings ) . toPromise ( ) . catch ( err => { this . errorHandler ( err ) ; return false ; } ) ; } private errorHandler ( err ) { this . notificationsService . error ( this . translate . instant ( 'CORE.STORAGE.ERROR.STORAGE' ) + err ) ; console . warn ( this . translate . instant ( 'CORE.STORAGE.ERROR.STORAGE' ) , err ) ; } }