Skip to content

Commit

Permalink
Migrate ResourceDrawableHelper.java (facebook#43765)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rshest authored and facebook-github-bot committed Apr 3, 2024
1 parent 11108f3 commit f2bdec2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 89 deletions.
17 changes: 11 additions & 6 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -6129,12 +6129,17 @@ public class com/facebook/react/views/imagehelper/MultiSourceHelper$MultiSourceR
public fun getBestResultInCache ()Lcom/facebook/react/views/imagehelper/ImageSource;
}

public class com/facebook/react/views/imagehelper/ResourceDrawableIdHelper {
public fun clear ()V
public static fun getInstance ()Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper;
public fun getResourceDrawable (Landroid/content/Context;Ljava/lang/String;)Landroid/graphics/drawable/Drawable;
public fun getResourceDrawableId (Landroid/content/Context;Ljava/lang/String;)I
public fun getResourceDrawableUri (Landroid/content/Context;Ljava/lang/String;)Landroid/net/Uri;
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 final fun getInstance ()Lcom/facebook/react/views/imagehelper/ResourceDrawableIdHelper;
}

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.views.imagehelper

import android.content.Context
import android.graphics.drawable.Drawable
import android.net.Uri
import androidx.core.content.res.ResourcesCompat
import javax.annotation.concurrent.ThreadSafe

/** Helper class for obtaining information about local images. */
@ThreadSafe
public class ResourceDrawableIdHelper private constructor() {
private val resourceDrawableIdMap: MutableMap<String, Int> = HashMap()

@Synchronized
public fun clear() {
resourceDrawableIdMap.clear()
}

public fun getResourceDrawableId(context: Context, name: String?): Int {
if (name.isNullOrEmpty()) {
return 0
}
val normalizedName = name.lowercase().replace("-", "_")

// name could be a resource id.
try {
return normalizedName.toInt()
} catch (e: NumberFormatException) {
// Do nothing.
}

synchronized(this) {
if (resourceDrawableIdMap.containsKey(normalizedName)) {
return resourceDrawableIdMap.get(normalizedName)!!
}
return context.resources.getIdentifier(normalizedName, "drawable", context.packageName).also {
resourceDrawableIdMap[normalizedName] = it
}
}
}

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
}

public fun getResourceDrawableUri(context: Context, name: String?): Uri {
val resId = getResourceDrawableId(context, name)
return if (resId > 0) {
Uri.Builder().scheme(LOCAL_RESOURCE_SCHEME).path(resId.toString()).build()
} else {
Uri.EMPTY
}
}

public companion object {
private const val LOCAL_RESOURCE_SCHEME = "res"
private val resourceDrawableIdHelper: ResourceDrawableIdHelper = ResourceDrawableIdHelper()
@JvmStatic
public val instance: ResourceDrawableIdHelper
get() = resourceDrawableIdHelper
}
}

0 comments on commit f2bdec2

Please sign in to comment.