Source code can be found at : https://github.com/AliRezaeiii/TVMaze
I have following repository class :
class ShowRepository(
private val showDao: ShowDao,
private val api: TVMazeService
) {
/**
* A list of shows that can be shown on the screen.
*/
val shows: LiveData<List<Show>> =
Transformations.map(showDao.getShows()) {
it.asDomainModel()
}
/**
* Refresh the shows stored in the offline cache.
*/
suspend fun refreshShows(): Result<List<Show>> = withContext(Dispatchers.IO) {
try {
val news = api.fetchShowList().await()
showDao.insertAll(*news.asDatabaseModel())
Result.Success(news)
} catch (err: HttpException) {
Result.Error(err)
}
}
}
As I understand Room does not support MutableLiveData rather it support LiveData. So I have created two object in my ViewModel to be observed :
class MainViewModel(
private val repository: ShowRepository,
app: Application
) : AndroidViewModel(app) {
private val _shows = repository.shows
val shows: LiveData<List<Show>>
get() = _shows
private val _liveData = MutableLiveData<Result<List<Show>>>()
val liveData: LiveData<Result<List<Show>>>
get() = _liveData
/**
* init{} is called immediately when this ViewModel is created.
*/
init {
if (isNetworkAvailable(app)) {
viewModelScope.launch {
_liveData.postValue(repository.refreshShows())
}
}
}
}
I use show
LiveData varialbe to submit list in my Activity :
viewModel.shows.observe(this, Observer { shows ->
viewModelAdapter.submitList(shows)
})
And I use LiveData
variable to display error message when an exception occurs in refreshShows()
of repository :
viewModel.liveData.observe(this, Observer { result ->
if (result is Result.Error) {
Toast.makeText(this, getString(R.string.failed_loading_msg), Toast.LENGTH_LONG).show()
Timber.e(result.exception)
}
})
Do you think is there a better solution to have one LiveData in ViewModel rather than two?