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
ModuleDataCleaner.Cleanable {
@Nullable private RequestListener mRequestListener;
@Nullable private DiskCacheConfig mDiskCacheConfig;
private @Nullable ImagePipelineConfig mConfig;
public FrescoModule(ReactApplicationContext reactContext) {
super(reactContext);
this(reactContext, getDefaultConfig(reactContext, null, null));
}
public FrescoModule(ReactApplicationContext reactContext, RequestListener listener) {
super(reactContext);
mRequestListener = listener;
this(reactContext, getDefaultConfig(reactContext, listener, null));
}
public FrescoModule(
ReactApplicationContext reactContext,
RequestListener listener,
DiskCacheConfig diskCacheConfig) {
this(reactContext, getDefaultConfig(reactContext, listener, diskCacheConfig));
}
public FrescoModule(ReactApplicationContext reactContext, ImagePipelineConfig config) {
super(reactContext);
mRequestListener = listener;
mDiskCacheConfig = diskCacheConfig;
mConfig = config;
}
@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
SoLoaderShim.setHandler(new FrescoHandler());
HashSet<RequestListener> requestListeners = new HashSet<>();
requestListeners.add(new SystraceRequestListener());
if (mRequestListener != 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);
Context context = getReactApplicationContext().getApplicationContext();
Fresco.initialize(context, mConfig);
mConfig = null;
}
@Override
@ -105,6 +88,31 @@ public class FrescoModule extends ReactContextBaseJavaModule implements
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 {
@Override
public void loadLibrary(String libraryName) {