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

jeff

01/04/2022, 5:52 PM
Anyone have recommendations for getting sample JSON usable by a Compose
@Preview
? We normally use Moshi to deserialize but that doesn't seem to work in previews because codegen hasn't run. I could construct the resulting (post-deserialization) object manually, but the JSON has enough nesting that it's very cumbersome.
m

mattinger

01/04/2022, 6:03 PM
I have a similar problem with glide in respect to previews. You could try using GSON to deserialize the json string inside of your preview code. It uses reflection, so it won’t have this issue. That said, if you have a bunch of complex mappings it might get hairy to create your own converters and stuff just for preview rendering.
The other option you have is just deploy the preview to an emulator. This is how i verify my glide stuff is working as intended. It’s not perfect, but it’s what i can do.
b

Bradleycorn

01/04/2022, 7:53 PM
We always deserialize the json at a much lower level, and just let our composables work with deserialized model data. Most of the time, I’ve found that if I’m trying to write a preview for a composable that takes a huge (complex/nested) model, I probably need to break it down more into smaller chunks that take in less data. But, there are times when that’s either not practical, or I want to preview the larger set of components together. In those cases, I usually create a
val
in the same file where the model is defined. then whenever I need to preview a composable that uses that model, I can just use that
val
. I’ve not gotten into the habit of just always creating a “preview val” every time I create a model, so I have it ready to go when I’m working with UI and need to write a preview. It’ also helps when I have a model that has complex/nested properties, because I can use their “preview vals” when constructing the higher level model’s “preview val”.
2
3 Views