Skip to content

Commit

Permalink
Avoid !! in ResourceDrawableHelper.kt (facebook#43820)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#43820

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D55701291

fbshipit-source-id: afdce3ffa59bc488d6c36d78a8cf3176089599ba
  • Loading branch information
Thomas Nardone authored and facebook-github-bot committed Apr 4, 2024
1 parent 70c3158 commit fd3ef7b
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fd3ef7b

Please sign in to comment.