Browse Source

KMT-879: Make `org.jetbrains.compose.ui.tooling.preview.PreviewParameterProvider` an actual implementation of `androidx.compose.ui.tooling.preview.PreviewParameterProvider` on Android (#5319)

This is required for the preview parameters to be correctly picked up by
the preview adapter in IDE, which expects the provider to be a subclass
of AndroidX's interface, and fails with the `ClassCastException`
otherwise.

## Release Notes
### Fixes - Multiple Platforms
- Support Preview parameters for Previews in common source sets in IJ
and AS. Note: IDEs also need to implement support on their end. Please
check the respective IDE release notes to confirm this is supported.

Example usage:

```
import androidx.compose.runtime.Composable
import org.jetbrains.compose.ui.tooling.preview.Preview
import org.jetbrains.compose.ui.tooling.preview.PreviewParameter
import org.jetbrains.compose.ui.tooling.preview.PreviewParameterProvider

class MyPreviewParameterProvider : PreviewParameterProvider<String> {
    override val values = sequenceOf("Hello, Compose!", "Hello, World!")
}

/**
 * This function will generate two preview images with different texts.
 */
@Preview
@Composable
fun MyPreview(@PreviewParameter(MyPreviewParameterProvider::class) text: String) {
    Text(text)
}
```
pull/5328/head v1.9.0+dev2496
Ilya Bogdanovich 7 months ago committed by GitHub
parent
commit
2eaa36f973
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 27
      components/ui-tooling-preview/library/build.gradle.kts
  2. 23
      components/ui-tooling-preview/library/src/androidMain/kotlin/org/jetbrains/compose/ui/tooling/preview/PreviewParameter.android.kt
  3. 4
      components/ui-tooling-preview/library/src/commonMain/kotlin/org/jetbrains/compose/ui/tooling/preview/PreviewParameter.kt
  4. 34
      components/ui-tooling-preview/library/src/nonAndroidMain/kotlin/org/jetbrains/compose/ui/tooling/preview/PreviewParameter.nonAndroid.kt

27
components/ui-tooling-preview/library/build.gradle.kts

@ -1,4 +1,3 @@ @@ -1,4 +1,3 @@
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
plugins {
@ -31,6 +30,32 @@ kotlin { @@ -31,6 +30,32 @@ kotlin {
}
macosX64()
macosArm64()
applyDefaultHierarchyTemplate()
sourceSets {
val commonMain by getting
androidMain.dependencies {
api(libs.androidx.ui.tooling.preview)
}
val nonAndroidMain by creating {
dependsOn(commonMain)
}
val appleMain by getting
appleMain.dependsOn(nonAndroidMain)
val desktopMain by getting
desktopMain.dependsOn(nonAndroidMain)
val jsMain by getting
jsMain.dependsOn(nonAndroidMain)
val wasmJsMain by getting
wasmJsMain.dependsOn(nonAndroidMain)
}
}
android {

23
components/ui-tooling-preview/library/src/androidMain/kotlin/org/jetbrains/compose/ui/tooling/preview/PreviewParameter.android.kt

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.compose.ui.tooling.preview
/**
* Interface to be implemented by any provider of values that you want to be injected as @[Preview]
* parameters. This allows providing sample information for previews.
*/
actual typealias PreviewParameterProvider<T> = androidx.compose.ui.tooling.preview.PreviewParameterProvider<T>

4
components/ui-tooling-preview/library/src/commonMain/kotlin/org/jetbrains/compose/ui/tooling/preview/PreviewParameter.kt

@ -22,7 +22,7 @@ import kotlin.reflect.KClass @@ -22,7 +22,7 @@ import kotlin.reflect.KClass
* Interface to be implemented by any provider of values that you want to be injected as @[Preview]
* parameters. This allows providing sample information for previews.
*/
interface PreviewParameterProvider<T> {
expect interface PreviewParameterProvider<T> {
/**
* [Sequence] of values of type [T] to be passed as @[Preview] parameter.
*/
@ -31,7 +31,7 @@ interface PreviewParameterProvider<T> { @@ -31,7 +31,7 @@ interface PreviewParameterProvider<T> {
/**
* Returns the number of elements in the [values] [Sequence].
*/
val count get() = values.count()
open val count: Int
}
/**

34
components/ui-tooling-preview/library/src/nonAndroidMain/kotlin/org/jetbrains/compose/ui/tooling/preview/PreviewParameter.nonAndroid.kt

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.compose.ui.tooling.preview
/**
* Interface to be implemented by any provider of values that you want to be injected as @[Preview]
* parameters. This allows providing sample information for previews.
*/
actual interface PreviewParameterProvider<T> {
/**
* [Sequence] of values of type [T] to be passed as @[Preview] parameter.
*/
actual val values: Sequence<T>
/**
* Returns the number of elements in the [values] [Sequence].
*/
actual val count: Int
get() = values.count()
}
Loading…
Cancel
Save