1
1
import { Injectable } from '@angular/core' ;
2
- import { File , FileEntry , FileReader , Transfer } from 'ionic-native' ;
2
+ import { File , FileEntry , FileReader } from '@ionic-native/file' ;
3
+ import { Transfer } from '@ionic-native/transfer' ;
3
4
import { ImageLoaderConfig } from "./image-loader-config" ;
5
+ import { Platform } from 'ionic-angular' ;
4
6
import * as _ from 'lodash' ;
5
7
6
- declare var cordova : any ;
7
-
8
8
@Injectable ( )
9
9
export class ImageLoader {
10
10
@@ -50,20 +50,28 @@ export class ImageLoader {
50
50
51
51
private indexed : boolean = false ;
52
52
53
- private get shouldIndex ( ) {
53
+ private get shouldIndex ( ) : boolean {
54
54
return ( this . config . maxCacheAge > - 1 ) || ( this . config . maxCacheSize > - 1 ) ;
55
55
}
56
56
57
- constructor ( private config : ImageLoaderConfig ) {
57
+ private get isWKWebView ( ) : boolean {
58
+ return this . platform . is ( 'ios' ) && ( < any > window ) . webkit ;
59
+ }
60
+
61
+ constructor (
62
+ private config : ImageLoaderConfig ,
63
+ private file : File ,
64
+ private transfer : Transfer ,
65
+ private platform : Platform
66
+ ) {
58
67
if ( window . location . protocol === 'http:' || window . location . protocol === 'https:' ) {
59
68
// we are running on a browser, or using livereload
60
69
// plugin will not function in this case
61
70
this . isInit = true ;
62
71
this . throwWarning ( 'You are running on a browser or using livereload, IonicImageLoader will not function, falling back to browser loading.' ) ;
63
72
} else {
64
- document . addEventListener ( 'deviceready' , ( ) => {
65
- this . initCache ( ) ;
66
- } , false ) ;
73
+ this . platform . ready ( )
74
+ . then ( ( ) => this . initCache ( ) ) ;
67
75
}
68
76
}
69
77
@@ -81,7 +89,7 @@ export class ImageLoader {
81
89
*/
82
90
clearCache ( ) : void {
83
91
84
- if ( typeof cordova === 'undefined' ) return ;
92
+ if ( ! this . platform . is ( ' cordova' ) ) return ;
85
93
86
94
const clear = ( ) => {
87
95
@@ -94,7 +102,7 @@ export class ImageLoader {
94
102
// pause any operations
95
103
this . isInit = false ;
96
104
97
- File . removeRecursively ( cordova . file . cacheDirectory , this . config . cacheDirectoryName )
105
+ this . file . removeRecursively ( this . file . cacheDirectory , this . config . cacheDirectoryName )
98
106
. then ( ( ) => {
99
107
this . initCache ( true ) ;
100
108
} )
@@ -113,7 +121,7 @@ export class ImageLoader {
113
121
* @returns {Promise<any> } Returns a promise that resolves when the download is complete, or rejects on error.
114
122
*/
115
123
private downloadImage ( imageUrl : string , localPath : string ) : Promise < any > {
116
- let transfer = new Transfer ( ) ;
124
+ const transfer = this . transfer . create ( ) ;
117
125
return transfer . download ( imageUrl , localPath , true ) ;
118
126
}
119
127
@@ -209,7 +217,7 @@ export class ImageLoader {
209
217
this . processQueue ( ) ;
210
218
} ;
211
219
212
- let localPath = cordova . file . cacheDirectory + this . config . cacheDirectoryName + '/' + this . createFileName ( currentItem . imageUrl ) ;
220
+ let localPath = this . file . cacheDirectory + this . config . cacheDirectoryName + '/' + this . createFileName ( currentItem . imageUrl ) ;
213
221
this . downloadImage ( currentItem . imageUrl , localPath )
214
222
. then ( ( file : FileEntry ) => {
215
223
@@ -238,11 +246,6 @@ export class ImageLoader {
238
246
239
247
this . concurrency = this . config . concurrency ;
240
248
241
- if ( ! this . filePluginExists ) {
242
- this . isInit = true ;
243
- return ;
244
- }
245
-
246
249
this . cacheDirectoryExists
247
250
. catch ( ( ) => {
248
251
// doesn't exist
@@ -277,7 +280,7 @@ export class ImageLoader {
277
280
&& ( Date . now ( ) - metadata . modificationTime . getTime ( ) ) > this . config . maxCacheAge
278
281
) {
279
282
// file age exceeds maximum cache age
280
- return File . removeFile ( cordova . file . cacheDirectory + this . config . cacheDirectoryName , file . name ) ;
283
+ return this . file . removeFile ( this . file . cacheDirectory + this . config . cacheDirectoryName , file . name ) ;
281
284
} else {
282
285
283
286
// file age doesn't exceed maximum cache age, or maximum cache age isn't set
@@ -308,7 +311,7 @@ export class ImageLoader {
308
311
309
312
this . cacheIndex = [ ] ;
310
313
311
- return File . listDir ( cordova . file . cacheDirectory , this . config . cacheDirectoryName )
314
+ return this . file . listDir ( this . file . cacheDirectory , this . config . cacheDirectoryName )
312
315
. then ( files => Promise . all ( files . map ( this . addFileToIndex . bind ( this ) ) ) )
313
316
. then ( ( ) => {
314
317
this . cacheIndex = _ . sortBy ( this . cacheIndex , 'modificationTime' ) ;
@@ -333,7 +336,7 @@ export class ImageLoader {
333
336
// we exceeded max cache size
334
337
while ( this . currentCacheSize > this . config . maxCacheSize ) {
335
338
let file = this . cacheIndex . splice ( 0 , 1 ) [ 0 ] ;
336
- File . removeFile ( cordova . file . cacheDirectory + this . config . cacheDirectoryName , file . name ) ;
339
+ this . file . removeFile ( this . file . cacheDirectory + this . config . cacheDirectoryName , file . name ) ;
337
340
this . currentCacheSize -= file . size ;
338
341
}
339
342
@@ -358,18 +361,18 @@ export class ImageLoader {
358
361
let fileName = this . createFileName ( url ) ;
359
362
360
363
// get full path
361
- let dirPath = cordova . file . cacheDirectory + this . config . cacheDirectoryName ;
364
+ let dirPath = this . file . cacheDirectory + this . config . cacheDirectoryName ;
362
365
363
366
// check if exists
364
- File . resolveLocalFilesystemUrl ( dirPath + '/' + fileName )
367
+ this . file . resolveLocalFilesystemUrl ( dirPath + '/' + fileName )
365
368
. then ( ( fileEntry : FileEntry ) => {
366
369
// file exists in cache
367
370
368
371
// now check if iOS device & using WKWebView Engine
369
- if ( cordova . platformId == 'ios' && ( < any > window ) . webkit ) {
372
+ if ( this . isWKWebView ) {
370
373
371
374
// Read FileEntry and return as data url
372
- fileEntry . file ( ( file : Blob ) => {
375
+ fileEntry . file ( ( file : any ) => {
373
376
const reader = new FileReader ( ) ;
374
377
375
378
reader . onloadend = function ( ) {
@@ -411,24 +414,12 @@ export class ImageLoader {
411
414
}
412
415
}
413
416
414
- /**
415
- * Check if file plugin exists
416
- * @returns {boolean } returns a boolean that indicates whether the plugin exists
417
- */
418
- private get filePluginExists ( ) : boolean {
419
- if ( ! cordova || ! cordova . file ) {
420
- this . throwWarning ( 'Unable to find the cordova file plugin. ImageLoader will not cache images.' ) ;
421
- return false ;
422
- }
423
- return true ;
424
- }
425
-
426
417
/**
427
418
* Check if the cache directory exists
428
419
* @returns {Promise<boolean|FileError> } Returns a promise that resolves if exists, and rejects if it doesn't
429
420
*/
430
421
private get cacheDirectoryExists ( ) : Promise < boolean > {
431
- return < Promise < boolean > > File . checkDir ( cordova . file . cacheDirectory , this . config . cacheDirectoryName ) ;
422
+ return this . file . checkDir ( this . file . cacheDirectory , this . config . cacheDirectoryName ) ;
432
423
}
433
424
434
425
/**
@@ -437,7 +428,7 @@ export class ImageLoader {
437
428
* @returns {Promise<DirectoryEntry|FileError> } Returns a promise that resolves if the directory was created, and rejects on error
438
429
*/
439
430
private createCacheDirectory ( replace : boolean = false ) : Promise < any > {
440
- return File . createDir ( cordova . file . cacheDirectory , this . config . cacheDirectoryName , replace ) ;
431
+ return this . file . createDir ( this . file . cacheDirectory , this . config . cacheDirectoryName , replace ) ;
441
432
}
442
433
443
434
/**
0 commit comments