StatefulLiveData
StatefulLiveData is a lean, yet powerful tool that harnesses the capabilities of LiveData and enhances them, enabling the observer to distinguish between different states the data can be in, such as Success, Loading and Error.
StatefulLiveData is open-ended, which gives you possibility to create more types of StatefulData, as well as extensions and functionality.
Quick start guide
Setup
Adding the dependency to the project using gradle or maven.
Gradle setup
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.climacell.statefullivedata:core:1.0.0'
}
Maven setup
<!-- <repositories> section of pom.xml -->
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<!-- <dependencies> section of pom.xml -->
<dependency>
<groupId>com.github.climacell.statefullivedata</groupId>
<artifactId>core</artifactId>
<version>1.0.0</version>
</dependency>
Usage
Create a StatefulLiveData object:
val myStatefulLiveData: MutableStatefulLiveData<String> = MutableStatefulLiveData<String>()
Observe:
myStatefulLiveData.observe(lifecycleOwner, Observer { statefulData: StatefulData<String> ->
when (statefulData) {
is StatefulData.Success -> {
showMyData(statefulData.data)
}
is StatefulData.Loading -> {
showProgressBar()
}
is StatefulData.Error -> {
showError(statefulData.throwable)
}
}
})
Update states:
Put success state
myStatefulLiveData.putData("My data String.")
Put loading state
myStatefulLiveData.putLoading()
Put error state
myStatefulLiveData.putError(IllegalArgumentException())
Thats it! You are ready to go! =)
For more awesome capabilities and other super powers check out the other modules that accompany the core module. Also make sure to look at the kdoc in the library.
Documentation
Coming soon.
Modules
There are 4 modules comprising StatefulLiveData:
- Core - The essential components of the StatefulLiveData framework.
- Coroutines - Additional functionalities to further enhance StatefulLiveData with coroutines.
- Google-Tasks - Provides easy conversions from Tasks to StatefulLiveData.
- Retrofit - A Retrofit adapter to convert calls to StatefulLiveData.