Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix local resource cache issue, fixes #402 #472

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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