From 818ed0c3f59f825144609479047190f9bfd6dc76 Mon Sep 17 00:00:00 2001 From: Woraphot Chokratanasombat Date: Wed, 15 Sep 2021 04:55:31 +0700 Subject: [PATCH] feat: add clear image cache from memory and disk (#425) --- .../fastimage/FastImageViewModule.java | 30 +++++++++++++++++++ ios/FastImage/FFFastImageViewManager.m | 15 +++++++++- src/index.js.flow | 2 ++ src/index.tsx | 6 ++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/dylanvann/fastimage/FastImageViewModule.java b/android/src/main/java/com/dylanvann/fastimage/FastImageViewModule.java index 5452c7922..019032b23 100644 --- a/android/src/main/java/com/dylanvann/fastimage/FastImageViewModule.java +++ b/android/src/main/java/com/dylanvann/fastimage/FastImageViewModule.java @@ -4,6 +4,7 @@ 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 @@ public void run() { } }); } + + @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); + } } diff --git a/ios/FastImage/FFFastImageViewManager.m b/ios/FastImage/FFFastImageViewManager.m index 4cb522078..a8059afb2 100644 --- a/ios/FastImage/FFFastImageViewManager.m +++ b/ios/FastImage/FFFastImageViewManager.m @@ -1,6 +1,7 @@ #import "FFFastImageViewManager.h" #import "FFFastImageView.h" +#import #import @implementation FFFastImageViewManager @@ -34,5 +35,17 @@ - (FFFastImageView*)view { [[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 diff --git a/src/index.js.flow b/src/index.js.flow index 71fac3a92..7e17f3731 100644 --- a/src/index.js.flow +++ b/src/index.js.flow @@ -68,4 +68,6 @@ declare export default class FastImage extends React$Component { static priority: Priority; static cacheControl: CacheControl; static preload: PreloadFn; + static clearMemoryCache: () => Promise; + static clearDiskCache: () => Promise; } diff --git a/src/index.tsx b/src/index.tsx index 4734a784c..0aaf35b1b 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -203,6 +203,8 @@ interface FastImageStaticProperties { priority: typeof priority cacheControl: typeof cacheControl preload: (sources: Source[]) => void + clearMemoryCache: () => Promise + clearDiskCache: () => Promise } const FastImage: React.ComponentType & @@ -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',