Skip to content

Commit

Permalink
fix: Fix local resource cache issue on Android. (#472)
Browse files Browse the repository at this point in the history
fix #402
  • Loading branch information
patrickkempff authored and DylanVann committed May 25, 2019
1 parent 1eed575 commit 5f65383
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.bumptech.glide.load.model.Headers;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.signature.ApplicationVersionSignature;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.NoSuchKeyException;
import com.facebook.react.bridge.ReadableMap;
Expand All @@ -30,6 +31,8 @@

import javax.annotation.Nullable;

import static com.bumptech.glide.request.RequestOptions.signatureOf;

class FastImageViewConverter {
private static final Drawable TRANSPARENT_DRAWABLE = new ColorDrawable(Color.TRANSPARENT);

Expand Down Expand Up @@ -81,7 +84,7 @@ static Headers getHeaders(ReadableMap source) {
return headers;
}

static RequestOptions getOptions(ReadableMap source) {
static RequestOptions getOptions(Context context, FastImageSource imageSource, ReadableMap source) {
// Get priority.
final Priority priority = FastImageViewConverter.getPriority(source);
// Get cache control method.
Expand All @@ -102,12 +105,25 @@ static RequestOptions getOptions(ReadableMap source) {
// Use defaults.
break;
}
return new RequestOptions()
.diskCacheStrategy(diskCacheStrategy)
.onlyRetrieveFromCache(onlyFromCache)
.skipMemoryCache(skipMemoryCache)
.priority(priority)
.placeholder(TRANSPARENT_DRAWABLE);

RequestOptions options = new RequestOptions()
.diskCacheStrategy(diskCacheStrategy)
.onlyRetrieveFromCache(onlyFromCache)
.skipMemoryCache(skipMemoryCache)
.priority(priority)
.placeholder(TRANSPARENT_DRAWABLE);

if (imageSource.isResource()) {
// Every local resource (drawable) in Android has its own unique numeric id, which are
// generated at build time. Although these ids are unique, they are not guaranteed unique
// across builds. The underlying glide implementation caches these resources. To make
// sure the cache does not return the wrong image, we should clear the cache when the
// application version changes. Adding a cache signature for only these local resources
// solves this issue: https://github.com/DylanVann/react-native-fast-image/issues/402
options = options.apply(signatureOf(ApplicationVersionSignature.obtain(context)));
}

return options;
}

private static FastImageCacheControl getCacheControl(ReadableMap source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void setSrc(FastImageViewWithUrl view, @Nullable ReadableMap source) {
// - android.resource://
// - data:image/png;base64
.load(imageSource.getSourceForLoad())
.apply(FastImageViewConverter.getOptions(source))
.apply(FastImageViewConverter.getOptions(context, imageSource, source))
.listener(new FastImageRequestListener(key))
.into(view);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void run() {
imageSource.isBase64Resource() ? imageSource.getSource() :
imageSource.isResource() ? imageSource.getUri() : imageSource.getGlideUrl()
)
.apply(FastImageViewConverter.getOptions(source))
.apply(FastImageViewConverter.getOptions(activity, imageSource, source))
.preload();
}
}
Expand Down

0 comments on commit 5f65383

Please sign in to comment.