Skip to content

Commit

Permalink
fix(cache): Images not saved to cache (#171)
Browse files Browse the repository at this point in the history
* fix(wkwebview): Images not loading in Ionic WKWebView

* fix(livereload): Images not loading

When running in an emulator or in a device with the flag --livereload images were not showing.

* lint(livereload): Updated for ihadeed comments

* fix(livereload): Using reject instead of resolve

Reject caused an error and did not return the URL. Fixed this resolving and returning the image URL back to the promise.

* fix(cache): Images not saved to cache

If you didn't set a maxCacheSize, images won't be stored in the cache and will not load in your component.

Changed shouldIndex property based in @felixbroehl modifications.

* fix(cache): Removed unused check cache space func
  • Loading branch information
eduardoRoth authored and ihadeed committed Jul 12, 2018
1 parent 2022b9c commit 07eb2e7
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/providers/image-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export class ImageLoader {

private indexed: boolean = false;

private get shouldIndex(): boolean {
return (this.config.maxCacheAge > -1) || (this.config.maxCacheSize > -1);
private get isCacheSpaceExceeded(): boolean {
return this.config.maxCacheSize > -1 && this.currentCacheSize > this.config.maxCacheSize;
}

private get isWKWebView(): boolean {
Expand All @@ -76,9 +76,9 @@ export class ImageLoader {
private get isIonicWKWebView(): boolean {
return this.isWKWebView && (location.host === 'localhost:8080' || (<any>window).LiveReload);
}

private get isDevServer() : boolean {
return (window['IonicDevServer'] != undefined);
return (window['IonicDevServer'] != undefined);
}

constructor(
Expand Down Expand Up @@ -289,16 +289,17 @@ export class ImageLoader {
}).subscribe(
(data: Blob) => {
this.file.writeFile(localDir, fileName, data, {replace: true}).then((file: FileEntry) => {
if (this.shouldIndex) {
this.addFileToIndex(file).then(() => {
this.getCachedImagePath(currentItem.imageUrl).then((localUrl) => {
currentItem.resolve(localUrl);
resolve();
done();
this.maintainCacheSize();
});
});
if (this.isCacheSpaceExceeded) {
this.maintainCacheSize();
}
this.addFileToIndex(file).then(() => {
this.getCachedImagePath(currentItem.imageUrl).then((localUrl) => {
currentItem.resolve(localUrl);
resolve();
done();
this.maintainCacheSize();
});
});
}).catch((e) => {
//Could not write image
error(e);
Expand Down Expand Up @@ -383,9 +384,6 @@ export class ImageLoader {
*/
private indexCache(): Promise<void> {

// only index if needed, to save resources
if (!this.shouldIndex) return Promise.resolve();

this.cacheIndex = [];

return this.file.listDir(this.file.cacheDirectory, this.config.cacheDirectoryName)
Expand Down Expand Up @@ -468,7 +466,7 @@ export class ImageLoader {
if (!this.isCacheReady) {
return reject();
}

// if we're running with livereload, ignore cache and call the resource from it's URL
if(this.isDevServer){
return resolve(url);
Expand Down

0 comments on commit 07eb2e7

Please sign in to comment.