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.

Reviewed By: andreicoman11

Differential Revision: D3463697

fbshipit-source-id: 708058c5f2445e27b151d8a6f3b47fd23485bc88
This commit is contained in:
Martín Bigio 2016-06-28 14:11:17 -07:00 committed by Facebook Github Bot 8
parent edbe0dc019
commit 54ed44628d
8 changed files with 48 additions and 17 deletions

View File

@ -9,6 +9,7 @@ DEPS = [
react_native_target('java/com/facebook/react/modules/debug:debug'),
react_native_target('java/com/facebook/react/modules/systeminfo:systeminfo'),
react_native_target('java/com/facebook/react/modules/toast:toast'),
react_native_target('java/com/facebook/react/views/imagehelper:imagehelper'),
react_native_target('java/com/facebook/react/uimanager:uimanager'),
react_native_dep('java/com/facebook/systrace:systrace'),
react_native_dep('libraries/fbcore/src/main/java/com/facebook/common/logging:logging'),

View File

@ -61,6 +61,7 @@ import com.facebook.react.devsupport.ReactInstanceDevCommandsHandler;
import com.facebook.react.devsupport.RedBoxHandler;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
import com.facebook.react.uimanager.AppRegistry;
import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.UIImplementationProvider;
@ -562,6 +563,8 @@ import static com.facebook.react.bridge.ReactMarkerConstants.RUN_JS_BUNDLE_START
mHasStartedCreatingInitialContext = false;
}
mCurrentActivity = null;
ResourceDrawableIdHelper.getInstance().clear();
}
private void moveToResumedLifecycleState(boolean force) {

View File

@ -63,6 +63,7 @@ import com.facebook.react.devsupport.RedBoxHandler;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.modules.debug.DeveloperSettings;
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
import com.facebook.react.uimanager.AppRegistry;
import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.UIImplementationProvider;
@ -547,6 +548,8 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
mHasStartedCreatingInitialContext = false;
}
mCurrentActivity = null;
ResourceDrawableIdHelper.getInstance().clear();
}
private void moveToResumedLifecycleState(boolean force) {

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

@ -0,0 +1,17 @@
include_defs('//ReactAndroid/DEFS')
android_library(
name = 'imagehelper',
srcs = glob(['*.java']),
deps = [
react_native_dep('third-party/java/infer-annotations:infer-annotations'),
react_native_dep('third-party/java/jsr-305:jsr-305'),
],
visibility = [
'PUBLIC',
],
)
project_config(
src_target = ':imagehelper',
)

View File

@ -1,6 +1,6 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.react.views.image;
package com.facebook.react.views.imagehelper;
import javax.annotation.Nullable;
@ -11,19 +11,31 @@ 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 {
public class ResourceDrawableIdHelper {
private Map<String, Integer> mResourceDrawableIdMap;
public ResourceDrawableIdHelper() {
private static final String LOCAL_RESOURCE_SCHEME = "res";
private static ResourceDrawableIdHelper sResourceDrawableIdHelper;
private ResourceDrawableIdHelper() {
mResourceDrawableIdMap = new HashMap<String, Integer>();
}
public static ResourceDrawableIdHelper getInstance() {
if (sResourceDrawableIdHelper == null) {
sResourceDrawableIdHelper = new ResourceDrawableIdHelper();
}
return sResourceDrawableIdHelper;
}
public void clear() {
mResourceDrawableIdMap.clear();
}
public int getResourceDrawableId(Context context, @Nullable String name) {
if (name == null || name.isEmpty()) {
return 0;
@ -48,7 +60,7 @@ import com.facebook.common.util.UriUtil;
public Uri getResourceDrawableUri(Context context, @Nullable String name) {
int resId = getResourceDrawableId(context, name);
return resId > 0 ? new Uri.Builder()
.scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
.scheme(LOCAL_RESOURCE_SCHEME)
.path(String.valueOf(resId))
.build() : Uri.EMPTY;
}