Allow to pass in `ImagePipelineConfig` to `FrescoModule`

Summary: This allows an app to configure much more options in Fresco. public

Reviewed By: astreet

Differential Revision: D2700598

fb-gh-sync-id: e1ffff18bff270e14ef82c14f7bfeef984605738
This commit is contained in:
Alexander Blom 2015-11-29 03:53:49 -08:00 committed by facebook-github-bot-0
parent 4c7378cf8d
commit 4890424562
1 changed files with 36 additions and 28 deletions

View File

@ -39,25 +39,26 @@ import com.squareup.okhttp.OkHttpClient;
public class FrescoModule extends ReactContextBaseJavaModule implements public class FrescoModule extends ReactContextBaseJavaModule implements
ModuleDataCleaner.Cleanable { ModuleDataCleaner.Cleanable {
@Nullable private RequestListener mRequestListener; private @Nullable ImagePipelineConfig mConfig;
@Nullable private DiskCacheConfig mDiskCacheConfig;
public FrescoModule(ReactApplicationContext reactContext) { public FrescoModule(ReactApplicationContext reactContext) {
super(reactContext); this(reactContext, getDefaultConfig(reactContext, null, null));
} }
public FrescoModule(ReactApplicationContext reactContext, RequestListener listener) { public FrescoModule(ReactApplicationContext reactContext, RequestListener listener) {
super(reactContext); this(reactContext, getDefaultConfig(reactContext, listener, null));
mRequestListener = listener;
} }
public FrescoModule( public FrescoModule(
ReactApplicationContext reactContext, ReactApplicationContext reactContext,
RequestListener listener, RequestListener listener,
DiskCacheConfig diskCacheConfig) { DiskCacheConfig diskCacheConfig) {
this(reactContext, getDefaultConfig(reactContext, listener, diskCacheConfig));
}
public FrescoModule(ReactApplicationContext reactContext, ImagePipelineConfig config) {
super(reactContext); super(reactContext);
mRequestListener = listener; mConfig = config;
mDiskCacheConfig = diskCacheConfig;
} }
@Override @Override
@ -67,27 +68,9 @@ public class FrescoModule extends ReactContextBaseJavaModule implements
// This code can be removed if using Fresco from Maven rather than from source // This code can be removed if using Fresco from Maven rather than from source
SoLoaderShim.setHandler(new FrescoHandler()); SoLoaderShim.setHandler(new FrescoHandler());
HashSet<RequestListener> requestListeners = new HashSet<>(); Context context = getReactApplicationContext().getApplicationContext();
requestListeners.add(new SystraceRequestListener()); Fresco.initialize(context, mConfig);
if (mRequestListener != null) { mConfig = null;
requestListeners.add(mRequestListener);
}
Context context = this.getReactApplicationContext().getApplicationContext();
OkHttpClient okHttpClient = OkHttpClientProvider.getOkHttpClient();
ImagePipelineConfig.Builder builder =
OkHttpImagePipelineConfigFactory.newBuilder(context, okHttpClient);
builder
.setDownsampleEnabled(false)
.setRequestListeners(requestListeners);
if (mDiskCacheConfig != null) {
builder.setMainDiskCacheConfig(mDiskCacheConfig);
}
ImagePipelineConfig config = builder.build();
Fresco.initialize(context, config);
} }
@Override @Override
@ -105,6 +88,31 @@ public class FrescoModule extends ReactContextBaseJavaModule implements
imagePipelineFactory.getSmallImageDiskStorageCache().clearAll(); imagePipelineFactory.getSmallImageDiskStorageCache().clearAll();
} }
private static ImagePipelineConfig getDefaultConfig(
Context context,
@Nullable RequestListener listener,
@Nullable DiskCacheConfig diskCacheConfig) {
HashSet<RequestListener> requestListeners = new HashSet<>();
requestListeners.add(new SystraceRequestListener());
if (listener != null) {
requestListeners.add(listener);
}
OkHttpClient okHttpClient = OkHttpClientProvider.getOkHttpClient();
ImagePipelineConfig.Builder builder =
OkHttpImagePipelineConfigFactory.newBuilder(context.getApplicationContext(), okHttpClient);
builder
.setDownsampleEnabled(false)
.setRequestListeners(requestListeners);
if (diskCacheConfig != null) {
builder.setMainDiskCacheConfig(diskCacheConfig);
}
return builder.build();
}
private static class FrescoHandler implements SoLoaderShim.Handler { private static class FrescoHandler implements SoLoaderShim.Handler {
@Override @Override
public void loadLibrary(String libraryName) { public void loadLibrary(String libraryName) {