Skip to content

Commit

Permalink
Release version 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Jun 14, 2020
1 parent 00f7594 commit 2058a0e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
Add a dependency code to your **module**'s `build.gradle` file.
```gradle
dependencies {
implementation "com.github.skydoves:sandwich:1.0.1"
implementation "com.github.skydoves:sandwich:1.0.2"
}
```

Expand Down Expand Up @@ -134,7 +134,7 @@ response.onError {
### ResponseDataSource
ResponseDataSource is an implementation of the `DataSource` interface. <br>
* Asynchronously send requests.
* A response data holder from the REST API call for caching data on memory.
* A temporarily response data holder from the REST API call for caching data on memory.
* Observable for every response.
* Retry fetching data when the request gets failure.
* Concat another `DataSource` and request sequentially.
Expand Down Expand Up @@ -175,6 +175,15 @@ dataSource.observeResponse {
}
```

#### RetainPolicy
We can limit the policy for retaining data on the temporarily internal storage.<br>
The default policy is no retaining any fetched data from the network, but we can set the policy using `dataRetainPolicy` method.
```kotlin
// Retain fetched data on the memory storage temporarily.
// If request again, returns the retained data instead of re-fetching from the network.
dataSource.dataRetainPolicy(DataRetainPolicy.RETAIN)
```

#### Invalidate
Invalidate a cached (holding) data and re-fetching the API request.

Expand All @@ -197,6 +206,22 @@ dataSource1
.concat(dataSource3) // request dataSource3's API call after the success of the dataSource2.
```

#### asLiveData
we can observe fetched data via `DataSource` as a `LiveData`.
```kotlin
val posterListLiveData: LiveData<List<Poster>>

init {
posterListLiveData = disneyService.fetchDisneyPosterList().toResponseDataSource()
.retry(3, 5000L)
.dataRetainPolicy(DataRetainPolicy.RETAIN)
.request {
// ...
}.asLiveData()
}
```


Here is the example of the `ResponseDataSource` in the `MainViewModel`.
```kotlin
class MainViewModel constructor(
Expand Down Expand Up @@ -298,6 +323,18 @@ class MainViewModel constructor(disneyService: DisneyService) : ViewModel() {
.request() // must call request()
```

#### toResponseDataSource
We can change `DataSource` to `ResponseDataSource` after getting instance from network call using the below method.
```kotlin
private val dataSource: ResponseDataSource<List<Poster>>

init {
dataSource = disneyService.fetchDisneyPosterList().toResponseDataSource()

//...
}
```


## Find this library useful? :heart:
Support it by joining __[stargazers](https://github.com/skydoves/sandwich/stargazers)__ for this repository. :star: <br>
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/skydoves/sandwichdemo/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.skydoves.sandwichdemo

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.skydoves.sandwich.DataRetainPolicy
Expand All @@ -35,15 +36,15 @@ import timber.log.Timber
class MainViewModel constructor(disneyService: DisneyService) : ViewModel() {

// request API call Asynchronously and holding successful response data.
private val dataSource: ResponseDataSource<List<Poster>>

val posterListLiveData = MutableLiveData<List<Poster>>()

val posterListLiveData: LiveData<List<Poster>>
val toastLiveData = MutableLiveData<String>()

init {
Timber.d("initialized MainViewModel.")

dataSource = disneyService.fetchDisneyPosterList().toResponseDataSource()
posterListLiveData = disneyService.fetchDisneyPosterList().toResponseDataSource()
// retry fetching data 3 times with 5000L interval when the request gets failure.
.retry(3, 5000L)
// a retain policy for retaining data on the internal storage
Expand All @@ -56,7 +57,6 @@ class MainViewModel constructor(disneyService: DisneyService) : ViewModel() {
// handle the case when the API request gets a success response.
onSuccess {
Timber.d("$data")
posterListLiveData.postValue(data)
}
// handle the case when the API request gets a error response.
// e.g. internal server error.
Expand All @@ -81,6 +81,6 @@ class MainViewModel constructor(disneyService: DisneyService) : ViewModel() {
Timber.d(message())
toastLiveData.postValue(message())
}
}
}.asLiveData()
}
}
4 changes: 2 additions & 2 deletions dependencies.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ext.versions = [
minSdk : 16,
compileSdk : 29,
versionCode : 2,
versionName : '1.0.1',
versionCode : 3,
versionName : '1.0.2',

gradleBuildTool : '3.6.3',
spotlessGradle : '4.3.0',
Expand Down

0 comments on commit 2058a0e

Please sign in to comment.