Skip to content

Latest commit

 

History

History

lifecycle-livedata-ktx

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

lifecycle-livedata-ktx

Version License

Extended set of extensions for dealing with LiveData.

ℹ️ Since Kotlin Coroutines 1.4.0 you can use StateFlow instead of LiveData to hold screen state in this case you don't want this library.


Installation

Add the dependency:

repositories {
    mavenCentral()
    google()
}

dependencies {
    implementation("com.redmadrobot.extensions:lifecycle-livedata-ktx:2.3.1-0")
}

This library applies next dependencies, so you don't need to apply it manually:

  • androidx.lifecycle:lifecycle-livedata-ktx:2.3.1
  • androidx.lifecycle:lifecycle-livedata-core-ktx:2.3.1

Also, it depends on:

  • androidx.activity:activity:1.2.3
  • androidx.annotation:annotation:1.2.0
  • androidx.fragment:fragment:1.3.5

Usage

Extension Description
LiveData<T>.requireValue(): T
Get non-null value or throw exception
LiveData<X>.mapDistinct(
transform: (X) -> Y
): LiveData<Y>
Sequential call of map and distinctUntilChanged
Fragment.observe(
liveData: LiveData<T>,
onChanged: (T) -> Unit
)
Shorter way to observe LiveData in a fragment
ComponentActivity.observe(
liveData: LiveData<T>,
onChanged: (T) -> Unit
)
Shorter way to observe LiveData in an activity

LiveData delegate

To access value in LiveData you can use delegate:

import com.redmadrobot.extensions.lifecycle.provideDelegate

val liveData = MutableLiveData<SomeViewState>(initialState)
var state: SomeViewState by liveData

Contributing

Merge requests are welcome. For major changes, please open an issue first to discuss what you would like to change.