Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 20, 2025

Conversation

boiler23
Copy link
Collaborator

@boiler23 boiler23 commented May 14, 2025

Fixes https://youtrack.jetbrains.com/issue/CMP-8220

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)
}

…terProvider` an actual implementation of `androidx.compose.ui.tooling.preview.PreviewParameterProvider` on Android.

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 the AndroidX's interface, and fails with the ClassCaseException otherwise.
@boiler23 boiler23 requested review from pjBooms and igordmn May 14, 2025 13:24
@@ -4,6 +4,7 @@ androidx-appcompat = "1.6.1"
androidx-activity-compose = "1.8.2"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Highlights - Android

  • It looks not major feature. Maybe even it is a bugfix, as it is available, but doesn't work?
  • It is not only for Android, I assume (as it available in common)

So, please change it to "Fixes - Multiple Platforms" or to "Features - Multiple Platforms" if it isn't a bugfix.

org.jetbrains.compose.ui.tooling.preview.PreviewParameterProvider becomes an alias to androidx.compose.ui.tooling.preview.PreviewParameterProvider on Android.

This describes the change from implementation details perspective, but it should describe the change from user perspective. What is changed for them? org.jetbrains.compose.ui.tooling.preview.PreviewParameterProvider now works in IntelliJ IDEs?

Could you also add a code snippet to the release notes on how to use it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the release notes, please take a look if that works better now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I rewrote it more, please check

@@ -52,3 +52,7 @@ configureMavenPublication(
name = "Experimental Compose Multiplatform tooling library API. This library provides the API required to declare " +
"@Preview composables in user apps."
)

dependencies {
compileOnly(libs.androidx.ui.tooling.preview.android)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this mean, that this will crash:

class SampleUserProvider: PreviewParameterProvider<User> {
    override val values = sequenceOf(User("Jens",31),User("Jim",44))
}

If users don't include it explicitly?

If so, we have to fix it. The common preview ideally should require including only it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will crash regardless. Rendering previews in Android requires ui-tooling-preview from AndroidX to be on the classpath. However, I agree that changing it to implementation might require the user to have fewer dependencies. On the other hand, what would happen if there is a conflict between the user project's version of the ui-tooling-preview and this one? I assume a compileOnly dependency here might be a more reliable approach from this perspective.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to implementation, as we discussed in DMs.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the implementation doesn't work for the expect way if there is no Android preview in the classpath:
image
image.

Let's change it to api:

api(libs.androidx.ui.tooling.preview.android)

It adds another issue for the common preview - it exposes 2 API for previews in androidMain (2 Preview annotations and 2 PreviewParameter classes). But current users already see 2 APIs as there is no way to enable Preview other than including debugImplementation(compose.uiTooling)

If anything goes wrong, we can revert it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MatkovIvan, as it is new "API", please review it as well

Copy link
Collaborator

@igordmn igordmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

…wParameterProvider` an actual implementation of `androidx.compose.ui.tooling.preview.PreviewParameterProvider` on Android.
…wParameterProvider` an actual implementation of `androidx.compose.ui.tooling.preview.PreviewParameterProvider` on Android.
…wParameterProvider` an actual implementation of `androidx.compose.ui.tooling.preview.PreviewParameterProvider` on Android.
@igordmn
Copy link
Collaborator

igordmn commented May 20, 2025

LGTM after the change implementation -> api

…wParameterProvider` an actual implementation of `androidx.compose.ui.tooling.preview.PreviewParameterProvider` on Android.
@igordmn igordmn requested a review from MatkovIvan May 20, 2025 11:36
…wParameterProvider` an actual implementation of `androidx.compose.ui.tooling.preview.PreviewParameterProvider` on Android.
@igordmn igordmn merged commit 2eaa36f into master May 20, 2025
1 check passed
@igordmn igordmn deleted the ilia.bogdanovich/KMT-879-preview-parameters branch May 20, 2025 12:10
boiler23 added a commit that referenced this pull request May 20, 2025
…terProvider` 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)
}
```
Kpotko added a commit that referenced this pull request May 20, 2025
…terProvider` an actual implementation of `androidx.compose.ui.tooling.preview.PreviewParameterProvider` on Android (#5319) (#5323)

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)
}
```

Describe proposed changes and the issue being fixed

(Optional) Fixes https://youtrack.jetbrains.com/issue/CMP-8220

## Testing
Once https://youtrack.jetbrains.com/issue/KMT-879 is merged in IJ, this
change can be tested on Nightly Builds (ETA tomorrow)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants