Is there any way to skip code by checking if I'm c...
# compose
p
Is there any way to skip code by checking if I'm currently in the preview? I have code that uses
EmojiCompat.get()
and that breaks the preview because EmojiCompat wasnt initialized because as we're inside the preview the application wasn't created. (sth like
View.isInEditMode
but statically available?)
a
Can always declare your own CompositionLocal for it
p
My code is completely compose unrelated
It's inside an
EmojiDrawable
class
I think this is something that compose needs in some way
m
Is your composable attempting to use your
EmojiDrawable
directly? If so, could you change it such that it receives the drawable (or supplier of drawables) as a parameter? Then, if you set up a default value of the parameter that does not use
EmojiDrawable
itself, the preview should be isolated from
EmojiDrawable
and should not need
EmojiCompat
. Or, as Adam suggests, perhaps your supplier of drawables could be provided by a
CompositionLocal
, with a default implementation that does not depend upon
EmojiCompat
. The preview would use that default implementation, and higher-level composables would provide the
EmojiDrawable
-based implementation. On the whole, the advice seems to be: if you are encountering preview problems because of platform considerations, your composable is insufficiently isolated from the platform.
p
Hm that issue happens in various places. For example it's the same with accompanist image integrations. That makes me think that this needs some form of global handling
Found it. It's called
LocalInspectionMode
🙂
👍 2
Copy code
@Composable
internal fun rememberEmojiPainter(emoji: Emoji): Painter {
  val useEmojiCompat = !LocalInspectionMode.current
  return remember(emoji) {
    val drawable = EmojiDrawable(emoji, useEmojiCompat = useEmojiCompat)
    DrawablePainter(drawable)
  }
}