From 491fac78a4345942816f8c7a385a2177d30954ba Mon Sep 17 00:00:00 2001 From: StephenVinouze Date: Wed, 24 May 2017 11:05:20 +0200 Subject: [PATCH 1/3] Update README with Kotlin support --- README.md | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 46d7f47d..a853031b 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ Intent intent = Henson.with(this) .extra2(2) .extra3(new User()) .build(); - + startActivity(intent); ``` @@ -88,13 +88,13 @@ class AnotherActivity extends Activity { } ``` -The Henson annotation processor will generate the `Henson` navigator class (used above) in a package that is : +The Henson annotation processor will generate the `Henson` navigator class (used above) in a package that is : * either the package specified by the `dart.henson.package` annotation processor option * or if no such option is used, in the common package of all annotated activities. See the Javadoc of `HensonExtraProcessor` for more details. - + If your activites and fragment are in *different packages*, you will need to specify a package via the `dart.henson.package` annotation processor option. If you're using gradle, simply add this to your `build.gradle` -```groovy +```groovy apt { arguments { "dart.henson.package" "your.package.name" @@ -201,13 +201,13 @@ or maven ``` -And for using Henson : +And for using Henson : Gradle: ```groovy compile 'com.f2prateek.dart:henson:(insert latest version)' provided 'com.f2prateek.dart:henson-processor:(insert latest version)' ``` -When using Henson, as Android Studio doesn't call live annotation processors when editing a file, you might prefer using the [apt Android Studio plugin](https://bitbucket.org/hvisser/android-apt). It will allow you to use the Henson generated DSL right away when you edit your code. +When using Henson, as Android Studio doesn't call live annotation processors when editing a file, you might prefer using the [apt Android Studio plugin](https://bitbucket.org/hvisser/android-apt). It will allow you to use the Henson generated DSL right away when you edit your code. The Henson annotation processor dependency would then have to be declared within the apt scope instead of provided. @@ -239,6 +239,68 @@ Maven: ``` +Kotlin +----- +For all Kotlin enthousiasts, you may wonder how to use this library to configure your intents. This is perfectly compatible, with a bit of understanding of how Kotlin works, especially when it comes to annotation processing. + +Assuming that your project is already configured with Kotlin, update your `build.gradle` file : + +```groovy +apply plugin: 'kotlin-kapt' + +dependencies { + compile 'com.f2prateek.dart:henson:(insert latest version)' + kapt 'com.f2prateek.dart:henson-processor:(insert latest version)' +} +``` + +Now you can use `@InjectExtra` annotation to generate either non-null or nullables properties : + +```kotlin +class ExampleActivity : Activity() { + @InjectExtra + lateinit var title: String + + @InjectExtra + var titleDefaultValue: String = "Default Title" + + @Nullable + @JvmField + @InjectExtra + var titleNullable: String? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.simple_activity) + Dart.inject(this) + // TODO Use "injected" extras... + } +} +``` + +Note that you still need to use the Java `@Nullable` annotation otherwise Henson won't interpret your property as nullable and will generate a builder with a mandatory field (even though you declared your property as nullable with the "?" Kotlin marker). Finally, you have to add the `@JvmField` annotation or your compiler will complain about not having a backing field. + +You may need to add an argument to your `build.gradle` file if your activities and fragments are located in different packages as mentioned above. The Kotlin syntax with **kapt** is : + +```groovy +kapt { + arguments { + arg("dart.henson.package", "your.package.name") + } +} +``` + +Finally, if you are using Parceler that comes built-in with this library, the syntax does not change from Java, except when dealing with **data** classes. Because Parceler requires a default constructor with no argument, here is how you need to declare your **data** class : + +```kotlin +@Parcel(Parcel.Serialization.BEAN) +data class ParcelExample @ParcelConstructor constructor( + val id: Int, + val name: String, + ... +} +``` + Talks & Slides ------- From 011697831ea04023335f4c031dd109718289cb10 Mon Sep 17 00:00:00 2001 From: StephenVinouze Date: Sat, 9 Sep 2017 09:41:22 +0200 Subject: [PATCH 2/3] Typo fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a853031b..d56a6de3 100644 --- a/README.md +++ b/README.md @@ -241,7 +241,7 @@ Maven: Kotlin ----- -For all Kotlin enthousiasts, you may wonder how to use this library to configure your intents. This is perfectly compatible, with a bit of understanding of how Kotlin works, especially when it comes to annotation processing. +For all Kotlin enthusiasts, you may wonder how to use this library to configure your intents. This is perfectly compatible, with a bit of understanding of how Kotlin works, especially when it comes to annotation processing. Assuming that your project is already configured with Kotlin, update your `build.gradle` file : From aa71696f1be54861c11dc7fe2f639083dfb7e483 Mon Sep 17 00:00:00 2001 From: Stephen Vinouze Date: Mon, 6 Nov 2017 15:43:19 +0100 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d56a6de3..81e8847b 100644 --- a/README.md +++ b/README.md @@ -249,7 +249,7 @@ Assuming that your project is already configured with Kotlin, update your `build apply plugin: 'kotlin-kapt' dependencies { - compile 'com.f2prateek.dart:henson:(insert latest version)' + implementation 'com.f2prateek.dart:henson:(insert latest version)' kapt 'com.f2prateek.dart:henson-processor:(insert latest version)' } ```