Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation regarding Custom preview annotation usage 馃ぉ 馃摐 #307

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,81 @@ fun MyComposable() {
Name and group are optional. Look at the [properties section](#showkasecomposable-currently-supports-the-following-properties)
to understand the behavior when you don't pass any properties.

### Custom Preview Annotations
You can use custom preview annotations for Showkase as well. Custom preview annotations are annotations that are annotated with preview it self.
An example of this can be:

```kt
@Preview(name = "Custom Preview One First", group = "Custom Previews")
@Preview(name = "Custom Preview One Second", group = "Custom Previews")
annotation class CustomPreview1
```

```kt
@Preview(name = "Custom Preview One First", group = "Custom Previews")
annotation class CustomPreview2
```

this can be used to annotate a composable function like you would do any other preview function in Compose like this:

```kt
@CustomPreview1
@Composable
fun CustomAnnotationPreview() {
}
```

This can also be combined like this:

```kt
@Preview(name = "Custom Preview One First", group = "Custom Previews")
@Preview(name = "Custom Preview One Second", group = "Custom Previews")
annotation class CustomPreviewOne


@Preview(name = "Custom Preview Two First", group = "Custom Previews")
annotation class CustomPreviewTwo

@CustomPreviewOne
@CustomPreviewTwo
@Composable
fun CustomAnnotationPreviewCombined() {
}
```

### KAPT vs KSP

Because of [this issue](https://youtrack.jetbrains.com/issue/KT-49682/Support-JVM-IR-in-KAPT-stub-generation) KAPT does not quite support repeatable annotations from Kotlin. This means that if you have an annotation class like:

```kt
@Preview(name = "Shape 100 by 100", group = "Shapes", widthDp = 100, heightDp = 100)
@Preview(name = "Shape 150 by 150", group = "Shapes", widthDp = 150, heightDp = 150)
annotation class CustomShape
```

This will be skipped by KAPT, but KSP will pick it up. However, if you have an annotation like:

```kt
@Preview(name = "Shape 100 by 100", group = "Shapes", widthDp = 100, heightDp = 100)
annotation class CustomShape
```
It will be picked up by both KSP and KAPT.

#### Important for KAPT users

You will need to provide a compiler arg in you module for the custom preview annotations that you are using and expecting to be picked up by Showkase. This can be done with the following code:

```kt
kapt {
arguments {
arg("multiPreviewType", "com.airbnb.android.submodule.showkasesample.LocalePreview")
}
}
```

It is important to remember to use the whole qualified name of the annotation, and not just the name.

### Further usage
**Note:** Make sure that you add this annotation to only those functions that meet the following criteria:
- Functions that don't have any parameters
- If it does have a parameter, it has to be annotated with `@PreviewParameter` that is provided a `PreviewParameterProvider` implementation.
Expand Down