https://github.com/climacell/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.
https://github.com/climacell/StatefulLiveData
android coroutines google-tasks livedata retrofit retrofit2 stateful statefullivedata
Last synced: about 1 year ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/climacell/StatefulLiveData
- Owner: climacell
- License: mit
- Created: 2019-12-16T14:42:51.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-05-11T08:58:13.000Z (almost 3 years ago)
- Last Synced: 2023-05-11T10:15:46.726Z (almost 3 years ago)
- Topics: android, coroutines, google-tasks, livedata, retrofit, retrofit2, stateful, statefullivedata
- Language: Kotlin
- Homepage:
- Size: 86.9 KB
- Stars: 18
- Watchers: 11
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# StatefulLiveData
[](https://jitpack.io/#climacell/statefullivedata) [](https://android-arsenal.com/api?level=14)
**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
```gradle
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.climacell.statefullivedata:core:1.0.0'
}
```
#### Maven setup
```maven-pom
jitpack.io
https://jitpack.io
com.github.climacell.statefullivedata
core
1.0.0
```
### Usage
#### Create a StatefulLiveData object:
```kotlin
val myStatefulLiveData: MutableStatefulLiveData = MutableStatefulLiveData()
```
#### Observe:
```kotlin
myStatefulLiveData.observe(lifecycleOwner, Observer { statefulData: StatefulData ->
when (statefulData) {
is StatefulData.Success -> {
showMyData(statefulData.data)
}
is StatefulData.Loading -> {
showProgressBar()
}
is StatefulData.Error -> {
showError(statefulData.throwable)
}
}
})
```
#### Update states:
Put success state
```kotlin
myStatefulLiveData.putData("My data String.")
```
Put loading state
```kotlin
myStatefulLiveData.putLoading()
```
Put error state
```kotlin
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](https://github.com/climacell/StatefulLiveData/tree/master/core) - The essential components of the StatefulLiveData framework.
- [Coroutines](https://github.com/climacell/StatefulLiveData/tree/master/coroutines) - Additional functionalities to further enhance StatefulLiveData with coroutines.
- [Google-Tasks](https://github.com/climacell/StatefulLiveData/tree/master/google-tasks) - Provides easy conversions from Tasks to StatefulLiveData.
- [Retrofit](https://github.com/climacell/StatefulLiveData/tree/master/retrofit) - A Retrofit adapter to convert calls to StatefulLiveData.