diff --git a/app/src/main/java/com/skydoves/disneymotions/repository/DetailRepository.kt b/app/src/main/java/com/skydoves/disneymotions/repository/DetailRepository.kt index d128367..04fa791 100644 --- a/app/src/main/java/com/skydoves/disneymotions/repository/DetailRepository.kt +++ b/app/src/main/java/com/skydoves/disneymotions/repository/DetailRepository.kt @@ -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) } diff --git a/app/src/main/java/com/skydoves/disneymotions/repository/MainRepository.kt b/app/src/main/java/com/skydoves/disneymotions/repository/MainRepository.kt index 4d775cb..6c8705a 100644 --- a/app/src/main/java/com/skydoves/disneymotions/repository/MainRepository.kt +++ b/app/src/main/java/com/skydoves/disneymotions/repository/MainRepository.kt @@ -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 @@ -35,16 +34,16 @@ 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 = 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. @@ -52,22 +51,23 @@ class MainRepository constructor( 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) } diff --git a/app/src/main/java/com/skydoves/disneymotions/repository/Repository.kt b/app/src/main/java/com/skydoves/disneymotions/repository/Repository.kt index e8dae88..c991cf1 100644 --- a/app/src/main/java/com/skydoves/disneymotions/repository/Repository.kt +++ b/app/src/main/java/com/skydoves/disneymotions/repository/Repository.kt @@ -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 diff --git a/app/src/main/java/com/skydoves/disneymotions/view/ui/main/MainViewModel.kt b/app/src/main/java/com/skydoves/disneymotions/view/ui/main/MainViewModel.kt index 9f201cb..ccb2bc4 100644 --- a/app/src/main/java/com/skydoves/disneymotions/view/ui/main/MainViewModel.kt +++ b/app/src/main/java/com/skydoves/disneymotions/view/ui/main/MainViewModel.kt @@ -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 @@ -33,19 +34,25 @@ class MainViewModel constructor( private var posterFetchingLiveData: MutableLiveData = MutableLiveData() val posterListLiveData: LiveData> - val isLoading: ObservableBoolean = mainRepository.isLoading + val isLoading: ObservableBoolean = ObservableBoolean(false) val toastLiveData: MutableLiveData = 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 + } }