All Projects → Shopify → Livedata Ktx

Shopify / Livedata Ktx

Licence: mit
Kotlin extension for LiveData, chaining like RxJava

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Livedata Ktx

Daggerandroidmvvm
Demonstrates using Dagger 2.11+ in MVVM app with Android Architecture Components, Clean Architecture, RxJava
Stars: ✭ 255 (-45.28%)
Mutual labels:  rxjava, architecture-components, livedata
Fountain
Android Kotlin paged endpoints made easy
Stars: ✭ 175 (-62.45%)
Mutual labels:  rxjava, architecture-components, livedata
ReactiveLiveData
Transformation functions for LiveData
Stars: ✭ 80 (-82.83%)
Mutual labels:  livedata, architecture-components
LifecycleCells
An Android library that provides a Lifecycle to any ViewHolder through the implementation of the LifecycleOwner interface, allowing it to interact with a Lifecycle-Aware Component.
Stars: ✭ 19 (-95.92%)
Mutual labels:  livedata, architecture-components
ProxerAndroid
The official Android App of Proxer.Me
Stars: ✭ 105 (-77.47%)
Mutual labels:  rxjava, architecture-components
Wanandroid
🏄 基于Architecture Components dependencies (Lifecycles,LiveData,ViewModel,Room)构建的WanAndroid开源项目。 你值得拥有的MVVM快速开发框架:https://github.com/jenly1314/MVVMFrame
Stars: ✭ 410 (-12.02%)
Mutual labels:  architecture-components, livedata
movies
An example approach for modularization, reactive clean architecture and persistancy.
Stars: ✭ 110 (-76.39%)
Mutual labels:  livedata, architecture-components
AndroidGo
Android、Flutter 开发者帮助 APP。包含事件分发、性能分析、Google Jetpack组件、OkHttp、RxJava、Retrofit、Volley、Canvas绘制以及优秀博文代码案例等内容,帮助开发者快速上手!
Stars: ✭ 30 (-93.56%)
Mutual labels:  rxjava, livedata
Githubarchitecturecomponents
The implementation of Android "Architecture Components" sample explained by Google : https://developer.android.com/topic/libraries/architecture/guide.html
Stars: ✭ 302 (-35.19%)
Mutual labels:  architecture-components, livedata
Android Architecture Components Kotlin
Sample used to practice Kotlin and Android Architecture Components.
Stars: ✭ 326 (-30.04%)
Mutual labels:  architecture-components, livedata
Gank
干货集中营 app 安卓实现,基于 RxFlux 架构使用了 RxJava、Retrofit、Glide、Koin等
Stars: ✭ 444 (-4.72%)
Mutual labels:  rxjava, architecture-components
LiveData-DataBinding-Kotlin
Sample to practice LiveData + DataBinding
Stars: ✭ 89 (-80.9%)
Mutual labels:  livedata, architecture-components
Superhero-App
🦸🏻‍♂️🦹🏻‍♀️Superhero app built with Kotlin, ViewModel, LiveData, ViewBinding, Room, and Hilt
Stars: ✭ 27 (-94.21%)
Mutual labels:  livedata, architecture-components
Android-Mvi-Starter
Android MVI Starter application
Stars: ✭ 19 (-95.92%)
Mutual labels:  livedata, architecture-components
android-clean-architecture
Android Sample Clean Architecture App written in Kotlin. (MVVM, dagger2, RXjava, data binding, Live data,room)
Stars: ✭ 29 (-93.78%)
Mutual labels:  rxjava, livedata
AndroidCleanArchitecture
Android Project with clean android architecture contain Dagger, Retrofit, Retrofit, Android archtecture components, LiveData with MVVM architecture
Stars: ✭ 22 (-95.28%)
Mutual labels:  livedata, architecture-components
Android Jetpack Demo
🔥 快速入门Android Jetpack以及相关Kotlin、RxJava、MVVM等主流技术,独立构架App的基础技能
Stars: ✭ 335 (-28.11%)
Mutual labels:  rxjava, livedata
KTAndroidArchitecture
A Kotlin android architecture with Google Architecture Components
Stars: ✭ 33 (-92.92%)
Mutual labels:  livedata, architecture-components
Simple-Notes-Kotlin-App
✍️ Simple Note Making App use mvvm architecture , dagger , coroutines and navigation component. Features includes 🗒️ create , edit and ❌ delete notes
Stars: ✭ 40 (-91.42%)
Mutual labels:  livedata, architecture-components
Architecturecomponentsdemo
Kotlin demo project that uses some Android Architecture Components (ViewModel and LiveData) with Dagger 2 and Coroutines
Stars: ✭ 269 (-42.27%)
Mutual labels:  architecture-components, livedata

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].