Promote ResourceDrawableIdHelper to new module

Summary:
Found a couple of places where we were copy-pasting the logic `ResourceDrawableIdHelper` had. This class was only available on the image module and it had package visibility. I moved it to its own module so that we can easily use it from others.

This diff is pretty simillar to 54ed44628d but it fixes a bug due to which we had to revert it.

Reviewed By: andreicoman11

Differential Revision: D3499062

fbshipit-source-id: f912f57e5ac21a9f30fe42067c784f49fa46ed48
This commit is contained in:
Felix Oghina
2016-07-01 04:20:35 -07:00
committed by Facebook Github Bot 5
parent 508cc06565
commit 010e1977a8
10 changed files with 60 additions and 21 deletions

View File

@@ -9,6 +9,7 @@ android_library(
react_native_target('java/com/facebook/csslayout:csslayout'),
react_native_target('java/com/facebook/react/uimanager:uimanager'),
react_native_target('java/com/facebook/react/uimanager/annotations:annotations'),
react_native_target('java/com/facebook/react/views/imagehelper:imagehelper'),
react_native_dep('libraries/fresco/fresco-react-native:fbcore'),
react_native_dep('libraries/fresco/fresco-react-native:fresco-react-native'),
react_native_dep('libraries/fresco/fresco-react-native:fresco-drawee'),

View File

@@ -37,7 +37,6 @@ public class ReactImageManager extends SimpleViewManager<ReactImageView> {
return REACT_CLASS;
}
private ResourceDrawableIdHelper mResourceDrawableIdHelper;
private @Nullable AbstractDraweeControllerBuilder mDraweeControllerBuilder;
private final @Nullable Object mCallerContext;
@@ -46,14 +45,12 @@ public class ReactImageManager extends SimpleViewManager<ReactImageView> {
Object callerContext) {
mDraweeControllerBuilder = draweeControllerBuilder;
mCallerContext = callerContext;
mResourceDrawableIdHelper = new ResourceDrawableIdHelper();
}
public ReactImageManager() {
// Lazily initialize as FrescoModule have not been initialized yet
mDraweeControllerBuilder = null;
mCallerContext = null;
mResourceDrawableIdHelper = new ResourceDrawableIdHelper();
}
public AbstractDraweeControllerBuilder getDraweeControllerBuilder() {
@@ -72,8 +69,7 @@ public class ReactImageManager extends SimpleViewManager<ReactImageView> {
return new ReactImageView(
context,
getDraweeControllerBuilder(),
getCallerContext(),
mResourceDrawableIdHelper);
getCallerContext());
}
// In JS this is Image.props.source

View File

@@ -59,6 +59,7 @@ import com.facebook.react.common.SystemClock;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
/**
* Wrapper class around Fresco's GenericDraweeView, enabling persisting props across multiple view
@@ -190,7 +191,7 @@ public class ReactImageView extends GenericDraweeView {
// ignore malformed uri, then attempt to extract resource ID.
}
if (mUri == null) {
mUri = mResourceDrawableIdHelper.getResourceDrawableUri(getContext(), mSource);
mUri = ResourceDrawableIdHelper.getInstance().getResourceDrawableUri(getContext(), mSource);
mIsLocalImage = true;
} else {
mIsLocalImage = false;
@@ -198,7 +199,6 @@ public class ReactImageView extends GenericDraweeView {
}
}
private final ResourceDrawableIdHelper mResourceDrawableIdHelper;
private final List<ImageSource> mSources;
private @Nullable ImageSource mImageSource;
@@ -229,14 +229,12 @@ public class ReactImageView extends GenericDraweeView {
public ReactImageView(
Context context,
AbstractDraweeControllerBuilder draweeControllerBuilder,
@Nullable Object callerContext,
ResourceDrawableIdHelper resourceDrawableIdHelper) {
@Nullable Object callerContext) {
super(context, buildHierarchy(context));
mScaleType = ImageResizeMode.defaultValue();
mDraweeControllerBuilder = draweeControllerBuilder;
mRoundedCornerPostprocessor = new RoundedCornerPostprocessor();
mCallerContext = callerContext;
mResourceDrawableIdHelper = resourceDrawableIdHelper;
mSources = new LinkedList<>();
}
@@ -344,7 +342,7 @@ public class ReactImageView extends GenericDraweeView {
}
public void setLoadingIndicatorSource(@Nullable String name) {
Drawable drawable = mResourceDrawableIdHelper.getResourceDrawable(getContext(), name);
Drawable drawable = ResourceDrawableIdHelper.getInstance().getResourceDrawable(getContext(), name);
mLoadingImageDrawable =
drawable != null ? (Drawable) new AutoRotateDrawable(drawable, 1000) : null;
mIsDirty = true;

View File

@@ -1,55 +0,0 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.react.views.image;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import com.facebook.common.util.UriUtil;
/**
* Helper class for obtaining information about local images.
*/
/* package */ class ResourceDrawableIdHelper {
private Map<String, Integer> mResourceDrawableIdMap;
public ResourceDrawableIdHelper() {
mResourceDrawableIdMap = new HashMap<String, Integer>();
}
public int getResourceDrawableId(Context context, @Nullable String name) {
if (name == null || name.isEmpty()) {
return 0;
}
name = name.toLowerCase().replace("-", "_");
if (mResourceDrawableIdMap.containsKey(name)) {
return mResourceDrawableIdMap.get(name);
}
int id = context.getResources().getIdentifier(
name,
"drawable",
context.getPackageName());
mResourceDrawableIdMap.put(name, id);
return id;
}
public @Nullable Drawable getResourceDrawable(Context context, @Nullable String name) {
int resId = getResourceDrawableId(context, name);
return resId > 0 ? context.getResources().getDrawable(resId) : null;
}
public Uri getResourceDrawableUri(Context context, @Nullable String name) {
int resId = getResourceDrawableId(context, name);
return resId > 0 ? new Uri.Builder()
.scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
.path(String.valueOf(resId))
.build() : Uri.EMPTY;
}
}