https://kotlinlang.org logo
#compose
Title
# compose
d

darkmoon_uk

11/17/2021, 8:33 PM
Any built-in way to determine if a
@Composable
is being presented in a
@Preview
or not? (Something in Locals?) I know it shouldn't care; but sometimes in the real world there are practical reasons. In this case the Coil SVG library is crashing in Previews for some class-loader related reason; I need some flag to suppress registering of the SVG handler in Previews so I can get on with building the rest of the View.
d

darkmoon_uk

11/17/2021, 8:46 PM
@Andrew Neal I don't think I need help using Coil - not sure how linking to that doco answers my question. Coil is already integrated and I'm using
LocalImageLoader
- Coils SVG renderer is just exhibiting an incompatibility with `Preview`s right now, and I need something like an
isRunningInPreviewMode
flag to suppress it.
d

darkmoon_uk

11/17/2021, 8:48 PM
Thank you @Ian Lake 🎉
a

Andrew Neal

11/17/2021, 8:50 PM
Are you not able to use a dummy
LocalImageLoader
in your preview to avoid loading the SVG?
👍 1
i

Ian Lake

11/17/2021, 9:04 PM
Yeah, what Andrew is suggesting is providing your own
LocalImageLoader
specifically wrapping the components you want to preview that is essentially a no-op / has preview specific behavior. That way your component itself doesn't need to know about whether it is in a preview or not
d

darkmoon_uk

11/17/2021, 9:11 PM
Thanks, yes, that's close to what I'm intending 👍 - I'm already providing my own
LocalImageLoader
from a top-level
@Composable
scope and identified that it's enough to just not register the SVG handler during its configuration. The rest of Coil's
ImageLoader
isn't a problem, and no components need to be aware; SVG's just won't be handled.
I'll try and dig into the issue the SVG Component has with Previews in a bit, and raise an appropriate issue. Unclear whether that would be with Coil or Compose atm.
(It's a `java.lang.NoClassDefFoundError: Could not initialize class coil.decode.SvgDecoder`even though that class is defined in the
implementation
classpath in Gradle and runtime is OK 🤷).
Workaround is working, went with providing a full 'dummy'
ImageLoader
in the end; thanks Andrew & Ian
y

yschimke

11/18/2021, 8:14 AM
I like your approach, I ended up conditionally setting a placeholder but I think I'll switch to your way. It seems worth raising a bug for SVG failing in previews.
Copy code
fun rememberAstronautPainter(person: Assignment?) =
    rememberImagePainter(person?.personImageUrl) {
        // Use the generic astronaut SVG for missing or error (404?).
        fallback(R.drawable.ic_american_astronaut)
        error(R.drawable.ic_american_astronaut)

        if (LocalInspectionMode.current) {
            // Show error image instead of blank in @Preview
            placeholder(R.drawable.ic_american_astronaut)
        }
    }
👍 1
d

darkmoon_uk

11/18/2021, 9:22 AM
Did you see the same issue as above with SVG in Preview, @yschimke?
y

yschimke

11/18/2021, 9:50 AM
No, but I have remote images which don't download in previews
👍 1
13 Views