Skip to content

Commit

Permalink
Feature: Add clear image cache from memory and disk
Browse files Browse the repository at this point in the history
  • Loading branch information
guhungry committed Jun 20, 2020
1 parent 432d01d commit 1912be0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
15 changes: 14 additions & 1 deletion ios/FastImage/FFFastImageViewManager.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "FFFastImageViewManager.h"
#import "FFFastImageView.h"

#import <SDWebImage/SDImageCache.h>
#import <SDWebImage/SDWebImagePrefetcher.h>

@implementation FFFastImageViewManager
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}
6 changes: 6 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ interface FastImageStaticProperties {
priority: typeof priority
cacheControl: typeof cacheControl
preload: (sources: Source[]) => void
clearMemoryCache: () => Promise<void>
clearDiskCache: () => Promise<void>
}

const FastImage: React.ComponentType<FastImageProps> &
Expand All @@ -211,6 +213,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',
Expand Down

0 comments on commit 1912be0

Please sign in to comment.