Skip to content

Commit 50b6b7b

Browse files
committed
feat(): use ion-spinner and ionic native v3
1 parent 16dd4e3 commit 50b6b7b

File tree

4 files changed

+36
-40
lines changed

4 files changed

+36
-40
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@
3131
"@angular/compiler": "2.4.8",
3232
"@angular/compiler-cli": "2.4.8",
3333
"@angular/core": "2.4.8",
34+
"@angular/forms": "2.4.8",
35+
"@angular/http": "2.4.8",
3436
"@angular/platform-browser": "2.4.8",
3537
"@ionic-native/core": "^3.1.1",
3638
"@ionic-native/file": "^3.1.1",
3739
"@ionic-native/transfer": "^3.1.1",
3840
"@types/lodash": "4.14.50",
3941
"conventional-changelog-cli": "1.2.0",
42+
"ionic-angular": "^2.2.0",
4043
"rxjs": "5.0.1",
4144
"typescript": "2.0.9",
4245
"zone.js": "0.7.2"

src/components/img-loader/img-loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ImageLoaderConfig } from '../../providers/image-loader-config';
44

55
@Component({
66
selector: 'img-loader',
7-
template: '<image-loader-spinner *ngIf="spinner && isLoading"></image-loader-spinner>'
7+
template: '<ion-spinner *ngIf="spinner && isLoading"></ion-spinner>'
88
})
99
export class ImgLoader implements OnInit {
1010

src/image-loader.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ImageLoader } from './providers/image-loader';
44
import { ImageLoaderConfig } from './providers/image-loader-config';
55
import { ImageLoaderSpinner } from './components/image-loader-spinner/image-loader-spinner';
66
import { BrowserModule } from '@angular/platform-browser';
7+
import { IonicModule } from 'ionic-angular';
78

89
@NgModule({
910
declarations: [
@@ -15,7 +16,8 @@ import { BrowserModule } from '@angular/platform-browser';
1516
ImageLoader
1617
],
1718
imports: [
18-
BrowserModule
19+
BrowserModule,
20+
IonicModule
1921
],
2022
exports: [
2123
ImgLoader

src/providers/image-loader.ts

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
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';
34
import { ImageLoaderConfig } from "./image-loader-config";
5+
import { Platform } from 'ionic-angular';
46
import * as _ from 'lodash';
57

6-
declare var cordova: any;
7-
88
@Injectable()
99
export class ImageLoader {
1010

@@ -50,20 +50,28 @@ export class ImageLoader {
5050

5151
private indexed: boolean = false;
5252

53-
private get shouldIndex() {
53+
private get shouldIndex(): boolean {
5454
return (this.config.maxCacheAge > -1) || (this.config.maxCacheSize > -1);
5555
}
5656

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+
) {
5867
if (window.location.protocol === 'http:' || window.location.protocol === 'https:') {
5968
// we are running on a browser, or using livereload
6069
// plugin will not function in this case
6170
this.isInit = true;
6271
this.throwWarning('You are running on a browser or using livereload, IonicImageLoader will not function, falling back to browser loading.');
6372
} else {
64-
document.addEventListener('deviceready', () => {
65-
this.initCache();
66-
}, false);
73+
this.platform.ready()
74+
.then(() => this.initCache());
6775
}
6876
}
6977

@@ -81,7 +89,7 @@ export class ImageLoader {
8189
*/
8290
clearCache(): void {
8391

84-
if (typeof cordova === 'undefined') return;
92+
if (!this.platform.is('cordova')) return;
8593

8694
const clear = () => {
8795

@@ -94,7 +102,7 @@ export class ImageLoader {
94102
// pause any operations
95103
this.isInit = false;
96104

97-
File.removeRecursively(cordova.file.cacheDirectory, this.config.cacheDirectoryName)
105+
this.file.removeRecursively(this.file.cacheDirectory, this.config.cacheDirectoryName)
98106
.then(() => {
99107
this.initCache(true);
100108
})
@@ -113,7 +121,7 @@ export class ImageLoader {
113121
* @returns {Promise<any>} Returns a promise that resolves when the download is complete, or rejects on error.
114122
*/
115123
private downloadImage(imageUrl: string, localPath: string): Promise<any> {
116-
let transfer = new Transfer();
124+
const transfer = this.transfer.create();
117125
return transfer.download(imageUrl, localPath, true);
118126
}
119127

@@ -209,7 +217,7 @@ export class ImageLoader {
209217
this.processQueue();
210218
};
211219

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);
213221
this.downloadImage(currentItem.imageUrl, localPath)
214222
.then((file: FileEntry) => {
215223

@@ -238,11 +246,6 @@ export class ImageLoader {
238246

239247
this.concurrency = this.config.concurrency;
240248

241-
if (!this.filePluginExists) {
242-
this.isInit = true;
243-
return;
244-
}
245-
246249
this.cacheDirectoryExists
247250
.catch(() => {
248251
// doesn't exist
@@ -277,7 +280,7 @@ export class ImageLoader {
277280
&& (Date.now() - metadata.modificationTime.getTime()) > this.config.maxCacheAge
278281
) {
279282
// 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);
281284
} else {
282285

283286
// file age doesn't exceed maximum cache age, or maximum cache age isn't set
@@ -308,7 +311,7 @@ export class ImageLoader {
308311

309312
this.cacheIndex = [];
310313

311-
return File.listDir(cordova.file.cacheDirectory, this.config.cacheDirectoryName)
314+
return this.file.listDir(this.file.cacheDirectory, this.config.cacheDirectoryName)
312315
.then(files => Promise.all(files.map(this.addFileToIndex.bind(this))))
313316
.then(() => {
314317
this.cacheIndex = _.sortBy(this.cacheIndex, 'modificationTime');
@@ -333,7 +336,7 @@ export class ImageLoader {
333336
// we exceeded max cache size
334337
while (this.currentCacheSize > this.config.maxCacheSize) {
335338
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);
337340
this.currentCacheSize -= file.size;
338341
}
339342

@@ -358,18 +361,18 @@ export class ImageLoader {
358361
let fileName = this.createFileName(url);
359362

360363
// get full path
361-
let dirPath = cordova.file.cacheDirectory + this.config.cacheDirectoryName;
364+
let dirPath = this.file.cacheDirectory + this.config.cacheDirectoryName;
362365

363366
// check if exists
364-
File.resolveLocalFilesystemUrl(dirPath + '/' + fileName)
367+
this.file.resolveLocalFilesystemUrl(dirPath + '/' + fileName)
365368
.then((fileEntry: FileEntry) => {
366369
// file exists in cache
367370

368371
// now check if iOS device & using WKWebView Engine
369-
if (cordova.platformId == 'ios' && (<any>window).webkit) {
372+
if (this.isWKWebView) {
370373

371374
// Read FileEntry and return as data url
372-
fileEntry.file((file: Blob) => {
375+
fileEntry.file((file: any) => {
373376
const reader = new FileReader();
374377

375378
reader.onloadend = function() {
@@ -411,24 +414,12 @@ export class ImageLoader {
411414
}
412415
}
413416

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-
426417
/**
427418
* Check if the cache directory exists
428419
* @returns {Promise<boolean|FileError>} Returns a promise that resolves if exists, and rejects if it doesn't
429420
*/
430421
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);
432423
}
433424

434425
/**
@@ -437,7 +428,7 @@ export class ImageLoader {
437428
* @returns {Promise<DirectoryEntry|FileError>} Returns a promise that resolves if the directory was created, and rejects on error
438429
*/
439430
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);
441432
}
442433

443434
/**

0 commit comments

Comments
 (0)