-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #216 from evant/android-lifecycle
Added support for android-lifecycle
- Loading branch information
Showing
21 changed files
with
665 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
apply plugin: 'com.android.library' | ||
apply plugin: 'com.github.dcendents.android-maven' | ||
apply plugin: 'kotlin-android' | ||
|
||
android { | ||
compileSdkVersion rootProject.ext.compileSdkVersion | ||
buildToolsVersion rootProject.ext.buildToolsVersion | ||
|
||
defaultConfig { | ||
minSdkVersion rootProject.ext.minSdkVersion | ||
} | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
maven { url 'https://maven.google.com' } | ||
} | ||
|
||
dependencies { | ||
compile kotlinStdlib | ||
compile project(':rxlifecycle-android-lifecycle') | ||
} | ||
|
||
apply from: "$rootDir/gradle/artifacts.gradle" | ||
apply from: "$rootDir/gradle/gradle-mvn-push.gradle" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
POM_NAME=RxLifecycle-Andoird-Lifecycle-kotlin | ||
POM_DESCRIPTION=RxLifecycle-Android-Lifecycle-kotlin | ||
POM_ARTIFACT_ID=rxlifecycle-android-lifecycle-kotlin | ||
POM_PACKAGING=aar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Add project specific ProGuard rules here. | ||
# By default, the flags in this file are appended to flags specified | ||
# in /opt/android-sdk/tools/proguard/proguard-android.txt | ||
# You can edit the include path and order by changing the proguardFiles | ||
# directive in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# Add any project specific keep options here: | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
2 changes: 2 additions & 0 deletions
2
rxlifecycle-android-lifecycle-kotlin/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="me.tatarka.rxlifecycle_android_lifecycle_kotlin" /> |
50 changes: 50 additions & 0 deletions
50
...ycle-kotlin/src/main/java/com/trello/rxlifecycle2/android/lifecycle/kotlin/rxlifecycle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* 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.rxlifecycle2.android.lifecycle.kotlin | ||
|
||
import android.arch.lifecycle.Lifecycle | ||
import android.arch.lifecycle.LifecycleOwner | ||
import com.trello.lifecycle2.android.lifecycle.AndroidLifecycle | ||
import io.reactivex.* | ||
|
||
fun <T> Observable<T>.bindToLifecycle(owner: LifecycleOwner): Observable<T> | ||
= this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindToLifecycle()) | ||
|
||
fun <T> Observable<T>.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Observable<T> | ||
= this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent(event)) | ||
|
||
fun <T> Flowable<T>.bindToLifecycle(owner: LifecycleOwner): Flowable<T> | ||
= this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindToLifecycle()) | ||
|
||
fun <T> Flowable<T>.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Flowable<T> | ||
= this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent(event)) | ||
|
||
fun <T> Single<T>.bindToLifecycle(owner: LifecycleOwner): Single<T> | ||
= this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindToLifecycle()) | ||
|
||
fun <T> Single<T>.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Single<T> | ||
= this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent(event)) | ||
|
||
fun <T> Maybe<T>.bindToLifecycle(owner: LifecycleOwner): Maybe<T> | ||
= this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindToLifecycle()) | ||
|
||
fun <T> Maybe<T>.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Maybe<T> | ||
= this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent(event)) | ||
|
||
fun <T> Completable.bindToLifecycle(owner: LifecycleOwner): Completable | ||
= this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindToLifecycle<Completable>()) | ||
|
||
fun <T> Completable.bindUntilEvent(owner: LifecycleOwner, event: Lifecycle.Event): Completable | ||
= this.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent<Completable>(event)) |
3 changes: 3 additions & 0 deletions
3
rxlifecycle-android-lifecycle-kotlin/src/main/res/values/strings.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<resources> | ||
<string name="app_name">rxlifecycle-android-lifecycle-kotlin</string> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
apply plugin: 'com.android.library' | ||
apply plugin: 'com.github.dcendents.android-maven' | ||
|
||
android { | ||
compileSdkVersion rootProject.ext.compileSdkVersion | ||
buildToolsVersion rootProject.ext.buildToolsVersion | ||
|
||
defaultConfig { | ||
minSdkVersion rootProject.ext.minSdkVersion | ||
} | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
maven { url 'https://maven.google.com' } | ||
} | ||
|
||
dependencies { | ||
compile project(':rxlifecycle') | ||
|
||
compile rootProject.ext.rxAndroid | ||
compile rootProject.ext.supportAnnotations | ||
compile rootProject.ext.lifecycle | ||
annotationProcessor rootProject.ext.lifecycleProcessor | ||
|
||
testCompile rootProject.ext.junit | ||
testCompile rootProject.ext.robolectric | ||
} | ||
|
||
apply from: "$rootDir/gradle/artifacts.gradle" | ||
apply from: "$rootDir/gradle/gradle-mvn-push.gradle" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
POM_NAME=RxLifecycle-Android-Lifecycle | ||
POM_DESCRIPTION=RxLifecycle-Android-Lifecycle | ||
POM_ARTIFACT_ID=rxlifecycle-android-lifecycle | ||
POM_PACKAGING=aar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<manifest package="com.trello.rxlifecycle2.android.lifecycle" /> | ||
|
69 changes: 69 additions & 0 deletions
69
...oid-lifecycle/src/main/java/com/trello/lifecycle2/android/lifecycle/AndroidLifecycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.trello.lifecycle2.android.lifecycle; | ||
|
||
import android.arch.lifecycle.Lifecycle; | ||
import android.arch.lifecycle.LifecycleObserver; | ||
import android.arch.lifecycle.LifecycleOwner; | ||
import android.arch.lifecycle.OnLifecycleEvent; | ||
import android.support.annotation.CheckResult; | ||
|
||
import com.trello.rxlifecycle2.LifecycleProvider; | ||
import com.trello.rxlifecycle2.LifecycleTransformer; | ||
import com.trello.rxlifecycle2.RxLifecycle; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import io.reactivex.Observable; | ||
import io.reactivex.subjects.BehaviorSubject; | ||
|
||
/** | ||
* Wraps a {@link LifecycleOwner} so that it can be used as a {@link LifecycleProvider}. For example, | ||
* you can do | ||
* <pre>{@code | ||
* LifecycleProvider<Lifecycle.Event> provider = AndroidLifecycle.createLifecycleProvider(this); | ||
* myObservable | ||
* .compose(provider.bindLifecycle()) | ||
* .subscribe(); | ||
* }</pre> | ||
* where {@code this} is a {@link android.arch.lifecycle.LifecycleActivity} or {@link android.arch.lifecycle.LifecycleFragment}. | ||
*/ | ||
public final class AndroidLifecycle implements LifecycleProvider<Lifecycle.Event>, LifecycleObserver { | ||
|
||
public static LifecycleProvider<Lifecycle.Event> createLifecycleProvider(LifecycleOwner owner) { | ||
return new AndroidLifecycle(owner); | ||
} | ||
|
||
private final BehaviorSubject<Lifecycle.Event> lifecycleSubject = BehaviorSubject.create(); | ||
|
||
private AndroidLifecycle(LifecycleOwner owner) { | ||
owner.getLifecycle().addObserver(this); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
@CheckResult | ||
public Observable<Lifecycle.Event> lifecycle() { | ||
return lifecycleSubject.hide(); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
@CheckResult | ||
public <T> LifecycleTransformer<T> bindUntilEvent(@Nonnull Lifecycle.Event event) { | ||
return RxLifecycle.bindUntilEvent(lifecycleSubject, event); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
@CheckResult | ||
public <T> LifecycleTransformer<T> bindToLifecycle() { | ||
return RxLifecycleAndroidLifecycle.bindLifecycle(lifecycleSubject); | ||
} | ||
|
||
@OnLifecycleEvent(Lifecycle.Event.ON_ANY) | ||
void onEvent(LifecycleOwner owner, Lifecycle.Event event) { | ||
lifecycleSubject.onNext(event); | ||
if (event == Lifecycle.Event.ON_DESTROY) { | ||
owner.getLifecycle().removeObserver(this); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...le/src/main/java/com/trello/lifecycle2/android/lifecycle/RxLifecycleAndroidLifecycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.trello.lifecycle2.android.lifecycle; | ||
|
||
import android.arch.lifecycle.Lifecycle; | ||
import android.support.annotation.CheckResult; | ||
import android.support.annotation.NonNull; | ||
|
||
import com.trello.rxlifecycle2.LifecycleTransformer; | ||
import com.trello.rxlifecycle2.OutsideLifecycleException; | ||
|
||
import io.reactivex.Observable; | ||
import io.reactivex.functions.Function; | ||
|
||
import static com.trello.rxlifecycle2.RxLifecycle.bind; | ||
|
||
public final class RxLifecycleAndroidLifecycle { | ||
|
||
private RxLifecycleAndroidLifecycle() { | ||
throw new AssertionError("No instances"); | ||
} | ||
|
||
/** | ||
* Binds the given source to an Android lifecycle. | ||
* <p> | ||
* This helper automatically determines (based on the lifecycle sequence itself) when the source | ||
* should stop emitting items. In the case that the lifecycle sequence is in the | ||
* creation phase (ON_CREATE, ON_START, etc) it will choose the equivalent destructive phase (ON_DESTROY, | ||
* ON_STOP, etc). If used in the destructive phase, the notifications will cease at the next event; | ||
* for example, if used in ON_PAUSE, it will unsubscribe in ON_STOP. | ||
* | ||
* @param lifecycle the lifecycle sequence of an Activity | ||
* @return a reusable {@link LifecycleTransformer} that unsubscribes the source during the Activity lifecycle | ||
*/ | ||
@NonNull | ||
@CheckResult | ||
public static <T> LifecycleTransformer<T> bindLifecycle(@NonNull Observable<Lifecycle.Event> lifecycle) { | ||
return bind(lifecycle, LIFECYCLE); | ||
} | ||
|
||
private static final Function<Lifecycle.Event, Lifecycle.Event> LIFECYCLE = new Function<Lifecycle.Event, Lifecycle.Event>() { | ||
@Override | ||
public Lifecycle.Event apply(Lifecycle.Event lastEvent) throws Exception { | ||
switch (lastEvent) { | ||
case ON_CREATE: | ||
return Lifecycle.Event.ON_DESTROY; | ||
case ON_START: | ||
return Lifecycle.Event.ON_STOP; | ||
case ON_RESUME: | ||
return Lifecycle.Event.ON_PAUSE; | ||
case ON_PAUSE: | ||
return Lifecycle.Event.ON_STOP; | ||
case ON_STOP: | ||
return Lifecycle.Event.ON_DESTROY; | ||
case ON_DESTROY: | ||
throw new OutsideLifecycleException("Cannot bind to Activity lifecycle when outside of it."); | ||
default: | ||
throw new UnsupportedOperationException("Binding to " + lastEvent + " not yet implemented"); | ||
} | ||
} | ||
}; | ||
} |
Oops, something went wrong.