Skip to content

Commit 2eaa36f

Browse files
authored
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) } ```
1 parent a8fe5a9 commit 2eaa36f

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
21
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
32

43
plugins {
@@ -31,6 +30,32 @@ kotlin {
3130
}
3231
macosX64()
3332
macosArm64()
33+
34+
applyDefaultHierarchyTemplate()
35+
36+
sourceSets {
37+
val commonMain by getting
38+
39+
androidMain.dependencies {
40+
api(libs.androidx.ui.tooling.preview)
41+
}
42+
43+
val nonAndroidMain by creating {
44+
dependsOn(commonMain)
45+
}
46+
47+
val appleMain by getting
48+
appleMain.dependsOn(nonAndroidMain)
49+
50+
val desktopMain by getting
51+
desktopMain.dependsOn(nonAndroidMain)
52+
53+
val jsMain by getting
54+
jsMain.dependsOn(nonAndroidMain)
55+
56+
val wasmJsMain by getting
57+
wasmJsMain.dependsOn(nonAndroidMain)
58+
}
3459
}
3560

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import kotlin.reflect.KClass
2222
* Interface to be implemented by any provider of values that you want to be injected as @[Preview]
2323
* parameters. This allows providing sample information for previews.
2424
*/
25-
interface PreviewParameterProvider<T> {
25+
expect interface PreviewParameterProvider<T> {
2626
/**
2727
* [Sequence] of values of type [T] to be passed as @[Preview] parameter.
2828
*/
@@ -31,7 +31,7 @@ interface PreviewParameterProvider<T> {
3131
/**
3232
* Returns the number of elements in the [values] [Sequence].
3333
*/
34-
val count get() = values.count()
34+
open val count: Int
3535
}
3636

3737
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jetbrains.compose.ui.tooling.preview
18+
19+
/**
20+
* Interface to be implemented by any provider of values that you want to be injected as @[Preview]
21+
* parameters. This allows providing sample information for previews.
22+
*/
23+
actual interface PreviewParameterProvider<T> {
24+
/**
25+
* [Sequence] of values of type [T] to be passed as @[Preview] parameter.
26+
*/
27+
actual val values: Sequence<T>
28+
29+
/**
30+
* Returns the number of elements in the [values] [Sequence].
31+
*/
32+
actual val count: Int
33+
get() = values.count()
34+
}

0 commit comments

Comments
 (0)