Skip to content

Commit f2bdec2

Browse files
rshestfacebook-github-bot
authored andcommitted
Migrate ResourceDrawableHelper.java (facebook#43765)
Summary: Pull Request resolved: facebook#43765 ## Changelog: [Internal] - As in the title, migrates the corresponding file to Kotlin. Reviewed By: cortinico Differential Revision: D55636177 fbshipit-source-id: 9e7f94475185953f22fdcb0b4c94b84d4fc4e931
1 parent 11108f3 commit f2bdec2

File tree

3 files changed

+81
-89
lines changed

3 files changed

+81
-89
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6129,12 +6129,17 @@ public class com/facebook/react/views/imagehelper/MultiSourceHelper$MultiSourceR
61296129
public fun getBestResultInCache ()Lcom/facebook/react/views/imagehelper/ImageSource;
61306130
}
61316131

6132-
public class com/facebook/react/views/imagehelper/ResourceDrawableIdHelper {
6133-
public fun clear ()V
6134-
public static fun getInstance ()Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper;
6135-
public fun getResourceDrawable (Landroid/content/Context;Ljava/lang/String;)Landroid/graphics/drawable/Drawable;
6136-
public fun getResourceDrawableId (Landroid/content/Context;Ljava/lang/String;)I
6137-
public fun getResourceDrawableUri (Landroid/content/Context;Ljava/lang/String;)Landroid/net/Uri;
6132+
public final class com/facebook/react/views/imagehelper/ResourceDrawableIdHelper {
6133+
public static final field Companion Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper$Companion;
6134+
public final fun clear ()V
6135+
public static final fun getInstance ()Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper;
6136+
public final fun getResourceDrawable (Landroid/content/Context;Ljava/lang/String;)Landroid/graphics/drawable/Drawable;
6137+
public final fun getResourceDrawableId (Landroid/content/Context;Ljava/lang/String;)I
6138+
public final fun getResourceDrawableUri (Landroid/content/Context;Ljava/lang/String;)Landroid/net/Uri;
6139+
}
6140+
6141+
public final class com/facebook/react/views/imagehelper/ResourceDrawableIdHelper$Companion {
6142+
public final fun getInstance ()Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper;
61386143
}
61396144

61406145
public class com/facebook/react/views/modal/ModalHostShadowNode$$PropsSetter : com/facebook/react/uimanager/ViewManagerPropertyUpdater$ShadowNodeSetter {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.java

Lines changed: 0 additions & 83 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.views.imagehelper
9+
10+
import android.content.Context
11+
import android.graphics.drawable.Drawable
12+
import android.net.Uri
13+
import androidx.core.content.res.ResourcesCompat
14+
import javax.annotation.concurrent.ThreadSafe
15+
16+
/** Helper class for obtaining information about local images. */
17+
@ThreadSafe
18+
public class ResourceDrawableIdHelper private constructor() {
19+
private val resourceDrawableIdMap: MutableMap<String, Int> = HashMap()
20+
21+
@Synchronized
22+
public fun clear() {
23+
resourceDrawableIdMap.clear()
24+
}
25+
26+
public fun getResourceDrawableId(context: Context, name: String?): Int {
27+
if (name.isNullOrEmpty()) {
28+
return 0
29+
}
30+
val normalizedName = name.lowercase().replace("-", "_")
31+
32+
// name could be a resource id.
33+
try {
34+
return normalizedName.toInt()
35+
} catch (e: NumberFormatException) {
36+
// Do nothing.
37+
}
38+
39+
synchronized(this) {
40+
if (resourceDrawableIdMap.containsKey(normalizedName)) {
41+
return resourceDrawableIdMap.get(normalizedName)!!
42+
}
43+
return context.resources.getIdentifier(normalizedName, "drawable", context.packageName).also {
44+
resourceDrawableIdMap[normalizedName] = it
45+
}
46+
}
47+
}
48+
49+
public fun getResourceDrawable(context: Context, name: String?): Drawable? {
50+
val resId = getResourceDrawableId(context, name)
51+
return if (resId > 0) ResourcesCompat.getDrawable(context.resources, resId, null) else null
52+
}
53+
54+
public fun getResourceDrawableUri(context: Context, name: String?): Uri {
55+
val resId = getResourceDrawableId(context, name)
56+
return if (resId > 0) {
57+
Uri.Builder().scheme(LOCAL_RESOURCE_SCHEME).path(resId.toString()).build()
58+
} else {
59+
Uri.EMPTY
60+
}
61+
}
62+
63+
public companion object {
64+
private const val LOCAL_RESOURCE_SCHEME = "res"
65+
private val resourceDrawableIdHelper: ResourceDrawableIdHelper = ResourceDrawableIdHelper()
66+
@JvmStatic
67+
public val instance: ResourceDrawableIdHelper
68+
get() = resourceDrawableIdHelper
69+
}
70+
}

0 commit comments

Comments
 (0)