import { Injectable } from '@angular/core' ; import { Storage } from '@ionic/storage' ; import { Subject } from 'rxjs' ; import { ScanSessionModel } from '../models/scan-session.model' ; /**
 * Provides methods to store and get data from the storage
 * It implements a memory cache for the main scanSessions.
 * TODO:
 *  - Remove JSON.stringify/parse, this job should be done with Storage by passing the type inside <>
 *  - Save the scan sessions in separate storage items instead of putting everything in the same object
 */ @ Injectable ( ) export class ScanSessionsStorage { private static SCAN_SESSIONS = "scan_sessions" ; private static ARCHIVED_SCAN_SESSIONS = 'archived_scan_sessions' ; private scanSessionsCache : ScanSessionModel [ ] = null ; private onScanSessionSetObservable = new Subject < ScanSessionModel [ ] > ( ) ; constructor ( public storage ) { this . onScanSessionSetObservable . debounceTime ( 500 ) . subscribe ( ( scanSessions ) => { this . storage . set ( ScanSessionsStorage . SCAN_SESSIONS , JSON . stringify ( scanSessions ) ) ; } ) ; } setScanSessions ( scanSessions ) { this . scanSessionsCache = scanSessions ; this . onScanSessionSetObservable . next ( scanSessions ) ; } getScanSessions ( ) { return new Promise ( ( resolve , reject ) => { if ( this . scanSessionsCache != null ) { resolve ( this . scanSessionsCache ) ; return ; } this . storage . get ( ScanSessionsStorage . SCAN_SESSIONS ) . then ( ( data ) => { if ( data != null ) { let json = JSON . parse ( data ) ; let result = json . map ( x => { let scanSession = { id : x . id , name : x . name , date : x . date , scannings : x . scannings , selected : false , syncedWith : x . syncedWith || [ ] } ; return scanSession ; } ) ; this . scanSessionsCache = result ; resolve ( result ) ; } else { resolve ( [ ] ) ; } } ) ; } ) ; } getNextScanSessionNumber ( ) { return this . getScanSessions ( ) . then ( scanSessions => scanSessions . length + 1 ) ; } updateScanSession ( scanSession ) { return this . getScanSessions ( ) . then ( sessions => { let existingSessionIndex = sessions . findIndex ( ( x ) => x . id == scanSession . id ) ; if ( existingSessionIndex == - 1 ) { sessions . unshift ( scanSession ) ; } else { sessions [ existingSessionIndex ] = scanSession ; } this . setScanSessions ( sessions ) ; } ) ; } setArchivedScanSessions ( scanSessions ) { return this . storage . set ( ScanSessionsStorage . ARCHIVED_SCAN_SESSIONS , JSON . stringify ( scanSessions ) ) ; } getArchivedScanSessions ( ) { return new Promise ( ( resolve , reject ) => { this . storage . get ( ScanSessionsStorage . ARCHIVED_SCAN_SESSIONS ) . then ( ( data ) => { if ( data != null ) { let json = JSON . parse ( data ) ; let result = json . map ( x => { let scanSession = { id : x . id , name : x . name , date : x . date , scannings : x . scannings , selected : false , syncedWith : x . syncedWith || [ ] } ; return scanSession ; } ) ; resolve ( result ) ; } else { resolve ( [ ] ) ; } } ) ; } ) ; } pushArchivedScanSessions ( newSessions ) { return this . getArchivedScanSessions ( ) . then ( sessions => { if ( ! sessions ) sessions = [ ] ; sessions = newSessions . concat ( sessions ) ; this . setArchivedScanSessions ( sessions ) ; } ) ; } pushArchivedScanSession ( newSession ) { return this . getArchivedScanSessions ( ) . then ( sessions => { if ( ! sessions ) sessions = [ ] ; sessions . unshift ( newSession ) ; this . setArchivedScanSessions ( sessions ) ; } ) ; } }