Thread
#compose
    d

    darkmoon_uk

    10 months ago
    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.
    a

    Andrew Neal

    10 months ago
    d

    darkmoon_uk

    10 months ago
    @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

    10 months ago
    Thank you @Ian Lake 🎉
    a

    Andrew Neal

    10 months ago
    Are you not able to use a dummy
    LocalImageLoader
    in your preview to avoid loading the SVG?
    i

    Ian Lake

    10 months ago
    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

    10 months ago
    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.SvgDecodereven 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

    10 months ago
    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.
    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)
            }
        }
    d

    darkmoon_uk

    10 months ago
    Did you see the same issue as above with SVG in Preview, @yschimke?
    y

    yschimke

    10 months ago
    No, but I have remote images which don't download in previews