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

RxLifecycle Extensions #353

Merged
merged 6 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (C) 2019. Uber Technologies
*
* 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.ubercab.autodispose.rxlifecycle

import com.trello.rxlifecycle2.LifecycleProvider
import com.uber.autodispose.AutoDispose
import com.uber.autodispose.FlowableSubscribeProxy
import com.uber.autodispose.ObservableSubscribeProxy
import com.uber.autodispose.MaybeSubscribeProxy
import com.uber.autodispose.SingleSubscribeProxy
import com.uber.autodispose.CompletableSubscribeProxy
import com.uber.autodispose.ParallelFlowableSubscribeProxy
import com.uber.autodispose.ScopeProvider
import io.reactivex.Flowable
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.Completable
import io.reactivex.annotations.CheckReturnValue
import io.reactivex.parallel.ParallelFlowable

/**
* Extension that converts a [LifecycleProvider] to [ScopeProvider].
*/
inline fun <E> LifecycleProvider<E>.scope(event: E? = null): ScopeProvider {
return if (event == null) {
RxLifecycleInterop.from(this)
} else {
RxLifecycleInterop.from(this, event)
}
}

/**
* Extension that proxies to [Flowable.as] + [AutoDispose.autoDisposable] and takes an [event] when
* subscription will be disposed.
*
* @param lifecycleProvider The lifecycle provider from RxLifecycle.
* @param event Optional lifecycle event when subscription will be disposed.
*/
@CheckReturnValue
inline fun <T, E> Flowable<T>.autoDisposable(lifecycleProvider: LifecycleProvider<E>, event: E? = null): FlowableSubscribeProxy<T> {
return this.`as`(AutoDispose.autoDisposable(lifecycleProvider.scope(event)))
}

/**
* Extension that proxies to [Observable.as] + [AutoDispose.autoDisposable] and takes an [event] when
* subscription will be disposed.
*
* @param lifecycleProvider The lifecycle provider from RxLifecycle.
* @param event Optional lifecycle event when subscription will be disposed.
*/
@CheckReturnValue
inline fun <T, E> Observable<T>.autoDisposable(lifecycleProvider: LifecycleProvider<E>, event: E? = null): ObservableSubscribeProxy<T> {
return this.`as`(AutoDispose.autoDisposable(lifecycleProvider.scope(event)))
}

/**
* Extension that proxies to [Single.as] + [AutoDispose.autoDisposable] and takes an [event] when
* subscription will be disposed.
*
* @param lifecycleProvider The lifecycle provider from RxLifecycle.
* @param event Optional lifecycle event when subscription will be disposed.
*/
@CheckReturnValue
inline fun <T, E> Single<T>.autoDisposable(lifecycleProvider: LifecycleProvider<E>, event: E? = null): SingleSubscribeProxy<T> {
return this.`as`(AutoDispose.autoDisposable(lifecycleProvider.scope(event)))
}

/**
* Extension that proxies to [Maybe.as] + [AutoDispose.autoDisposable] and takes an [event] when
* subscription will be disposed.
*
* @param lifecycleProvider The lifecycle provider from RxLifecycle.
* @param event Optional lifecycle event when subscription will be disposed.
*/
@CheckReturnValue
inline fun <T, E> Maybe<T>.autoDisposable(lifecycleProvider: LifecycleProvider<E>, event: E? = null): MaybeSubscribeProxy<T> {
return this.`as`(AutoDispose.autoDisposable(lifecycleProvider.scope(event)))
}

/**
* Extension that proxies to [Completable.as] + [AutoDispose.autoDisposable] and takes an [event] when
* subscription will be disposed.
*
* @param lifecycleProvider The lifecycle provider from RxLifecycle.
* @param event Optional lifecycle event when subscription will be disposed.
*/
@CheckReturnValue
inline fun <E> Completable.autoDisposable(lifecycleProvider: LifecycleProvider<E>, event: E? = null): CompletableSubscribeProxy {
return this.`as`(AutoDispose.autoDisposable<Any>(lifecycleProvider.scope(event)))
}

/**
* Extension that proxies to [ParallelFlowable.as] + [AutoDispose.autoDisposable] and takes an [event] when
* subscription will be disposed.
*
* @param lifecycleProvider The lifecycle provider from RxLifecycle.
* @param event Optional lifecycle event when subscription will be disposed.
*/
@CheckReturnValue
inline fun <T, E> ParallelFlowable<T>.autoDisposable(lifecycleProvider: LifecycleProvider<E>, event: E? = null): ParallelFlowableSubscribeProxy<T> {
return this.`as`(AutoDispose.autoDisposable(lifecycleProvider.scope(event)))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Top-level functions that can only be used by Kotlin.
-dontwarn com.ubercab.autodispose.rxlifecycle.KotlinExtensions
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (C) 2019. Uber Technologies
*
* 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.ubercab.autodispose.rxlifecycle3

import com.trello.rxlifecycle3.LifecycleProvider
import com.uber.autodispose.AutoDispose
import com.uber.autodispose.FlowableSubscribeProxy
import com.uber.autodispose.ObservableSubscribeProxy
import com.uber.autodispose.MaybeSubscribeProxy
import com.uber.autodispose.SingleSubscribeProxy
import com.uber.autodispose.CompletableSubscribeProxy
import com.uber.autodispose.ParallelFlowableSubscribeProxy
import com.uber.autodispose.ScopeProvider
import io.reactivex.Flowable
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.Completable
import io.reactivex.annotations.CheckReturnValue
import io.reactivex.parallel.ParallelFlowable

/**
* Extension that converts a [LifecycleProvider] to [ScopeProvider].
*/
inline fun <E> LifecycleProvider<E>.scope(event: E? = null): ScopeProvider {
return if (event == null) {
RxLifecycleInterop.from(this)
} else {
RxLifecycleInterop.from(this, event)
}
}

/**
* Extension that proxies to [Flowable.as] + [AutoDispose.autoDisposable] and takes an [event] when
* subscription will be disposed.
*
* @param lifecycleProvider The lifecycle provider from RxLifecycle.
* @param event Optional lifecycle event when subscription will be disposed.
*/
@CheckReturnValue
inline fun <T, E> Flowable<T>.autoDisposable(lifecycleProvider: LifecycleProvider<E>, event: E? = null): FlowableSubscribeProxy<T> {
return this.`as`(AutoDispose.autoDisposable(lifecycleProvider.scope(event)))
}

/**
* Extension that proxies to [Observable.as] + [AutoDispose.autoDisposable] and takes an [event] when
* subscription will be disposed.
*
* @param lifecycleProvider The lifecycle provider from RxLifecycle.
* @param event Optional lifecycle event when subscription will be disposed.
*/
@CheckReturnValue
inline fun <T, E> Observable<T>.autoDisposable(lifecycleProvider: LifecycleProvider<E>, event: E? = null): ObservableSubscribeProxy<T> {
return this.`as`(AutoDispose.autoDisposable(lifecycleProvider.scope(event)))
}

/**
* Extension that proxies to [Single.as] + [AutoDispose.autoDisposable] and takes an [event] when
* subscription will be disposed.
*
* @param lifecycleProvider The lifecycle provider from RxLifecycle.
* @param event Optional lifecycle event when subscription will be disposed.
*/
@CheckReturnValue
inline fun <T, E> Single<T>.autoDisposable(lifecycleProvider: LifecycleProvider<E>, event: E? = null): SingleSubscribeProxy<T> {
return this.`as`(AutoDispose.autoDisposable(lifecycleProvider.scope(event)))
}

/**
* Extension that proxies to [Maybe.as] + [AutoDispose.autoDisposable] and takes an [event] when
* subscription will be disposed.
*
* @param lifecycleProvider The lifecycle provider from RxLifecycle.
* @param event Optional lifecycle event when subscription will be disposed.
*/
@CheckReturnValue
inline fun <T, E> Maybe<T>.autoDisposable(lifecycleProvider: LifecycleProvider<E>, event: E? = null): MaybeSubscribeProxy<T> {
return this.`as`(AutoDispose.autoDisposable(lifecycleProvider.scope(event)))
}

/**
* Extension that proxies to [Completable.as] + [AutoDispose.autoDisposable] and takes an [event] when
* subscription will be disposed.
*
* @param lifecycleProvider The lifecycle provider from RxLifecycle.
* @param event Optional lifecycle event when subscription will be disposed.
*/
@CheckReturnValue
inline fun <E> Completable.autoDisposable(lifecycleProvider: LifecycleProvider<E>, event: E? = null): CompletableSubscribeProxy {
return this.`as`(AutoDispose.autoDisposable<Any>(lifecycleProvider.scope(event)))
}

/**
* Extension that proxies to [ParallelFlowable.as] + [AutoDispose.autoDisposable] and takes an [event] when
* subscription will be disposed.
*
* @param lifecycleProvider The lifecycle provider from RxLifecycle.
* @param event Optional lifecycle event when subscription will be disposed.
*/
@CheckReturnValue
inline fun <T, E> ParallelFlowable<T>.autoDisposable(lifecycleProvider: LifecycleProvider<E>, event: E? = null): ParallelFlowableSubscribeProxy<T> {
return this.`as`(AutoDispose.autoDisposable(lifecycleProvider.scope(event)))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Top-level functions that can only be used by Kotlin.
-dontwarn com.ubercab.autodispose.rxlifecycle3.KotlinExtensions
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ Set<String> mixedSourcesArtifacts = [
"autodispose-android",
"autodispose-android-archcomponents",
"autodispose-android-archcomponents-test",
"autodispose-lifecycle"
"autodispose-lifecycle",
"autodispose-rxlifecycle",
"autodispose-rxlifecycle3"
]
// These are files with different copyright headers that should not be modified automatically.
String[] copiedFiles = [
Expand Down
1 change: 1 addition & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ dependencies {
implementation project(':android:autodispose-android-archcomponents')
implementation project(':autodispose')
implementation project(':autodispose-lifecycle')
implementation project(":autodispose-rxlifecycle3")
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1'
implementation 'com.google.android.material:material:1.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import com.trello.rxlifecycle3.LifecycleProvider
import com.trello.rxlifecycle3.LifecycleTransformer
import com.uber.autodispose.android.autoDisposable
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider
import com.uber.autodispose.android.lifecycle.autoDisposable
import com.uber.autodispose.android.lifecycle.scope
import com.ubercab.autodispose.rxlifecycle3.autoDisposable
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
Expand Down Expand Up @@ -99,9 +102,39 @@ class TestKotlinActivity : AppCompatActivity(), ScopeProvider {
Observable.interval(1, TimeUnit.DAYS)
.autoDisposable(rootView)
.subscribe()

// RxLifecycle
val lifecycleProvider = TestLifecycleProvider()
Observable.interval(1, TimeUnit.SECONDS)
.autoDisposable(lifecycleProvider)
.subscribe()

Observable.interval(1, TimeUnit.SECONDS)
.autoDisposable(lifecycleProvider, TestLifecycleProvider.Event.CREATE)
.subscribe()
}

override fun requestScope(): CompletableSource {
return Completable.complete()
}

/** Stub implementation for [LifecycleProvider] for compilation testing */
class TestLifecycleProvider : LifecycleProvider<TestLifecycleProvider.Event> {
override fun lifecycle(): Observable<Event> {
return Observable.empty<Event>()
}

override fun <T : Any?> bindUntilEvent(event: Event): LifecycleTransformer<T> {
TODO("Stub to test compilation of extensions that use LifecycleProvider")
}

override fun <T : Any?> bindToLifecycle(): LifecycleTransformer<T> {
TODO("Stub to test compilation of extensions that use LifecycleProvider")
}

enum class Event {
CREATE,
DESTROY
}
}
}