Skip to content

Commit e5090e7

Browse files
oas004vinaygaba
andauthored
Add documentation regarding Custom preview annotation usage 🤩 📜 (#307)
* Add documentation regarding Custom preview annotation usage Her I have added documentation for using Custom preview annotation with Showkase. I have added use cases and KSP vs KAPT differences. Also added docs about how to use this with KAPT. * Moved this documentation to an alternate section README.md --------- Co-authored-by: Vinay Gaba <vinaygaba@gmail.com>
1 parent ec48619 commit e5090e7

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,80 @@ val typography = metadata.typographyList
386386

387387
```
388388

389+
##### 6. Custom/Multipreview Annotations
390+
You can use custom preview annotations for Showkase as well. Custom preview annotations are annotations that are annotated with preview it self.
391+
An example of this can be:
392+
393+
```kt
394+
@Preview(name = "Custom Preview One First", group = "Custom Previews")
395+
@Preview(name = "Custom Preview One Second", group = "Custom Previews")
396+
annotation class CustomPreview1
397+
```
398+
399+
```kt
400+
@Preview(name = "Custom Preview One First", group = "Custom Previews")
401+
annotation class CustomPreview2
402+
```
403+
404+
this can be used to annotate a composable function like you would do any other preview function in Compose like this:
405+
406+
```kt
407+
@CustomPreview1
408+
@Composable
409+
fun CustomAnnotationPreview() {
410+
}
411+
```
412+
413+
This can also be combined like this:
414+
415+
```kt
416+
@Preview(name = "Custom Preview One First", group = "Custom Previews")
417+
@Preview(name = "Custom Preview One Second", group = "Custom Previews")
418+
annotation class CustomPreviewOne
419+
420+
421+
@Preview(name = "Custom Preview Two First", group = "Custom Previews")
422+
annotation class CustomPreviewTwo
423+
424+
@CustomPreviewOne
425+
@CustomPreviewTwo
426+
@Composable
427+
fun CustomAnnotationPreviewCombined() {
428+
}
429+
```
430+
431+
###### KAPT vs KSP
432+
433+
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:
434+
435+
```kt
436+
@Preview(name = "Shape 100 by 100", group = "Shapes", widthDp = 100, heightDp = 100)
437+
@Preview(name = "Shape 150 by 150", group = "Shapes", widthDp = 150, heightDp = 150)
438+
annotation class CustomShape
439+
```
440+
441+
This will be skipped by KAPT, but KSP will pick it up. However, if you have an annotation like:
442+
443+
```kt
444+
@Preview(name = "Shape 100 by 100", group = "Shapes", widthDp = 100, heightDp = 100)
445+
annotation class CustomShape
446+
```
447+
It will be picked up by both KSP and KAPT.
448+
449+
###### Important for KAPT users
450+
451+
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:
452+
453+
```kt
454+
kapt {
455+
arguments {
456+
arg("multiPreviewType", "com.airbnb.android.submodule.showkasesample.LocalePreview")
457+
}
458+
}
459+
```
460+
461+
It is important to remember to use the whole qualified name of the annotation, and not just the name.
462+
389463
## R8 / ProGuard
390464

391465
If you use Showkase as a dependency in an Android project which uses R8 as a default compiler

0 commit comments

Comments
 (0)