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.
Adding the dependency to the project using gradle or maven.
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.climacell.statefullivedata:core:1.0.0'
}
<!-- <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>
val myStatefulLiveData: MutableStatefulLiveData<String> = MutableStatefulLiveData<String>()
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)
}
}
})
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.
Coming soon.
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.