Make ResourceDrawableIdHelper an object (#51013)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/51013

There's no need to use a singleton pattern here, we can just use a Kotlin object.

Changelog: [Android][Removed] Deprecated `ResourceDrawableIdHelper.instance`

Reviewed By: Abbondanzo

Differential Revision: D73923281

fbshipit-source-id: f46e125ce595fe4506b9fb5aa471109f5ba150f0
This commit is contained in:
Pieter De Baets authored and Facebook GitHub Bot committed 2025-04-30 08:53:59 -07:00
1 parent 16bfb2f87a
commit 8de401c625
5 files changed
+32 -33

No files matched your search

@@ -5688,17 +5688,13 @@ public final class com/facebook/react/views/imagehelper/ImageSource$Companion {
}
public final class com/facebook/react/views/imagehelper/ResourceDrawableIdHelper {
public static final field Companion Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper$Companion;
public final fun clear ()V
public static final fun getInstance ()Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper;
public final fun getResourceDrawable (Landroid/content/Context;Ljava/lang/String;)Landroid/graphics/drawable/Drawable;
public final fun getResourceDrawableId (Landroid/content/Context;Ljava/lang/String;)I
public final fun getResourceDrawableUri (Landroid/content/Context;Ljava/lang/String;)Landroid/net/Uri;
}
public final class com/facebook/react/views/imagehelper/ResourceDrawableIdHelper$Companion {
public static final field INSTANCE Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper;
public final fun DEPRECATED$getInstance ()Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper;
public final fun getInstance ()Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper;
public static final fun clear ()V
public static final fun getInstance ()Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper;
public static final fun getResourceDrawable (Landroid/content/Context;Ljava/lang/String;)Landroid/graphics/drawable/Drawable;
public static final fun getResourceDrawableId (Landroid/content/Context;Ljava/lang/String;)I
public static final fun getResourceDrawableUri (Landroid/content/Context;Ljava/lang/String;)Landroid/net/Uri;
}
public final class com/facebook/react/views/modal/ReactModalHostManager : com/facebook/react/uimanager/ViewGroupManager, com/facebook/react/viewmanagers/ModalHostViewManagerInterface {
@@ -73,7 +73,7 @@ import com.facebook.react.views.image.MultiPostprocessor.Companion.from
import com.facebook.react.views.imagehelper.ImageSource
import com.facebook.react.views.imagehelper.ImageSource.Companion.getTransparentBitmapImageSource
import com.facebook.react.views.imagehelper.MultiSourceHelper.getBestSourceForSize
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper.Companion.instance
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper
import kotlin.math.abs
/**
@@ -326,7 +326,7 @@ public class ReactImageView(
}
public fun setDefaultSource(name: String?) {
val newDefaultDrawable = instance.getResourceDrawable(context, name)
val newDefaultDrawable = ResourceDrawableIdHelper.getResourceDrawable(context, name)
if (defaultImageDrawable != newDefaultDrawable) {
defaultImageDrawable = newDefaultDrawable
isDirty = true
@@ -334,7 +334,7 @@ public class ReactImageView(
}
public fun setLoadingIndicatorSource(name: String?) {
val drawable = instance.getResourceDrawable(context, name)
val drawable = ResourceDrawableIdHelper.getResourceDrawable(context, name)
val newLoadingIndicatorSource = drawable?.let { AutoRotateDrawable(it, 1000) }
if (loadingImageDrawable != newLoadingIndicatorSource) {
loadingImageDrawable = newLoadingIndicatorSource
@@ -64,7 +64,7 @@ constructor(
private fun computeLocalUri(context: Context): Uri {
_isResource = true
return ResourceDrawableIdHelper.instance.getResourceDrawableUri(context, source)
return ResourceDrawableIdHelper.getResourceDrawableUri(context, source)
}
public companion object {
@@ -15,14 +15,16 @@ import javax.annotation.concurrent.ThreadSafe
/** Helper class for obtaining information about local images. */
@ThreadSafe
public class ResourceDrawableIdHelper private constructor() {
public object ResourceDrawableIdHelper {
private val resourceDrawableIdMap: MutableMap<String, Int> = HashMap()
@Synchronized
@JvmStatic
public fun clear() {
resourceDrawableIdMap.clear()
}
@JvmStatic
public fun getResourceDrawableId(context: Context, name: String?): Int {
if (name.isNullOrEmpty()) {
return 0
@@ -47,11 +49,13 @@ public class ResourceDrawableIdHelper private constructor() {
return newId
}
@JvmStatic
public fun getResourceDrawable(context: Context, name: String?): Drawable? {
val resId = getResourceDrawableId(context, name)
return if (resId > 0) ResourcesCompat.getDrawable(context.resources, resId, null) else null
}
@JvmStatic
public fun getResourceDrawableUri(context: Context, name: String?): Uri {
val resId = getResourceDrawableId(context, name)
return if (resId > 0) {
@@ -61,21 +65,20 @@ public class ResourceDrawableIdHelper private constructor() {
}
}
public companion object {
private const val LOCAL_RESOURCE_SCHEME = "res"
private val resourceDrawableIdHelper: ResourceDrawableIdHelper = ResourceDrawableIdHelper()
@JvmStatic
public val instance: ResourceDrawableIdHelper
get() = resourceDrawableIdHelper
@JvmStatic
@Deprecated("Use object methods instead, this API is for backward compat")
public val instance: ResourceDrawableIdHelper
get() = this
/**
* We're just re-adding this to reduce a breaking change for libraries in React Native 0.75.
*
* @deprecated Use instance instead
*/
@Deprecated("Use .instance instead, this API is for backward compat", ReplaceWith("instance"))
@JvmName(
"DEPRECATED\$getInstance") // We intentionally don't want to expose this accessor to Java.
public fun getInstance(): ResourceDrawableIdHelper = instance
}
private const val LOCAL_RESOURCE_SCHEME = "res"
/**
* We're just re-adding this to reduce a breaking change for libraries in React Native 0.75.
*
* @deprecated Use instance instead
*/
@Deprecated("Use .instance instead, this API is for backward compat", ReplaceWith("instance"))
@JvmName(
"DEPRECATED\$getInstance") // We intentionally don't want to expose this accessor to Java.
public fun getInstance(): ResourceDrawableIdHelper = this
}
@@ -56,7 +56,7 @@ import com.facebook.react.uimanager.events.EventDispatcher
import com.facebook.react.uimanager.style.BorderRadiusProp
import com.facebook.react.uimanager.style.BorderStyle.Companion.fromString
import com.facebook.react.uimanager.style.LogicalEdge
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper.Companion.instance
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper
import com.facebook.react.views.scroll.ScrollEventType
import com.facebook.react.views.scroll.ScrollEventType.Companion.getJSEventName
import com.facebook.react.views.text.DefaultStyleValuesUtil.getDefaultTextColor
@@ -598,7 +598,7 @@ public open class ReactTextInputManager public constructor() :
@ReactProp(name = "inlineImageLeft")
public fun setInlineImageLeft(view: ReactEditText, resource: String?) {
val id = instance.getResourceDrawableId(view.context, resource)
val id = ResourceDrawableIdHelper.getResourceDrawableId(view.context, resource)
view.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0)
}