import { Injectable } from '@angular/core' ; import { Storage } from "@ionic/storage" ; import { File } from "@ionic-native/file" ; import _ from "lodash" ; import { Novel } from '../common/models/novel' ; import { Transfer , TransferObject } from "@ionic-native/transfer" ; @ Injectable ( ) export class NovelsLocalService { private NOVELS : string = "novels" ; private coversDir : string = "covers" ; fileTransfer : TransferObject ; constructor ( private storage , private file , private transfer ) { console . log ( 'Hello Novels Local Service' ) ; this . fileTransfer = this . transfer . create ( ) ; } get ( ) { return new Promise ( ( resolve , reject ) => { this . storage . get ( this . NOVELS ) . then ( novels => { resolve ( novels || [ ] ) ; } ) . catch ( ( ) => { resolve ( [ ] ) ; } ) ; } ) ; } set ( novels ) { this . storage . set ( this . NOVELS , novels ) ; } add ( novel ) { return new Promise ( ( resolve , reject ) => { this . isInList ( novel ) . then ( ( ) => resolve ( ) ) . catch ( ( ) => { this . get ( ) . then ( ( novels ) => { novels . push ( novel ) ; this . set ( novels ) ; } ) ; this . saveImage ( novel ) ; } ) ; } ) ; } saveImage ( novel ) { let coversDir = this . file . dataDirectory ; return new Promise ( ( resolve , reject ) => { this . createDir ( ) . then ( ( ) => { this . fileTransfer . download ( `/api/covers/${ novel . cover }` , `${ coversDir }${ this . coversDir }/${ novel . cover }` ) . then ( entry => { console . log ( "download image complete: " , entry . toURL ( ) ) ; } ) . catch ( err => { console . log ( "error downloading image" , err ) ; } ) ; } ) . catch ( err => { console . log ( "error creating root directory" , err ) ; } ) ; } ) ; } private createDir ( ) { let rootDir = this . file . dataDirectory ; return new Promise ( ( resolve , reject ) => { this . file . checkDir ( rootDir , this . coversDir ) . then ( ( ) => { resolve ( ) ; } ) . catch ( ( ) => { this . file . createDir ( rootDir , this . coversDir , false ) . then ( ( ) => { resolve ( ) ; } ) . catch ( ( ) => { reject ( ) ; } ) ; } ) ; } ) ; } remove ( novel ) { return new Promise ( ( resolve , reject ) => { this . get ( ) . then ( ( novels ) => { _ . novels . pull ( novel ) ; this . set ( novels ) ; } ) ; } ) ; } isInList ( novel ) { return new Promise ( ( resolve , reject ) => { this . get ( ) . then ( novels => { let inList = _ . some ( novels , novel ) ; if ( inList ) { resolve ( ) ; } else { reject ( ) ; } } ) . catch ( ( ) => { reject ( ) ; } ) ; } ) ; } getNovel ( novelId ) { console . log ( "NovelsLocalService::getNovel" , novelId ) ; return new Promise ( ( resolve , reject ) => { this . get ( ) . then ( novels => { let found = _ . find ( novels , novel => novel . id == novelId ) ; if ( found ) { resolve ( found ) ; } else { reject ( ) ; } } ) . catch ( ( ) => { reject ( ) ; } ) ; } ) ; } getNovels ( novelIds ) { console . log ( "NovelsLocalService::getNovels" , novelIds ) ; return new Promise ( ( resolve , reject ) => { this . get ( ) . then ( novels => { let filtered = _ . filter ( novels , ( novel ) => { return _ . includes ( novelIds , novel . id ) ; } ) ; filtered = this . sortByOriginal ( novelIds , filtered ) ; filtered = _ . filter ( filtered , novel => ! ! novel ) ; resolve ( filtered ) ; } ) ; } ) ; } private sortByOriginal ( novelIds , novels ) { return _ . map ( novelIds , id => { return _ . find ( novels , novel => novel . id === id ) ; } ) ; } }