Home

Awesome

Lives - Android LiveData Extensions for Kotlin and Java

Build Status Latest Version

Add RxJava-like operators to your LiveData objects with ease, usable in Kotlin (as extension functions) and Java (as static functions to a class called Lives)

Download

Add the dependencies to your project:

AndroidX Version

implementation 'com.snakydesign.livedataextensions:lives:1.3.0'
implementation 'android.arch.lifecycle:extensions:x.x.x' // If you are using the AndroidX version, that's fine too, as the Jetifier will take care of the conversion.

Non AndroidX Version

implementation 'com.snakydesign.livedataextensions:lives:1.2.1'
implementation 'androidx.lifecycle:lifecycle-livedata:x.x.x' 

If you want to use this library on a Java project, add the following dependency:

implementation 'org.jetbrains.kotlin:kotlin-stdlib:x.x.x'

Usage

Kotlin

Import the functions

    import com.snakydesign.livedataextensions.*

Creating LiveData

    val liveData = liveDataOf(2) //liveData will produce 2 (as Int) when observed
    val liveData = liveDataOf {computePI()}
    val liveData = emptyLiveData<Int>()

Filtering

    val originalLiveData = MutableLiveData<Int>()
    val newLiveData = originalLiveData.distinct()
    originalLiveData.value = 2
    originalLiveData.value = 2 // newLiveData will not produce this
    originalLiveData.value = 3 // newLiveData will produce
    originalLiveData.value = 2 // newLiveData will not produce this
    val originalLiveData = MutableLiveData<Int>()
    val newLiveData = originalLiveData.distinctUntilChanged()
    originalLiveData.value = 2
    originalLiveData.value = 2 // newLiveData will not produce this
    originalLiveData.value = 3 // newLiveData will produce
    originalLiveData.value = 2 // newLiveData will produce
    val originalLiveData = MutableLiveData<Int>()
    val newLiveData = originalLiveData.filter { it > 2 }
    originalLiveData.value = 3 // newLiveData will produce
    originalLiveData.value = 2 // newLiveData will not produce this

Combining

Transforming

Java

You can call any function prefixed with Lives keyword.

import com.snakydesign.livedataextensions.Lives;

Notes

Please note that because of design of LiveData, after a value is emitted to an observer, and then another value is emitted, the old value is destroyed in any LiveData object. So unlike RxJava, if a new Observer is attached, It will only receive the most recent value.

So If you want to use operators like concat, you have to consider allowing only one observer to the LiveData.

PRs are more than welcome, and please file an issue If you encounter something 🍻.

You can also ping me on twitter @TheSNAKY.

License

Copyright 2018 Adib Faramarzi.

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.