From 423665a2e14f960e22be027f940e99e9006490ef Mon Sep 17 00:00:00 2001 From: Dmitriy Voronin Date: Mon, 15 Feb 2016 13:18:52 +0300 Subject: [PATCH 1/2] rxlifecycle-kotlin module added with bind() syntax --- README.md | 12 +++++++ build.gradle | 7 +++++ rxlifecycle-kotlin/build.gradle | 31 +++++++++++++++++++ rxlifecycle-kotlin/gradle.properties | 4 +++ .../src/main/AndroidManifest.xml | 13 ++++++++ .../trello/rxlifecycle/kotlin/rxlifecycle.kt | 13 ++++++++ settings.gradle | 2 +- 7 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 rxlifecycle-kotlin/build.gradle create mode 100644 rxlifecycle-kotlin/gradle.properties create mode 100644 rxlifecycle-kotlin/src/main/AndroidManifest.xml create mode 100644 rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle/kotlin/rxlifecycle.kt diff --git a/README.md b/README.md index 551bbaa..a4aca4f 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,15 @@ public class MyActivity extends NaviActivity { } ``` +If you want some kotlin goodness, you can use bind() syntax + +```java +myObservable + .bind(myView) //bind(myRxActivity or myRxFragment) + .subscribe() + +``` + ## Unsubscription RxLifecycle does not actually unsubscribe the sequence. It terminates it by emitting `onCompleted()`, which ends the @@ -91,6 +100,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..c5cb52c 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-rc-1036' + } 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..1c2662d --- /dev/null +++ b/rxlifecycle-kotlin/build.gradle @@ -0,0 +1,31 @@ +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') + + compile rootProject.ext.rxJava + + compile rootProject.ext.appCompat + + testCompile rootProject.ext.junit + testCompile rootProject.ext.mockito + testCompile rootProject.ext.robolectric +} + +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..c26f717 --- /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..39eedc8 --- /dev/null +++ b/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle/kotlin/rxlifecycle.kt @@ -0,0 +1,13 @@ +package com.trello.rxlifecycle.kotlin + +import android.view.View +import com.trello.rxlifecycle.ActivityLifecycleProvider +import com.trello.rxlifecycle.FragmentLifecycleProvider +import com.trello.rxlifecycle.RxLifecycle +import rx.Observable + +fun Observable.bind(activity: ActivityLifecycleProvider): Observable = this.compose(activity.bindToLifecycle()) + +fun Observable.bind(fragment: FragmentLifecycleProvider): Observable = this.compose(fragment.bindToLifecycle()) + +fun Observable.bind(view: View): Observable = this.compose(RxLifecycle.bindView(view)) \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index bc1e01c..dc5f3ce 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,4 @@ -include ':rxlifecycle' +include ':rxlifecycle', ':rxlifecycle-kotlin' include ':rxlifecycle-components' include ':rxlifecycle-navi' include ':rxlifecycle-sample' \ No newline at end of file From aaec4cf00fc9cf7cc534e6f4e7d3e196db4610b4 Mon Sep 17 00:00:00 2001 From: Dmitriy Voronin Date: Mon, 15 Feb 2016 17:09:26 +0300 Subject: [PATCH 2/2] kotlin 1.0 and bindUntilEvent() --- README.md | 11 ++++++-- build.gradle | 2 +- rxlifecycle-kotlin/build.gradle | 8 ------ rxlifecycle-kotlin/gradle.properties | 6 ++-- .../trello/rxlifecycle/kotlin/rxlifecycle.kt | 28 +++++++++++++++---- settings.gradle | 3 +- 6 files changed, 37 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index a4aca4f..8cddd21 100644 --- a/README.md +++ b/README.md @@ -74,8 +74,15 @@ If you want some kotlin goodness, you can use bind() syntax ```java myObservable - .bind(myView) //bind(myRxActivity or myRxFragment) - .subscribe() + .bindToLifecycle(myView) + .subscribe { } + +``` + +```java +myObservable + .bindUntilEvent(myRxActivity, STOP) + .subscribe { } ``` diff --git a/build.gradle b/build.gradle index c5cb52c..0436f4d 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,7 @@ 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-rc-1036' + verKotlin = '1.0.0' } repositories { jcenter() diff --git a/rxlifecycle-kotlin/build.gradle b/rxlifecycle-kotlin/build.gradle index 1c2662d..e366627 100644 --- a/rxlifecycle-kotlin/build.gradle +++ b/rxlifecycle-kotlin/build.gradle @@ -17,14 +17,6 @@ repositories { dependencies { compile kotlinStdlib compile project(':rxlifecycle') - - compile rootProject.ext.rxJava - - compile rootProject.ext.appCompat - - testCompile rootProject.ext.junit - testCompile rootProject.ext.mockito - testCompile rootProject.ext.robolectric } apply from: "$rootDir/gradle/artifacts.gradle" diff --git a/rxlifecycle-kotlin/gradle.properties b/rxlifecycle-kotlin/gradle.properties index c26f717..2aa2a96 100644 --- a/rxlifecycle-kotlin/gradle.properties +++ b/rxlifecycle-kotlin/gradle.properties @@ -1,4 +1,4 @@ -POM_NAME=RxLifecycle-Kotlin -POM_DESCRIPTION=RxLifecycle-Kotlin -POM_ARTIFACT_ID=rxlifecycle-Kotlin +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/java/com/trello/rxlifecycle/kotlin/rxlifecycle.kt b/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle/kotlin/rxlifecycle.kt index 39eedc8..4860a79 100644 --- a/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle/kotlin/rxlifecycle.kt +++ b/rxlifecycle-kotlin/src/main/java/com/trello/rxlifecycle/kotlin/rxlifecycle.kt @@ -1,13 +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.ActivityLifecycleProvider -import com.trello.rxlifecycle.FragmentLifecycleProvider -import com.trello.rxlifecycle.RxLifecycle +import com.trello.rxlifecycle.* import rx.Observable -fun Observable.bind(activity: ActivityLifecycleProvider): Observable = this.compose(activity.bindToLifecycle()) +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.bind(fragment: FragmentLifecycleProvider): Observable = this.compose(fragment.bindToLifecycle()) +fun Observable.bindUntilEvent(fragment: FragmentLifecycleProvider, event: FragmentEvent): Observable = this.compose(fragment.bindUntilEvent(event)) -fun Observable.bind(view: View): Observable = this.compose(RxLifecycle.bindView(view)) \ No newline at end of file +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 dc5f3ce..d7a79b8 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,5 @@ -include ':rxlifecycle', ':rxlifecycle-kotlin' +include ':rxlifecycle' include ':rxlifecycle-components' include ':rxlifecycle-navi' +include ':rxlifecycle-kotlin' include ':rxlifecycle-sample' \ No newline at end of file