https://kotlinlang.org logo
#compose-android
Title
# compose-android
t

Tgo1014

10/27/2023, 8:11 AM
Is there a better way to have previews for the same theme with different params than having to add a
@PreviewParameter
to all the preview methods? Is it possible to create a custom preview annotation that does this?
Copy code
@Preview
@Composable
fun ComposeTest(
    @PreviewParameter(ThemeProvider::class) brand: Brand,
) = CustomTheme(brand) {
    CustomButton(onClick = { /*TODO*/ }) {
        Text(text = "Click me")
    }
}
s

Stylianos Gakis

10/27/2023, 8:54 AM
As far as I understand, the annotations do these changes by changing things in the activity level, like things coming from the OS in some way, here is some examples of the new previews to come for example https://twitter.com/Alex_Zhukovich/status/1717760551068250288?t=iuGO39zNuAbPJ2RfQwnH7A Since your brand is a completely in-app concept, I would guess that the annotation can't help you here and your option is what you suggested instead
t

Tgo1014

10/27/2023, 8:56 AM
Do you know if there’s some way to write some lambda where I could just pass the content and it would generate the previews? Maybe with some reflection stuff? I imagine not because of the annotation on the top of the method, but don’t hurt to ask hahaha
y

yschimke

10/27/2023, 8:58 AM
Probably worth a feature request.
I suspect code generation, rather than reflection. But I'm guessing.
Wear has been doing the font scales and Screen sizes for a while. Nice to see it adopted more generally.
Theme isn't possible via the same method. Locale is, but better in your app with a list of known supported locales.
t

Tgo1014

10/27/2023, 9:04 AM
Would be nice if we could create our custom preview annotation by code, but I’m not sure if it’s something that could be possible to expose an api
y

yschimke

10/27/2023, 10:32 AM
I think that's the feature request. I'm not sure why it couldn't be an exposed API. It's effectively like junit test runner annotations for previews. They may not do it, but it's valid request.
t

Tgo1014

10/27/2023, 10:39 AM
I’ve opened a request: https://issuetracker.google.com/issues/307987905 Give it a plus1 if you can 😄
1