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)
Add the dependencies to your project:
implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'com.snakydesign.livedataextensions:lives:1.0.1'
Import the functions
import com.snakydesign.livedataextensions.*
Creating LiveData
just
: Create a LiveData object from a value
val liveData = just(2) //liveData will produce 2 (as Int) when observed
from
: Creates a LiveData that emits the value that thecallable
function produces, immediately.
val liveData = from {computePI()}
empty
: Creates an empty LiveData.
val liveData = empty()
Filtering
distinct
: Emits the items that are different from all the values that have been emitted so far
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
distinctUntilChanged
: Emits the items that are different from the last item
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 produce
filter
:Emits the items that pass through the predicate
val originalLiveData = MutableLiveData<Int>()
val newLiveData = originalLiveData.filter { it > 2 }
originalLiveData.value = 3 // newLiveData will produce
originalLiveData.value = 2 // newLiveData will not produce this
first()
: produces a SingleLiveData that produces only one Item.take(n:Int)
: produces a LiveData that produces only the first n Items.takeUntil(predicate)
: Takes until a certain predicate is met, and does not emit anything after that, whatever the value.skip(n)
: Skips the first n values.skipUntil(predicate)
: Skips all values until a certain predicate is met (the item that actives the predicate is also emitted).elementAt(index)
: emits the item that was emitted atindex
positionnonNull()
: Will never emit the nulls to the observers (outputs aNonNullLiveData
).defaultIfNull(value)
: Will produce thevalue
whennull
is recieved.
Combining
merge(List<LiveData>)
: Merges multiple LiveData, and emits any item that was emitted by any of themLiveData.merge(LiveData)
: Merges this LiveData with another one, and emits any item that was emitted by any of themconcat(LiveData...)
: Concats multiple LiveData objects (and converts them toSingleLiveData
if necessary, and emits their first item in order. (Please check the note below.)LiveData.then(LiveData)
: Concats the first LiveData with the given one. (Please check the note below.)LiveData.merge(LiveData)
: Merges this LiveData with another one, and emits any item that was emitted by any of themstartWith(startingValue)
: Emits thestartingValue
before any other value.zip(firstLiveData, secondLiveData)
: zips both of the LiveData and emits a value after both of them have emitted their values, after that, emits values whenever any of them emits a value.
Transforming
map(mapperFunction)
: Map each value emitted to another value (and type) with the given functionswitchMap(mapperFunction)
: Maps any values that were emitted by the LiveData to the given function that produces another LiveDatadoBeforeNext(OnNextAction)
: Does theonNext
function before everything actually emitting the item to the observersdoAfterNext(OnNextAction)
: Does theonNext
function after emitting the item to the observers(function) : Does theonNext
function before everything actually emitting the item to the observersbuffer(count)
: Buffers the items emitted by the LiveData, and emits them when they reach thecount
as a List.amb(LiveData...)
: Emits the items of the first LiveData that emits the item. Items of other LiveDatas will never be emitted and are not considered.toMutableLiveData()
: Converts a LiveData to a MutableLiveData with the initial value set by this LiveData's value
You can call any function prefixed with Lives
keyword.
import com.snakydesign.livedataextensions.Lives;
- Example (create a LiveData with the initial value of 2 and map each value to its String type
LiveData<String> liveData = Lives.map(Lives.just(2), new Function1<Integer, String>() { @Override public Integer invoke(Integer integer) { return String.valueOf(integer); } }) ;
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.
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.