Skip to content

Commit 7da91b7

Browse files
committed
add examples, make loading async on Android
1 parent 5a52f30 commit 7da91b7

File tree

13 files changed

+140
-84
lines changed

13 files changed

+140
-84
lines changed

android/src/main/java/com/rcttabview/RCTTabView.kt

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.rcttabview
22

3+
import android.annotation.SuppressLint
34
import android.content.Context
45
import android.content.res.ColorStateList
56
import android.graphics.Typeface
@@ -106,7 +107,9 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context
106107
val menuItem = getOrCreateItem(index, item.title)
107108
menuItem.isVisible = !item.hidden
108109
if (icons.containsKey(index)) {
109-
menuItem.icon = getDrawable(icons[index]!!)
110+
getDrawable(icons[index]!!) {
111+
menuItem.icon = it
112+
}
110113
}
111114

112115
if (item.badge.isNotEmpty()) {
@@ -154,7 +157,9 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context
154157

155158
// Update existing item if exists.
156159
menu.findItem(idx)?.let { menuItem ->
157-
menuItem.icon = getDrawable(imageSource)
160+
getDrawable(imageSource) {
161+
menuItem.icon = it
162+
}
158163
}
159164
}
160165
}
@@ -173,38 +178,34 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context
173178
itemRippleColor = color
174179
}
175180

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
181+
@SuppressLint("CheckResult")
182+
private fun getDrawable(imageSource: ImageSource, onDrawableReady: (Drawable?) -> Unit) {
183+
GlideApp.with(context)
184+
.`as`(Drawable::class.java)
185+
.load(imageSource.uri)
186+
.listener(object : RequestListener<Drawable> {
187+
override fun onLoadFailed(
188+
e: GlideException?,
189+
model: Any?,
190+
target: Target<Drawable>,
191+
isFirstResource: Boolean
192+
): Boolean {
193+
Log.e("ReactBottomNav", "Error loading image: ${imageSource.uri}", e)
194+
return false
202195
}
203-
}
204-
} catch (e: Exception) {
205-
Log.e("ReactBottomNav", "Error loading image: $uri", e)
206-
null
207-
}
196+
197+
override fun onResourceReady(
198+
resource: Drawable,
199+
model: Any,
200+
target: Target<Drawable>?,
201+
dataSource: DataSource,
202+
isFirstResource: Boolean
203+
): Boolean {
204+
post { onDrawableReady(resource) }
205+
return true
206+
}
207+
})
208+
.submit()
208209
}
209210

210211
override fun onDetachedFromWindow() {

android/src/main/java/com/rcttabview/SVGDrawableTranscoder.kt

Lines changed: 0 additions & 44 deletions
This file was deleted.

android/src/main/java/com/rcttabview/SVGDecoder.kt renamed to android/src/main/java/com/rcttabview/svg/SVGDecoder.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.rcttabview
1+
package com.rcttabview.svg
22

33
import com.bumptech.glide.load.Options
44
import com.bumptech.glide.load.ResourceDecoder
@@ -9,24 +9,29 @@ import com.caverock.androidsvg.SVGParseException
99
import java.io.IOException
1010
import java.io.InputStream
1111

12+
1213
class SVGDecoder : ResourceDecoder<InputStream, SVG> {
1314
override fun handles(source: InputStream, options: Options) = true
1415

16+
companion object {
17+
const val DEFAULT_SIZE = 40f
18+
}
19+
1520
@Throws(IOException::class)
1621
override fun decode(source: InputStream, width: Int, height: Int, options: Options): Resource<SVG>? {
1722
return try {
1823
val svg: SVG = SVG.getFromInputStream(source)
19-
// Use document width and height if view box is not set.
20-
// Later, we will override the document width and height with the dimensions of the native view.
24+
// Taken from https://github.com/expo/expo/blob/215d8a13a7ef3f0b36b14eead41291e2d2d6cd0c/packages/expo-image/android/src/main/java/expo/modules/image/svg/SVGDecoder.kt#L28
2125
if (svg.documentViewBox == null) {
2226
val documentWidth = svg.documentWidth
2327
val documentHeight = svg.documentHeight
2428
if (documentWidth != -1f && documentHeight != -1f) {
2529
svg.setDocumentViewBox(0f, 0f, documentWidth, documentHeight)
2630
}
2731
}
28-
svg.documentWidth = width.toFloat()
29-
svg.documentHeight = height.toFloat()
32+
33+
svg.documentWidth = DEFAULT_SIZE
34+
svg.documentHeight = DEFAULT_SIZE
3035
SimpleResource(svg)
3136
} catch (ex: SVGParseException) {
3237
throw IOException("Cannot load SVG from stream", ex)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.rcttabview.svg
2+
3+
import android.content.Context
4+
import android.graphics.Bitmap
5+
import android.graphics.Canvas
6+
import android.graphics.drawable.BitmapDrawable
7+
import android.graphics.drawable.Drawable
8+
import android.graphics.drawable.PictureDrawable
9+
import com.bumptech.glide.load.Options
10+
import com.bumptech.glide.load.engine.Resource
11+
import com.bumptech.glide.load.resource.SimpleResource
12+
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder
13+
import com.caverock.androidsvg.SVG
14+
15+
class SVGDrawableTranscoder(val context: Context) : ResourceTranscoder<SVG?, Drawable> {
16+
override fun transcode(toTranscode: Resource<SVG?>, options: Options): Resource<Drawable> {
17+
val svg = toTranscode.get()
18+
val picture = svg.renderToPicture()
19+
val drawable = PictureDrawable(picture)
20+
21+
val returnedBitmap = Bitmap.createBitmap(
22+
drawable.intrinsicWidth,
23+
drawable.intrinsicHeight,
24+
Bitmap.Config.ARGB_8888
25+
)
26+
27+
val canvas = Canvas(returnedBitmap)
28+
canvas.drawPicture(drawable.picture)
29+
val bitMapDrawable = BitmapDrawable(context.resources, returnedBitmap)
30+
return SimpleResource(bitMapDrawable)
31+
}
32+
}

android/src/main/java/com/rcttabview/TabViewGlideModule.kt renamed to android/src/main/java/com/rcttabview/svg/TabViewGlideModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.rcttabview
1+
package com.rcttabview.svg
22

33
import android.content.Context
44
import android.graphics.drawable.Drawable
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

example/assets/icons/newspaper.svg

Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)