feat: add clear image cache from memory and disk (#425)

This commit is contained in:
Woraphot Chokratanasombat 2021-09-15 04:55:31 +07:00 committed by GitHub
parent 4fc024b3bf
commit 818ed0c3f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import android.app.Activity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
@ -53,4 +54,33 @@ class FastImageViewModule extends ReactContextBaseJavaModule {
}
});
}
@ReactMethod
public void clearMemoryCache(final Promise promise) {
final Activity activity = getCurrentActivity();
if (activity == null) {
promise.resolve(null);
return;
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Glide.get(activity.getApplicationContext()).clearMemory();
promise.resolve(null);
}
});
}
@ReactMethod
public void clearDiskCache(Promise promise) {
final Activity activity = getCurrentActivity();
if (activity == null) {
promise.resolve(null);
return;
}
Glide.get(activity.getApplicationContext()).clearDiskCache();
promise.resolve(null);
}
}

View File

@ -1,6 +1,7 @@
#import "FFFastImageViewManager.h"
#import "FFFastImageView.h"
#import <SDWebImage/SDImageCache.h>
#import <SDWebImage/SDWebImagePrefetcher.h>
@implementation FFFastImageViewManager
@ -34,5 +35,17 @@ RCT_EXPORT_METHOD(preload:(nonnull NSArray<FFFastImageSource *> *)sources)
[[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:urls];
}
@end
RCT_EXPORT_METHOD(clearMemoryCache:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
{
[SDImageCache.sharedImageCache clearMemory];
resolve(NULL);
}
RCT_EXPORT_METHOD(clearDiskCache:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
{
[SDImageCache.sharedImageCache clearDiskOnCompletion:^(){
resolve(NULL);
}];
}
@end

View File

@ -68,4 +68,6 @@ declare export default class FastImage extends React$Component<FastImageProps> {
static priority: Priority;
static cacheControl: CacheControl;
static preload: PreloadFn;
static clearMemoryCache: () => Promise<void>;
static clearDiskCache: () => Promise<void>;
}

View File

@ -203,6 +203,8 @@ interface FastImageStaticProperties {
priority: typeof priority
cacheControl: typeof cacheControl
preload: (sources: Source[]) => void
clearMemoryCache: () => Promise<void>
clearDiskCache: () => Promise<void>
}
const FastImage: React.ComponentType<FastImageProps> &
@ -217,6 +219,10 @@ FastImage.priority = priority
FastImage.preload = (sources: Source[]) =>
FastImageViewNativeModule.preload(sources)
FastImage.clearMemoryCache = () => FastImageViewNativeModule.clearMemoryCache()
FastImage.clearDiskCache = () => FastImageViewNativeModule.clearDiskCache()
const styles = StyleSheet.create({
imageContainer: {
overflow: 'hidden',