在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Shopify/livedata-ktx开源软件地址(OpenSource Url):https://github.com/Shopify/livedata-ktx开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):livedata-ktxKotlin extension for LiveData. This library focuses on three things:
About Kotlin friendly, thanks to // Allow to set null value to LiveData
val liveData = MutableLiveData<Boolean>()
liveData.value = null
// LiveDataKtx doesn't allow to null value
val liveData = MutableLiveDataKtx<Boolean>()
liveData.value = null // doesn't allow
// Set nullable in LiveDataKtx
val liveData = MutableLiveDataKtx<Boolean?>()
liveData.value = null Getting StartedTo add LiveData KTX to your project, add the following to your app module's build.gradle: implementation "com.shopify:livedata-ktx:VERSION" UsageFrom LiveData to LiveDataKtxval liveData = LiveData<Boolean>()
val liveDataKtx = liveData.toKtx()
val nullableLiveDataKtx = liveData.toNullableKtx()
val anotherNullableLiveDataKtx = LiveDataKtx<Boolean?>()
val mutableLiveData = MutableLiveData<Boolean>()
val mutableLiveDataKtx = mutableLiveData.toKtx()
val nullableMutableLiveDataKtx = mutableLiveData.toNullableKtx()
val anotherNullableMutableLiveDataKtx = MutableLiveDataKtx<Boolean?>()
val mediatorLiveData = MediatorLiveData<Boolean>()
val mediatorLiveDataKtx = mediatorLiveData.toKtx()
val nullableMediatorLiveDataKtx = mediatorLiveData.toNullableKtx()
val anotherNullableMediatorLiveDataKtx = MediatorLiveDataKtx<Boolean?>() Chaining LiveDataval liveData = MutableLiveDataKtx<Boolean>()
liveData
.filter { it == false }
.map { true }
.observe(lifecycleOwner, Observer { result ->
// result is non-null and always true
}) PublishLiveData (SingleLiveData in version 2.x)It is a lifecycle-aware observable that sends only new updates after subscription, used for events like navigation and Snackbar messages. val liveData = PublishLiveDataKtx<Int>()
val actual = mutableListOf<Int>()
val observer = Observer<Int> { actual.add(it) }
liveData.value = -1
liveData.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 Use safeValueLiveDataKtx will throw NPE if you try to get value when it supposes to be nonNull. Use val liveDataKtx = MutableLiveDataKtx<Boolean>()
// Throw NPE if the value hasn't been set yet as it is defined as nonNull <Boolean>
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(...) Feel missing methodsIt is easy to add your custom extension without requiring to send a PR. For example: /**
* filter
*/
private class FilterOperator<T>(val predicate: (T) -> Boolean) : Operator<T, T> {
override fun run(output: MediatorLiveDataKtx<T>, value: T) {
if (predicate.invoke(value)) {
output.value = value
}
}
}
fun <T> LiveDataKtx<T>.filter(predicate: (T) -> Boolean): LiveDataKtx<T> =
Extension.create(this, FilterOperator(predicate))
fun <T> MutableLiveDataKtx<T>.filter(predicate: (T) -> Boolean): MutableLiveDataKtx<T> =
Extension.create(this, FilterOperator(predicate))
fun <T> MediatorLiveDataKtx<T>.filter(predicate: (T) -> Boolean): MediatorLiveDataKtx<T> =
Extension.create(this, FilterOperator(predicate)) ContributingAny contributions are welcome! Please check the CONTRIBUTING guideline before submitting a new issue. Wanna send PR? Click HERE Maintainers
License
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论