• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Shopify/livedata-ktx: Kotlin extension for LiveData, chaining like RxJava

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

Shopify/livedata-ktx

开源软件地址(OpenSource Url):

https://github.com/Shopify/livedata-ktx

开源编程语言(OpenSource Language):

Kotlin 100.0%

开源软件介绍(OpenSource Introduction):

GitHub license Download Build Status

livedata-ktx

Kotlin extension for LiveData. This library focuses on three things:

  • Kotlin friendly.
  • Preserve immutability.
  • Extensibility.

About Kotlin friendly, thanks to androidx.lifecycle:livedata:2.0.0 release, you can explicitly set optional type for LiveData. LiveData and LiveData<Boolean?> are supported now. However, you can still set null value, for instance:

// 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

README For 1.x

README For 2.x

Getting Started

To add LiveData KTX to your project, add the following to your app module's build.gradle:

implementation "com.shopify:livedata-ktx:VERSION"

Usage

From LiveData to LiveDataKtx

val 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 LiveData

val 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. livedata-ktx has different implementation comparing to SingleLiveEvent from google samples android-architecture.

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 safeValue

LiveDataKtx will throw NPE if you try to get value when it supposes to be nonNull. Use safeValue in this case.

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 methods

It 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))

Contributing

Any contributions are welcome! Please check the CONTRIBUTING guideline before submitting a new issue. Wanna send PR? Click HERE

Maintainers

License

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.



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
KDE/kile: Integrated LaTeX Editing Environment发布时间:2022-07-09
下一篇:
atilsamancioglu/IA25-KotlinDataBindingCountries发布时间:2022-07-07
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap