Kotlin extension for LiveData, chaining like RxJava
Kotlin extension for LiveData. This library focuses on three things:
About Kotlin friendly, thanks to
androidx.lifecycle:livedata:2.0.0release, you can explicitly set optional type for LiveData. LiveData and LiveData are supported now. However, you can still set null value, for instance:
// Allow to set null value to LiveData val liveData = MutableLiveData() liveData.value = null// LiveDataKtx doesn't allow to null value val liveData = MutableLiveDataKtx() liveData.value = null // doesn't allow
// Set nullable in LiveDataKtx val liveData = MutableLiveDataKtx() liveData.value = null
To add LiveData KTX to your project, add the following to your app module's build.gradle:
implementation "com.shopify:livedata-ktx:VERSION"
val liveData = LiveData() val liveDataKtx = liveData.toKtx() val nullableLiveDataKtx = liveData.toNullableKtx() val anotherNullableLiveDataKtx = LiveDataKtx()val mutableLiveData = MutableLiveData() val mutableLiveDataKtx = mutableLiveData.toKtx() val nullableMutableLiveDataKtx = mutableLiveData.toNullableKtx() val anotherNullableMutableLiveDataKtx = MutableLiveDataKtx()
val mediatorLiveData = MediatorLiveData() val mediatorLiveDataKtx = mediatorLiveData.toKtx() val nullableMediatorLiveDataKtx = mediatorLiveData.toNullableKtx() val anotherNullableMediatorLiveDataKtx = MediatorLiveDataKtx()
val liveData = MutableLiveDataKtx() liveData .filter { it == false } .map { true } .observe(lifecycleOwner, Observer { result -> // result is non-null and always true })
It is a lifecycle-aware observable that sends only new updates after subscription, used for events like navigation and Snackbar messages.
livedata-ktxhas different implementation comparing to SingleLiveEvent from google samples android-architecture.
val liveData = PublishLiveDataKtx() val actual = mutableListOf() val observer = Observer { actual.add(it) } liveData.value = -1liveData.observeForever(observer) liveData.value = 0 liveData.removeObserver(observer)
assertEquals(listOf(0), actual) actual.clear()
liveData.value = 1 liveData.value = 2 liveData.observeForever(observer) liveData.value = 3
assertEquals(listOf(3), actual)
For more use cases, please see the tests at LiveDataKtxTest.kt
LiveDataKtx will throw NPE if you try to get value when it supposes to be nonNull. Use
safeValuein this case.
val liveDataKtx = MutableLiveDataKtx()// Throw NPE if the value hasn't been set yet as it is defined as nonNull liveDataKtx.value
// This works fine as the value has been set liveDataKtx.value = true liveDataKtx.value
// safeValue always returns nullable value and does not throw NPE liveDataKtx.safeValue
// observe doesn't throw NPE even when the value hasn't been set liveDataKtx.observe(...)
It is easy to add your custom extension without requiring to send a PR. For example:
/** * filter */ private class FilterOperator(val predicate: (T) -> Boolean) : Operator {override fun run(output: MediatorLiveDataKtx<t>, value: T) { if (predicate.invoke(value)) { output.value = value } }
}
fun LiveDataKtx.filter(predicate: (T) -> Boolean): LiveDataKtx = Extension.create(this, FilterOperator(predicate))
fun MutableLiveDataKtx.filter(predicate: (T) -> Boolean): MutableLiveDataKtx = Extension.create(this, FilterOperator(predicate))
fun MediatorLiveDataKtx.filter(predicate: (T) -> Boolean): MediatorLiveDataKtx = Extension.create(this, FilterOperator(predicate))
Any contributions are welcome! Please check the CONTRIBUTING guideline before submitting a new issue. Wanna send PR? Click HERE
The MIT License (MIT)Copyright (c) 2018, 2019 Shopify Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.