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:
parent
508cc06565
commit
010e1977a8
|
@ -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'),
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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'),
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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',
|
||||
)
|
|
@ -1,6 +1,13 @@
|
|||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.views.image;
|
||||
package com.facebook.react.views.imagehelper;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
|
@ -11,19 +18,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 +67,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;
|
||||
}
|
|
@ -12,6 +12,7 @@ android_library(
|
|||
react_native_target('java/com/facebook/react/modules/core:core'),
|
||||
react_native_target('java/com/facebook/react/uimanager/annotations:annotations'),
|
||||
react_native_target('java/com/facebook/react/uimanager:uimanager'),
|
||||
react_native_target('java/com/facebook/react/views/imagehelper:imagehelper'),
|
||||
react_native_target('java/com/facebook/react/views/text:text'),
|
||||
react_native_target('java/com/facebook/react/views/view:view'),
|
||||
],
|
||||
|
|
|
@ -43,6 +43,7 @@ import com.facebook.react.uimanager.ViewDefaults;
|
|||
import com.facebook.react.uimanager.ViewProps;
|
||||
import com.facebook.react.uimanager.annotations.ReactProp;
|
||||
import com.facebook.react.uimanager.events.EventDispatcher;
|
||||
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
|
||||
import com.facebook.react.views.text.DefaultStyleValuesUtil;
|
||||
import com.facebook.react.views.text.ReactTextUpdate;
|
||||
import com.facebook.react.views.text.TextInlineImageSpan;
|
||||
|
@ -66,7 +67,7 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
|
|||
private static final String KEYBOARD_TYPE_PHONE_PAD = "phone-pad";
|
||||
private static final InputFilter[] EMPTY_FILTERS = new InputFilter[0];
|
||||
private static final int UNSET = -1;
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return REACT_CLASS;
|
||||
|
@ -325,8 +326,7 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
|
|||
|
||||
@ReactProp(name = "inlineImageLeft")
|
||||
public void setInlineImageLeft(ReactEditText view, @Nullable String resource) {
|
||||
// TODO: t11992069 fix this after `ResourceDrawableIdHelper` gets fixed
|
||||
int id = 0;//ResourceDrawableIdHelper.getInstance().getResourceDrawableId(view.getContext(), resource);
|
||||
int id = ResourceDrawableIdHelper.getInstance().getResourceDrawableId(view.getContext(), resource);
|
||||
view.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue