livedata-ktx

by Shopify

Shopify /livedata-ktx

Kotlin extension for LiveData, chaining like RxJava

462 Stars 28 Forks Last release: over 1 year ago (v2.0.2) MIT License 67 Commits 13 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

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

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

Chaining LiveData

val liveData = MutableLiveDataKtx()
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()
val actual = mutableListOf()
val observer = Observer { 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()

// 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(...)

Feel missing methods

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

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.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.