Skip to content

Commit c6addce

Browse files
Martín BigioMorgan Pretty
authored andcommitted
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
1 parent 418ffc8 commit c6addce

File tree

8 files changed

+48
-17
lines changed

8 files changed

+48
-17
lines changed

ReactAndroid/src/main/java/com/facebook/react/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ DEPS = [
99
react_native_target('java/com/facebook/react/modules/debug:debug'),
1010
react_native_target('java/com/facebook/react/modules/systeminfo:systeminfo'),
1111
react_native_target('java/com/facebook/react/modules/toast:toast'),
12+
react_native_target('java/com/facebook/react/views/imagehelper:imagehelper'),
1213
react_native_target('java/com/facebook/react/uimanager:uimanager'),
1314
react_native_dep('java/com/facebook/systrace:systrace'),
1415
react_native_dep('libraries/fbcore/src/main/java/com/facebook/common/logging:logging'),

ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import com.facebook.react.devsupport.RedBoxHandler;
6262
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
6363
import com.facebook.react.modules.core.DeviceEventManagerModule;
64+
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
6465
import com.facebook.react.uimanager.AppRegistry;
6566
import com.facebook.react.uimanager.DisplayMetricsHolder;
6667
import com.facebook.react.uimanager.UIImplementationProvider;
@@ -562,6 +563,8 @@ public void destroy() {
562563
mHasStartedCreatingInitialContext = false;
563564
}
564565
mCurrentActivity = null;
566+
567+
ResourceDrawableIdHelper.getInstance().clear();
565568
}
566569

567570
private void moveToResumedLifecycleState(boolean force) {

ReactAndroid/src/main/java/com/facebook/react/XReactInstanceManagerImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
6464
import com.facebook.react.modules.core.DeviceEventManagerModule;
6565
import com.facebook.react.modules.debug.DeveloperSettings;
66+
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
6667
import com.facebook.react.uimanager.AppRegistry;
6768
import com.facebook.react.uimanager.DisplayMetricsHolder;
6869
import com.facebook.react.uimanager.UIImplementationProvider;
@@ -547,6 +548,8 @@ public void destroy() {
547548
mHasStartedCreatingInitialContext = false;
548549
}
549550
mCurrentActivity = null;
551+
552+
ResourceDrawableIdHelper.getInstance().clear();
550553
}
551554

552555
private void moveToResumedLifecycleState(boolean force) {

ReactAndroid/src/main/java/com/facebook/react/views/image/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ android_library(
99
react_native_target('java/com/facebook/csslayout:csslayout'),
1010
react_native_target('java/com/facebook/react/uimanager:uimanager'),
1111
react_native_target('java/com/facebook/react/uimanager/annotations:annotations'),
12+
react_native_target('java/com/facebook/react/views/imagehelper:imagehelper'),
1213
react_native_dep('libraries/fresco/fresco-react-native:fbcore'),
1314
react_native_dep('libraries/fresco/fresco-react-native:fresco-react-native'),
1415
react_native_dep('libraries/fresco/fresco-react-native:fresco-drawee'),

ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageManager.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public String getName() {
3737
return REACT_CLASS;
3838
}
3939

40-
private ResourceDrawableIdHelper mResourceDrawableIdHelper;
4140
private @Nullable AbstractDraweeControllerBuilder mDraweeControllerBuilder;
4241
private final @Nullable Object mCallerContext;
4342

@@ -46,14 +45,12 @@ public ReactImageManager(
4645
Object callerContext) {
4746
mDraweeControllerBuilder = draweeControllerBuilder;
4847
mCallerContext = callerContext;
49-
mResourceDrawableIdHelper = new ResourceDrawableIdHelper();
5048
}
5149

5250
public ReactImageManager() {
5351
// Lazily initialize as FrescoModule have not been initialized yet
5452
mDraweeControllerBuilder = null;
5553
mCallerContext = null;
56-
mResourceDrawableIdHelper = new ResourceDrawableIdHelper();
5754
}
5855

5956
public AbstractDraweeControllerBuilder getDraweeControllerBuilder() {
@@ -72,8 +69,7 @@ public ReactImageView createViewInstance(ThemedReactContext context) {
7269
return new ReactImageView(
7370
context,
7471
getDraweeControllerBuilder(),
75-
getCallerContext(),
76-
mResourceDrawableIdHelper);
72+
getCallerContext());
7773
}
7874

7975
// In JS this is Image.props.source

ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageView.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import com.facebook.react.uimanager.PixelUtil;
6060
import com.facebook.react.uimanager.UIManagerModule;
6161
import com.facebook.react.uimanager.events.EventDispatcher;
62+
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
6263

6364
/**
6465
* Wrapper class around Fresco's GenericDraweeView, enabling persisting props across multiple view
@@ -190,15 +191,14 @@ private void computeUri() {
190191
// ignore malformed uri, then attempt to extract resource ID.
191192
}
192193
if (mUri == null) {
193-
mUri = mResourceDrawableIdHelper.getResourceDrawableUri(getContext(), mSource);
194+
mUri = ResourceDrawableIdHelper.getInstance().getResourceDrawableUri(getContext(), mSource);
194195
mIsLocalImage = true;
195196
} else {
196197
mIsLocalImage = false;
197198
}
198199
}
199200
}
200201

201-
private final ResourceDrawableIdHelper mResourceDrawableIdHelper;
202202
private final List<ImageSource> mSources;
203203

204204
private @Nullable ImageSource mImageSource;
@@ -229,14 +229,12 @@ private static GenericDraweeHierarchy buildHierarchy(Context context) {
229229
public ReactImageView(
230230
Context context,
231231
AbstractDraweeControllerBuilder draweeControllerBuilder,
232-
@Nullable Object callerContext,
233-
ResourceDrawableIdHelper resourceDrawableIdHelper) {
232+
@Nullable Object callerContext) {
234233
super(context, buildHierarchy(context));
235234
mScaleType = ImageResizeMode.defaultValue();
236235
mDraweeControllerBuilder = draweeControllerBuilder;
237236
mRoundedCornerPostprocessor = new RoundedCornerPostprocessor();
238237
mCallerContext = callerContext;
239-
mResourceDrawableIdHelper = resourceDrawableIdHelper;
240238
mSources = new LinkedList<>();
241239
}
242240

@@ -344,7 +342,7 @@ public void setSource(@Nullable ReadableArray sources) {
344342
}
345343

346344
public void setLoadingIndicatorSource(@Nullable String name) {
347-
Drawable drawable = mResourceDrawableIdHelper.getResourceDrawable(getContext(), name);
345+
Drawable drawable = ResourceDrawableIdHelper.getInstance().getResourceDrawable(getContext(), name);
348346
mLoadingImageDrawable =
349347
drawable != null ? (Drawable) new AutoRotateDrawable(drawable, 1000) : null;
350348
mIsDirty = true;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
include_defs('//ReactAndroid/DEFS')
2+
3+
android_library(
4+
name = 'imagehelper',
5+
srcs = glob(['*.java']),
6+
deps = [
7+
react_native_dep('third-party/java/infer-annotations:infer-annotations'),
8+
react_native_dep('third-party/java/jsr-305:jsr-305'),
9+
],
10+
visibility = [
11+
'PUBLIC',
12+
],
13+
)
14+
15+
project_config(
16+
src_target = ':imagehelper',
17+
)

ReactAndroid/src/main/java/com/facebook/react/views/image/ResourceDrawableIdHelper.java renamed to ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2004-present Facebook. All Rights Reserved.
22

3-
package com.facebook.react.views.image;
3+
package com.facebook.react.views.imagehelper;
44

55
import javax.annotation.Nullable;
66

@@ -11,19 +11,31 @@
1111
import android.graphics.drawable.Drawable;
1212
import android.net.Uri;
1313

14-
import com.facebook.common.util.UriUtil;
15-
1614
/**
1715
* Helper class for obtaining information about local images.
1816
*/
19-
/* package */ class ResourceDrawableIdHelper {
17+
public class ResourceDrawableIdHelper {
2018

2119
private Map<String, Integer> mResourceDrawableIdMap;
2220

23-
public ResourceDrawableIdHelper() {
21+
private static final String LOCAL_RESOURCE_SCHEME = "res";
22+
private static ResourceDrawableIdHelper sResourceDrawableIdHelper;
23+
24+
private ResourceDrawableIdHelper() {
2425
mResourceDrawableIdMap = new HashMap<String, Integer>();
2526
}
2627

28+
public static ResourceDrawableIdHelper getInstance() {
29+
if (sResourceDrawableIdHelper == null) {
30+
sResourceDrawableIdHelper = new ResourceDrawableIdHelper();
31+
}
32+
return sResourceDrawableIdHelper;
33+
}
34+
35+
public void clear() {
36+
mResourceDrawableIdMap.clear();
37+
}
38+
2739
public int getResourceDrawableId(Context context, @Nullable String name) {
2840
if (name == null || name.isEmpty()) {
2941
return 0;
@@ -48,7 +60,7 @@ public int getResourceDrawableId(Context context, @Nullable String name) {
4860
public Uri getResourceDrawableUri(Context context, @Nullable String name) {
4961
int resId = getResourceDrawableId(context, name);
5062
return resId > 0 ? new Uri.Builder()
51-
.scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
63+
.scheme(LOCAL_RESOURCE_SCHEME)
5264
.path(String.valueOf(resId))
5365
.build() : Uri.EMPTY;
5466
}

0 commit comments

Comments
 (0)