From fd3ef7b692f965c0170f6444661e1421ae1936b1 Mon Sep 17 00:00:00 2001 From: Thomas Nardone Date: Thu, 4 Apr 2024 15:00:41 -0700 Subject: [PATCH] Avoid !! in ResourceDrawableHelper.kt (#43820) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43820 Changelog: [Internal] Reviewed By: cortinico Differential Revision: D55701291 fbshipit-source-id: afdce3ffa59bc488d6c36d78a8cf3176089599ba --- .../views/imagehelper/ResourceDrawableIdHelper.kt | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.kt index 67e8eef582822c..c4e0e8331fd005 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.kt @@ -37,15 +37,16 @@ public class ResourceDrawableIdHelper private constructor() { } synchronized(this) { - if (resourceDrawableIdMap.containsKey(normalizedName)) { - return resourceDrawableIdMap.get(normalizedName)!! - } - return context.resources.getIdentifier(normalizedName, "drawable", context.packageName).also { - resourceDrawableIdMap[normalizedName] = it - } + return resourceDrawableIdMap.get(normalizedName) ?: addDrawableId(context, normalizedName) } } + private fun addDrawableId(context: Context, normalizedName: String): Int { + val newId = context.resources.getIdentifier(normalizedName, "drawable", context.packageName) + resourceDrawableIdMap[normalizedName] = newId + return newId + } + 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