since `painterResource` throws exception for some ...
# compose
f
since
painterResource
throws exception for some drawables and we can't write try/catch in
@Composable
functions, how can I work around not crashing my app?
painterResource
calls
loadVectorResource
who calls
loadVectorResource
Copy code
private fun loadVectorResource(theme: Resources.Theme, res: Resources, id: Int): ImageVector {
    @Suppress("ResourceType") val parser = res.getXml(id)
    if (parser.seekToStartTag().name != "vector") {
        throw IllegalArgumentException(errorMessage)
    }
    return loadVectorResourceInner(theme, res, parser)
}
It crashes for an image such as this
Copy code
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="<http://schemas.android.com/apk/res/android>">


    <item>
        <shape android:shape="oval">
            <solid android:color="@color/colorBardeenBrand" />
        </shape>
    </item>

</layer-list>
🧵 3
i
We'll, that isn't a
<vector>
resource, so the crash makes sense
f
not to me painterResource also loads things that are not vector
so what's the alternative? I could copy/paste the code for the private/internal functions and modify it to inflate shapes into bitmap and pass it to
Image
but wondering if there's an alternative
btw in other situations I could at least catch the exception, but is there something equivalent for
@Composable
? as a side note, if there's any docs/blogs/vlogs about the try/catch decision I'd be interested in reading it
f
I'm away from computer now Australia timezone) but will def stop there. Btw I've been using accompanist coil, curious to know why there's no network error placeholders? This is the exact use case I'm working here. I'll dive in accompanist GitHub issues by Monday btw
f
works like a charm! thx @cb It was a bit confusing because I can't remember seeing that in the docs, but the lib is super awesome. Btw we're going live with it on our app with
-coil
and
-pager
this week 🙂 , image placeholders are just the cherry on top
c
Awesome! 🦜
264 Views