Search code examples
androidkotlinandroid-jetpack-composeandroid-jetpack-compose-preview

how to using a injected variable in compose preview


I have a fragment which has an injected variable timeZoneId, which now I have to pass to a composable.

@Inject
lateinit var timeZoneId: ZoneId

In Composable I have a preview, how can I add this to the preview as a parameter please.

@Composable
fun EditDepTime(
    timeZoneId: ZoneId,
    onCancelEditDepTime: () -> Unit
) {

}

@Preview
@Composable
fun EditDepartureTimePreview() {
    OhmeM3Theme() {
        EditDepartureTime(
            ZoneId, //Need to pass timezoneId here
            {}
        )
    }
}

What do I need to pass to get the preview to work please

Thanks R


Solution

  • You need to provide some sample value to the preview because it doesn't have access to runtime values. Here you could put any ZoneId you like, for example:

    @Preview
    @Composable
    fun EditDepartureTimePreview() {
        OhmeM3Theme() {
            EditDepartureTime(
                ZoneId.systemDefault(),
                {}
            )
        }
    }