Any built-in way to determine if a `@Composable` i...
# compose
d
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
@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
Thank you @Ian Lake ๐ŸŽ‰
a
Are you not able to use a dummy
LocalImageLoader
in your preview to avoid loading the SVG?
๐Ÿ‘ 1
i
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
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
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
Did you see the same issue as above with SVG in Preview, @yschimke?
y
No, but I have remote images which don't download in previews
๐Ÿ‘ 1