All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ubercab.autodispose.rxlifecycle.KotlinExtensions.kt Maven / Gradle / Ivy

There is a newer version: 1.4.1
Show newest version
/*
 * 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  LifecycleProvider.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  Flowable.autoDisposable(lifecycleProvider: LifecycleProvider, event: E? = null): FlowableSubscribeProxy {
  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  Observable.autoDisposable(lifecycleProvider: LifecycleProvider, event: E? = null): ObservableSubscribeProxy {
  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  Single.autoDisposable(lifecycleProvider: LifecycleProvider, event: E? = null): SingleSubscribeProxy {
  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  Maybe.autoDisposable(lifecycleProvider: LifecycleProvider, event: E? = null): MaybeSubscribeProxy {
  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  Completable.autoDisposable(lifecycleProvider: LifecycleProvider, event: E? = null): CompletableSubscribeProxy {
  return this.`as`(AutoDispose.autoDisposable(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  ParallelFlowable.autoDisposable(lifecycleProvider: LifecycleProvider, event: E? = null): ParallelFlowableSubscribeProxy {
  return this.`as`(AutoDispose.autoDisposable(lifecycleProvider.scope(event)))
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy