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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README with Kotlin support #159

Merged
merged 3 commits into from
Nov 6, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 68 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Intent intent = Henson.with(this)
.extra2(2)
.extra3(new User())
.build();

startActivity(intent);
```

Expand All @@ -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"
Expand Down Expand Up @@ -201,13 +201,13 @@ or maven
</dependency>
```

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.

Expand Down Expand Up @@ -239,6 +239,68 @@ Maven:
</dependency>
```

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

  • enthusiasts

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done ;)


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)'
Copy link
Contributor

Choose a reason for hiding this comment

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

you can replace with implementation as compile is deprecated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done and ready for merge ;)

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

Expand Down