Hey guys, i'm trying to build a preview with the p...
# compose
f
Hey guys, i'm trying to build a preview with the preview parameter but it's only shown in light mode even tho there's another preview parameter attached to it as it forces the UIMode to be night, tried making the preview composable not private, the preview class not internal, doesn't work, code in thread, AS info in the picture
1
Copy code
internal class OnDeviceFileModelPreviewParameterProvider :
    PreviewParameterProvider<OnDeviceFileModel> {
    override val values = sequenceOf(
        OnDeviceFileModel("Passport scan", System.currentTimeMillis(), "pdf"),
        OnDeviceFileModel("Annually report", System.currentTimeMillis() - 5000, "csv"),
        OnDeviceFileModel("My cool presentation", System.currentTimeMillis() - 1500, "pptx"),
        OnDeviceFileModel("Some scribbles", System.currentTimeMillis() - 25000, "txt"),
        OnDeviceFileModel("The book i never wrote", System.currentTimeMillis() - 12000, "docx"),
    )
}

@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Preview(showBackground = true)
@Composable
private fun FileItemComponentPreview(
    @PreviewParameter(OnDeviceFileModelPreviewParameterProvider::class) model: OnDeviceFileModel
) {
    FileItemComponent(model)
}
c
Hmm have you tried this in Arctic Fox Patch 4? That is the latest stable version and I can't seem to repro your issue.
f
still the same issue
c
I think you need to wrap the Composable in your Preview function with MaterialTheme, or your app theme. The Preview is a Composable function in isolation so it doesn't have awareness of theming until you wrap it. It's not needed when using the function in your app though since it will likely be in a hierarchy that ultimately has a theme at the top level
f
still nothing 😕
c
Hmm what version of Compose are you using? I tried your code almost exactly but still can't repro in AF Patch 4 with Compose 1.0.1.
This is the code I'm using
Copy code
@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

@Preview(showBackground = true, uiMode = UI_MODE_NIGHT_YES)
@Preview(showBackground = true)
@Composable
private fun DefaultPreview(
    @PreviewParameter(SamplePreviewParameterProvider::class) model: String
) {
    Greeting("$model")
}

internal class SamplePreviewParameterProvider : PreviewParameterProvider<String> {
    override val values = sequenceOf(
        "Today",
        "This week",
        "December 2021"
    )
}
This might be a dumb suggestion, but have you tried cleaning/rebuilding the project, invalidating caches -> restarting, or what I sometimes have to do is close and reopen the file to make the Preview rebuild properly?
f
1.1.0 rc 01 Yes I've tried, unfortunately nothing changes
c
Could you possibly share the full code of the Composable you are trying to preview?
f
you have the preview code already, this is the widget
c
Hmm I upgraded my Compose version and still can't repro. If it's not inconvenient, can you repro this on your machine in a simple project (assuming you can't share your source) and send it over as a zip? It would be really helpful for us to debug.
And last env question - what version is the Kotlin plugin in your Studio installation?
f
Sorry, can't share the project, if i do have more time I'll repro simple stuff, Kotlin 1.6.10