RxLifecycleHandler

This library is a glue between the lifecycle of `androidx.lifecycle:lifecycle-extensions` and `io.reactivex.rxjava2:rxjava` libraries.

License

License

GroupId

GroupId

com.github.hadilq
ArtifactId

ArtifactId

rx-lifecycle-handler-android
Last Version

Last Version

0.4.0
Release Date

Release Date

Type

Type

aar
Description

Description

RxLifecycleHandler
This library is a glue between the lifecycle of `androidx.lifecycle:lifecycle-extensions` and `io.reactivex.rxjava2:rxjava` libraries.
Project URL

Project URL

https://github.com/hadilq/RxLifecycleHandler
Source Code Management

Source Code Management

https://github.com/hadilq/RxLifecycleHandler

Download rx-lifecycle-handler-android

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.hadilq/rx-lifecycle-handler-android/ -->
<dependency>
    <groupId>com.github.hadilq</groupId>
    <artifactId>rx-lifecycle-handler-android</artifactId>
    <version>0.4.0</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.hadilq/rx-lifecycle-handler-android/
implementation 'com.github.hadilq:rx-lifecycle-handler-android:0.4.0'
// https://jarcasting.com/artifacts/com.github.hadilq/rx-lifecycle-handler-android/
implementation ("com.github.hadilq:rx-lifecycle-handler-android:0.4.0")
'com.github.hadilq:rx-lifecycle-handler-android:aar:0.4.0'
<dependency org="com.github.hadilq" name="rx-lifecycle-handler-android" rev="0.4.0">
  <artifact name="rx-lifecycle-handler-android" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.github.hadilq', module='rx-lifecycle-handler-android', version='0.4.0')
)
libraryDependencies += "com.github.hadilq" % "rx-lifecycle-handler-android" % "0.4.0"
[com.github.hadilq/rx-lifecycle-handler-android "0.4.0"]

Dependencies

compile (2)

Group / Artifact Type Version
com.github.hadilq : android-lifecycle-handler-metadata jar 0.4.3
com.github.hadilq : android-lifecycle-handler-android jar 0.4.3

runtime (5)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib-common jar 1.4.0
org.jetbrains.kotlin : kotlin-stdlib jar 1.4.0
androidx.lifecycle » lifecycle-extensions jar 2.2.0
io.reactivex.rxjava2 : rxjava jar 2.2.15
io.reactivex.rxjava2 : rxandroid jar 2.1.1

Project Modules

There are no modules declared in this project.

Maven Central CircleCI codecov

Rx Lifecycle Handler

This library is a glue between the lifecycle of androidx.lifecycle:lifecycle-extensions and io.reactivex.rxjava2:rxjava libraries. The assumption is that we want the emitted values between start and stop of LifecycleOwner, so on other moments this library would unsubscribe from the upstream of RxJava, so called Observable, Flowable, Maybe, and Single. Also, we support a more general assumption that we want the emitted values after start and before stop or saveState of SavedStateRegistryOwner, depends which one gets called sooner.

Also you can find its twin library for Kotlin Coroutines in https://github.com/hadilq/CoroutineLifecycleHandler/.

Usage

This source has a sample app, which doesn't do anything, where you can find the usage in MainActivity and MainViewModelActivity. However, in MVVM or MVI architectural patterns, you can use it based on the data you want to save in onSaveInstanceState or not. For now let's restrict ourselves to MVVM. If the data you want to propagate is an action, then you probably don't want to save it so in ViewModel you have.

class MainViewModel : ViewModel() {

    private val publisher = PublishProcessor.create<String>()
    val stringEmitter = publisher.toLifecycleAware()
}

And in the Activity or Fragment you have.

class MyViewModelActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)

        val viewModel = ViewModelProvider(this).get(MainViewModel::class.java)

        (viewModel.stringEmitter.observe()) { testString ->
            /* use it here */
        }
    }
}

But if you want to save the propagated data in onSaveInstanceState method, then you may want to use it as follows.

class MainViewModel : ViewModel() {

    private val extendedPublisher = BehaviorProcessor.create<String>()

    val extendedStringEmitter = extendedPublisher.toExtendedLifecycleAware(KEY)

    companion object {
        private const val KEY = "key_to_save_string_emitter"
    }
}

And in the Activity or Fragment you have.

class MyViewModelActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)

        val viewModel = ViewModelProvider(this).get(MainViewModel::class.java)

        (viewModel.extendedStringEmitter.observe()) { testString ->
            /* use it here */
        }
    }
}

Here, you may noticed the extra parentheses above, or you may noticed that we didn't passe the Activity or Fragment to the observe method. In both cases, read the last sentence again! Because they're related to each other, which means in this library we preferred to avoid writing this to pass the Activity or Fragment directly in trade off of having odd position of parentheses.

Any way, in case you want to use this library out of MVVM architectural pattern, you can use it as follows.

class MainActivity : ComponentActivity() {

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

        val flowable: Flowable<String> = ...
        val maybe: Maybe<String> = ...
        val observable: Observable<String> = ...
        val single: Single<String> = ...

        // Flowable usage
        (flowable.observe())(::handleString)

        // Maybe usage
        (maybe.observe())(::handleString)

        // Observable usage
        (observable.observe())(::handleString)

        // Single usage
        (single.observe())(::handleString)

        // Flowable usage
        (flowable.observe(life, KEY))(::handleString)

        // Maybe usage
        (maybe.observe(life, KEY))(::handleString)

        // Observable usage
        (observable.observe(life, KEY))(::handleString)

        // Single usage
        (single.observe(life, KEY))(::handleString)
    }

    private fun handleString(s: String) { }

    companion object {
        private const val KEY = "key_to_save_the_data"
    }
}

Enjoy!

Download

Since version 0.4.0 this library is a Kotlin Multiplatform library, so depend on what kind of module you use it in, you can use different artifact IDs. Download via gradle

implementation "com.github.hadilq:rx-lifecycle-handler-android:$libVersion"

where the libVersion is Maven Central. Use the above one if you already using this library or you need it in an android module. However as this library is depending on RxJava, it does not make sense to support common library of Multiplatform! Let's see how we can emerge the this idea to Multiplatform in the future!

Contribution

Just create your branch from the master branch, change it, write additional tests, satisfy all tests, create your pull request, thank you, you're awesome.

Versions

Version
0.4.0