diff --git a/README.md b/README.md index 551bbaa..8cddd21 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,22 @@ public class MyActivity extends NaviActivity { } ``` +If you want some kotlin goodness, you can use bind() syntax + +```java +myObservable + .bindToLifecycle(myView) + .subscribe { } + +``` + +```java +myObservable + .bindUntilEvent(myRxActivity, STOP) + .subscribe { } + +``` + ## Unsubscription RxLifecycle does not actually unsubscribe the sequence. It terminates it by emitting `onCompleted()`, which ends the @@ -91,6 +107,9 @@ compile 'com.trello:rxlifecycle-components:0.4.0' // If you want to use Navi for providers compile 'com.trello:rxlifecycle-navi:0.4.0' + +// If you wat to use Kotlin syntax +compile 'com.trello:rxlifecycle-kotlin:0.4.0' ``` ## License diff --git a/build.gradle b/build.gradle index b68a984..0436f4d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,9 +1,15 @@ buildscript { + ext { + //version here to share between build script and projects + //todo https://github.com/JetBrains/kotlin/pull/775 : remove if it will be merged + verKotlin = '1.0.0' + } repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.5.0' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$verKotlin" } } @@ -22,6 +28,7 @@ ext { rxJava = 'io.reactivex:rxjava:1.0.16' rxBinding = 'com.jakewharton.rxbinding:rxbinding:0.3.0' navi = 'com.trello:navi:0.1.3' + kotlinStdlib = "org.jetbrains.kotlin:kotlin-stdlib:$verKotlin" appCompat = 'com.android.support:appcompat-v7:23.1.1' junit = 'junit:junit:4.12' mockito = 'org.mockito:mockito-core:1.10.19' diff --git a/rxlifecycle-kotlin/build.gradle b/rxlifecycle-kotlin/build.gradle new file mode 100644 index 0000000..e366627 --- /dev/null +++ b/rxlifecycle-kotlin/build.gradle @@ -0,0 +1,23 @@ +apply plugin: 'com.android.library' +apply plugin: 'kotlin-android' + +android { + compileSdkVersion rootProject.ext.compileSdkVersion + buildToolsVersion rootProject.ext.buildToolsVersion + + defaultConfig { + minSdkVersion rootProject.ext.minSdkVersion + } +} + +repositories { + mavenCentral() +} + +dependencies { + compile kotlinStdlib + compile project(':rxlifecycle') +} + +apply from: "$rootDir/gradle/artifacts.gradle" +apply from: "$rootDir/gradle/gradle-mvn-push.gradle" \ No newline at end of file diff --git a/rxlifecycle-kotlin/gradle.properties b/rxlifecycle-kotlin/gradle.properties new file mode 100644 index 0000000..2aa2a96 --- /dev/null +++ b/rxlifecycle-kotlin/gradle.properties @@ -0,0 +1,4 @@ +POM_NAME=RxLifecycle-kotlin +POM_DESCRIPTION=RxLifecycle-kotlin +POM_ARTIFACT_ID=rxlifecycle-kotlin +POM_PACKAGING=aar \ No newline at end of file diff --git a/rxlifecycle-kotlin/src/main/AndroidManifest.xml b/rxlifecycle-kotlin/src/main/AndroidManifest.xml new file mode 100644 index 0000000..27babe1 --- /dev/null +++ b/rxlifecycle-kotlin/src/main/AndroidManifest.xml @@ -0,0 +1,13 @@ + + + \ No newline at end of file diff --git a/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle/kotlin/rxlifecycle.kt b/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle/kotlin/rxlifecycle.kt new file mode 100644 index 0000000..4860a79 --- /dev/null +++ b/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle/kotlin/rxlifecycle.kt @@ -0,0 +1,29 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.trello.rxlifecycle.kotlin + +import android.view.View +import com.trello.rxlifecycle.* +import rx.Observable + +fun Observable.bindToLifecycle(activity: ActivityLifecycleProvider): Observable = this.compose(activity.bindToLifecycle()) + +fun Observable.bindUntilEvent(activity: ActivityLifecycleProvider, event: ActivityEvent): Observable = this.compose(activity.bindUntilEvent(event)) + +fun Observable.bindToLifecycle(fragment: FragmentLifecycleProvider): Observable = this.compose(fragment.bindToLifecycle()) + +fun Observable.bindUntilEvent(fragment: FragmentLifecycleProvider, event: FragmentEvent): Observable = this.compose(fragment.bindUntilEvent(event)) + +fun Observable.bindToLifecycle(view: View): Observable = this.compose(RxLifecycle.bindView(view)) \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index bc1e01c..d7a79b8 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,5 @@ include ':rxlifecycle' include ':rxlifecycle-components' include ':rxlifecycle-navi' +include ':rxlifecycle-kotlin' include ':rxlifecycle-sample' \ No newline at end of file