Created
February 9, 2022 13:17
-
-
Save dracula151997/9dc4edfc7fece8bb954431571ea21b91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun getWordInfo(word: String): Flow<Resource<List<WordInfo>>> = flow { | |
emit(Resource.Loading()) | |
val wordInfos = dao.getCachedWordInfos(word).map { it.toWordInfo() } | |
emit(Resource.Loading(wordInfos)) | |
try { | |
val remoteWorkApi = api.getWorkInfo(word) | |
dao.deleteWordInfos(remoteWorkApi.map { it.word!! }) | |
dao.insertWordInfo(remoteWorkApi.map { it.toWordInfoEntity() }) | |
} catch (e: HttpException) { | |
emit(Resource.Error("Oops! Something went error", data = wordInfos)) | |
} catch (e: IOException) { | |
emit( | |
Resource.Error( | |
"Couldn't reach to server. Check your internet connection", | |
wordInfos | |
) | |
) | |
} | |
val newWordInfos = dao.getCachedWordInfos(word).map { it.toWordInfo() } | |
emit(Resource.Success(newWordInfos)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment