Skip to content

Commit

Permalink
Remove isLoading overrides in repository
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Aug 29, 2020
1 parent 91d6e1e commit 5265aab
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@

package com.skydoves.disneymotions.repository

import androidx.databinding.ObservableBoolean
import com.skydoves.disneymotions.persistence.PosterDao

class DetailRepository constructor(
private val posterDao: PosterDao
) : Repository {

override var isLoading = ObservableBoolean(false)

fun getPosterById(id: Long) = posterDao.getPoster(id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.skydoves.disneymotions.repository

import androidx.databinding.ObservableBoolean
import com.skydoves.disneymotions.model.Poster
import com.skydoves.disneymotions.network.DisneyService
import com.skydoves.disneymotions.persistence.PosterDao
Expand All @@ -35,39 +34,40 @@ class MainRepository constructor(
private val posterDao: PosterDao
) : Repository {

override var isLoading = ObservableBoolean(false)

init {
Timber.d("Injection MainRepository")
}

suspend fun loadDisneyPosters(error: (String) -> Unit) = flow {
suspend fun loadDisneyPosters(
onSuccess: () -> Unit,
onError: (String) -> Unit
) = flow {
val posters: List<Poster> = posterDao.getPosterList()
if (posters.isEmpty()) {
isLoading.set(true)
// request API network call asynchronously.
disneyService.fetchDisneyPosterList().apply {
// handle the case when the API request gets a success response.
this.suspendOnSuccess {
data.whatIfNotNull {
posterDao.insertPosterList(it)
emit(it)
onSuccess()
}
}
// handle the case when the API request gets an error response.
// e.g. internal server error.
.onError {
error(message())
onError(message())
}
// handle the case when the API request gets an exception response.
// e.g. network connection error.
.onException {
error(message())
onError(message())
}
isLoading.set(false)
}
} else {
emit(posters)
onSuccess()
}
}.flowOn(Dispatchers.IO)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,5 @@

package com.skydoves.disneymotions.repository

import androidx.databinding.ObservableBoolean

/** Repository is an interface for configuring base repository classes. */
interface Repository {

// this override property is for saving network loading status.
var isLoading: ObservableBoolean
}
interface Repository
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.skydoves.disneymotions.view.ui.main

import androidx.annotation.MainThread
import androidx.databinding.ObservableBoolean
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
Expand All @@ -33,19 +34,25 @@ class MainViewModel constructor(
private var posterFetchingLiveData: MutableLiveData<Boolean> = MutableLiveData()
val posterListLiveData: LiveData<List<Poster>>

val isLoading: ObservableBoolean = mainRepository.isLoading
val isLoading: ObservableBoolean = ObservableBoolean(false)
val toastLiveData: MutableLiveData<String> = MutableLiveData()

init {
Timber.d("injection MainViewModel")

this.posterListLiveData = this.posterFetchingLiveData.switchMap {
posterListLiveData = posterFetchingLiveData.switchMap {
isLoading.set(true)
launchOnViewModelScope {
this.mainRepository.loadDisneyPosters { this.toastLiveData.postValue(it) }
.asLiveData()
this.mainRepository.loadDisneyPosters(
onSuccess = { isLoading.set(false) },
onError = { toastLiveData.postValue(it) }
).asLiveData()
}
}
}

fun fetchDisneyPosterList() = this.posterFetchingLiveData.postValue(true)
@MainThread
fun fetchDisneyPosterList() {
posterFetchingLiveData.value = true
}
}

0 comments on commit 5265aab

Please sign in to comment.