11package com.rcttabview
22
3+ import android.annotation.SuppressLint
34import android.content.Context
45import android.content.res.ColorStateList
56import android.graphics.Typeface
@@ -16,9 +17,7 @@ import android.view.View
1617import android.view.ViewGroup
1718import android.widget.TextView
1819import androidx.appcompat.content.res.AppCompatResources
19- import androidx.core.graphics.drawable.DrawableCompat
2020import com.bumptech.glide.load.DataSource
21- import com.bumptech.glide.load.DecodeFormat
2221import com.bumptech.glide.load.engine.GlideException
2322import com.bumptech.glide.request.RequestListener
2423import com.bumptech.glide.request.target.Target
@@ -30,8 +29,6 @@ import com.facebook.react.modules.core.ReactChoreographer
3029import com.facebook.react.views.imagehelper.ImageSource
3130import com.facebook.react.views.text.ReactTypefaceUtils
3231import com.google.android.material.bottomnavigation.BottomNavigationView
33- import kotlinx.coroutines.Dispatchers
34- import kotlinx.coroutines.runBlocking
3532
3633
3734class ReactBottomNavigationView (context : Context ) : BottomNavigationView(context) {
@@ -73,6 +70,7 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context
7370 override fun requestLayout () {
7471 super .requestLayout()
7572 @Suppress(" SENSELESS_COMPARISON" ) // layoutCallback can be null here since this method can be called in init
73+
7674 if (! isLayoutEnqueued && layoutCallback != null ) {
7775 isLayoutEnqueued = true
7876 // we use NATIVE_ANIMATED_MODULE choreographer queue because it allows us to catch the current
@@ -106,7 +104,9 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context
106104 val menuItem = getOrCreateItem(index, item.title)
107105 menuItem.isVisible = ! item.hidden
108106 if (icons.containsKey(index)) {
109- menuItem.icon = getDrawable(icons[index]!! )
107+ getDrawable(icons[index]!! ) {
108+ menuItem.icon = it
109+ }
110110 }
111111
112112 if (item.badge.isNotEmpty()) {
@@ -154,7 +154,9 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context
154154
155155 // Update existing item if exists.
156156 menu.findItem(idx)?.let { menuItem ->
157- menuItem.icon = getDrawable(imageSource)
157+ getDrawable(imageSource) {
158+ menuItem.icon = it
159+ }
158160 }
159161 }
160162 }
@@ -173,38 +175,35 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context
173175 itemRippleColor = color
174176 }
175177
176- private fun getDrawable (imageSource : ImageSource ): Drawable ? {
177- val uri = imageSource.uri.toString()
178- val isSvg = uri.contains(" .svg" , ignoreCase = true )
179- Log .d(" ReactBottomNav" , " Loading image: $uri , isSvg: $isSvg " )
180-
181- return try {
182- runBlocking(Dispatchers .IO ) {
183- val drawable = GlideApp .with (context)
184- .`as `(Drawable ::class .java)
185- .load(imageSource.uri)
186- .apply {
187- if (isSvg) {
188- override (200 , 200 )
189- }
190- }
191- .submit()
192- .get()
193-
194- // Make the drawable tintable
195- if (isSvg && drawable != null ) {
196- DrawableCompat .wrap(drawable.mutate()).apply {
197- DrawableCompat .setTintList(this , null ) // Clear any existing tint
198- alpha = 255
199- }
200- } else {
201- drawable
178+ @SuppressLint(" CheckResult" )
179+ private fun getDrawable (imageSource : ImageSource , onDrawableReady : (Drawable ? ) -> Unit ) {
180+ GlideApp .with (context)
181+ .`as `(Drawable ::class .java)
182+ .load(imageSource.uri)
183+ .listener(object : RequestListener <Drawable > {
184+ override fun onLoadFailed (
185+ e : GlideException ? ,
186+ model : Any? ,
187+ target : Target <Drawable >,
188+ isFirstResource : Boolean
189+ ): Boolean {
190+ Log .e(" RCTTabView" , " Error loading image: ${imageSource.uri} " , e)
191+ return false
202192 }
203- }
204- } catch (e: Exception ) {
205- Log .e(" ReactBottomNav" , " Error loading image: $uri " , e)
206- null
207- }
193+
194+ override fun onResourceReady (
195+ resource : Drawable ,
196+ model : Any ,
197+ target : Target <Drawable >? ,
198+ dataSource : DataSource ,
199+ isFirstResource : Boolean
200+ ): Boolean {
201+ // Update images on the main queue.
202+ post { onDrawableReady(resource) }
203+ return true
204+ }
205+ })
206+ .submit()
208207 }
209208
210209 override fun onDetachedFromWindow () {
0 commit comments