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

Add additional extension methods for android-archcomponents #348

Merged
merged 10 commits into from
May 14, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,22 @@ package com.uber.autodispose.android.lifecycle
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.Event
import androidx.lifecycle.LifecycleOwner
import com.uber.autodispose.AutoDispose
import com.uber.autodispose.FlowableSubscribeProxy
import com.uber.autodispose.ObservableSubscribeProxy
import com.uber.autodispose.SingleSubscribeProxy
import com.uber.autodispose.MaybeSubscribeProxy
import com.uber.autodispose.CompletableSubscribeProxy
import com.uber.autodispose.ParallelFlowableSubscribeProxy
import com.uber.autodispose.ScopeProvider
import com.uber.autodispose.lifecycle.CorrespondingEventsFunction
import io.reactivex.annotations.CheckReturnValue
import io.reactivex.parallel.ParallelFlowable
import io.reactivex.Observable
import io.reactivex.Flowable
import io.reactivex.Maybe
import io.reactivex.Single
import io.reactivex.Completable

/**
* Extension that returns a [ScopeProvider] for this [LifecycleOwner].
Expand Down Expand Up @@ -77,3 +90,93 @@ inline fun Lifecycle.scope(
boundaryResolver: CorrespondingEventsFunction<Event>
): ScopeProvider = AndroidLifecycleScopeProvider.from(
this, boundaryResolver)

/**
* Extension that proxies to [Flowable.as] + [AutoDispose.autoDisposable]
*/
@CheckReturnValue
inline fun <T> Flowable<T>.autoDisposable(lifecycleOwner: LifecycleOwner): FlowableSubscribeProxy<T> =
this.`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner)))

/**
* Extension that proxies to [Flowable.as] + [AutoDispose.autoDisposable] and takes an [untilEvent] when
* subscription will be disposed.
ShaishavGandhi marked this conversation as resolved.
Show resolved Hide resolved
*/
@CheckReturnValue
inline fun <T> Flowable<T>.autoDisposable(lifecycleOwner: LifecycleOwner, untilEvent: Event): FlowableSubscribeProxy<T> =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's default the untilEvent to something sensible and then just have one overload?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can we really default an untilEvent reliably for everyone? Usually the untilEvent is inferred from when in the lifecycle you subscribe. However that inference logic is private in AndroidLifecycleScopeProvider.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd be fine with it package private

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this.`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner, untilEvent)))

/**
* Extension that proxies to [Observable.as] + [AutoDispose.autoDisposable]
*/
@CheckReturnValue
inline fun <T> Observable<T>.autoDisposable(lifecycleOwner: LifecycleOwner): ObservableSubscribeProxy<T> =
this.`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner)))

/**
* Extension that proxies to [Observable.as] + [AutoDispose.autoDisposable] and takes an [untilEvent] when
* subscription will be disposed.
*/
@CheckReturnValue
inline fun <T> Observable<T>.autoDisposable(lifecycleOwner: LifecycleOwner, untilEvent: Event): ObservableSubscribeProxy<T> =
this.`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner, untilEvent)))

/**
* Extension that proxies to [Single.as] + [AutoDispose.autoDisposable]
*/
@CheckReturnValue
inline fun <T> Single<T>.autoDisposable(lifecycleOwner: LifecycleOwner): SingleSubscribeProxy<T> =
this.`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner)))

/**
* Extension that proxies to [Single.as] + [AutoDispose.autoDisposable] and takes an [untilEvent] when
* subscription will be disposed.
*/
@CheckReturnValue
inline fun <T> Single<T>.autoDisposable(lifecycleOwner: LifecycleOwner, untilEvent: Event): SingleSubscribeProxy<T> =
this.`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner, untilEvent)))

/**
* Extension that proxies to [Maybe.as] + [AutoDispose.autoDisposable]
*/
@CheckReturnValue
inline fun <T> Maybe<T>.autoDisposable(lifecycleOwner: LifecycleOwner): MaybeSubscribeProxy<T> =
this.`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner)))

/**
* Extension that proxies to [Maybe.as] + [AutoDispose.autoDisposable] and takes an [untilEvent] when
* subscription will be disposed.
*/
@CheckReturnValue
inline fun <T> Maybe<T>.autoDisposable(lifecycleOwner: LifecycleOwner, untilEvent: Event): MaybeSubscribeProxy<T> =
this.`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner, untilEvent)))

/**
* Extension that proxies to [Completable.as] + [AutoDispose.autoDisposable]
*/
@CheckReturnValue
inline fun Completable.autoDisposable(lifecycleOwner: LifecycleOwner): CompletableSubscribeProxy =
this.`as`(AutoDispose.autoDisposable<Any>(AndroidLifecycleScopeProvider.from(lifecycleOwner)))

/**
* Extension that proxies to [Completable.as] + [AutoDispose.autoDisposable] and takes an [untilEvent] when
* subscription will be disposed.
*/
@CheckReturnValue
inline fun Completable.autoDisposable(lifecycleOwner: LifecycleOwner, untilEvent: Event): CompletableSubscribeProxy =
this.`as`(AutoDispose.autoDisposable<Any>(AndroidLifecycleScopeProvider.from(lifecycleOwner, untilEvent)))

/**
* Extension that proxies to [ParallelFlowable.as] + [AutoDispose.autoDisposable]
*/
@CheckReturnValue
inline fun <T> ParallelFlowable<T>.autoDisposable(lifecycleOwner: LifecycleOwner): ParallelFlowableSubscribeProxy<T> =
this.`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner)))

/**
* Extension that proxies to [ParallelFlowable.as] + [AutoDispose.autoDisposable] and takes an [untilEvent] when
* subscription will be disposed.
*/
@CheckReturnValue
inline fun <T> ParallelFlowable<T>.autoDisposable(lifecycleOwner: LifecycleOwner, untilEvent: Event): ParallelFlowableSubscribeProxy<T> =
this.`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner, untilEvent)))
2 changes: 2 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,6 @@ dependencies {
def leakcanaryVersion = '1.6.2'
debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakcanaryVersion"
releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakcanaryVersion"

androidTestImplementation project(':test-utils')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.uber.autodispose

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider
import com.uber.autodispose.android.lifecycle.autoDisposable
import com.uber.autodispose.android.lifecycle.scope
import io.reactivex.Observable
import java.util.concurrent.TimeUnit

/**
* Test Activity class to verify compilation of various extension functions.
*/
class TestKotlinActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// With extension function that overloads LifecycleOwner
Observable.interval(1, TimeUnit.SECONDS)
.autoDisposable(this)
.subscribe()

// With extension function that overloads LifecycleOwner and until Event
Observable.interval(1, TimeUnit.SECONDS)
.autoDisposable(this, Lifecycle.Event.ON_DESTROY)
.subscribe()

// With extension function that overloads ScopeProvider
Observable.interval(1, TimeUnit.SECONDS)
.autoDisposable(scope(Lifecycle.Event.ON_DESTROY))
.subscribe()

// With no extension function
Observable.interval(1, TimeUnit.SECONDS)
.autoDisposable(AndroidLifecycleScopeProvider.from(this))
.subscribe()
}
}