Skip to content

Instantly share code, notes, and snippets.

@dracula151997
Created February 9, 2022 13:17
Show Gist options
  • Save dracula151997/9dc4edfc7fece8bb954431571ea21b91 to your computer and use it in GitHub Desktop.
Save dracula151997/9dc4edfc7fece8bb954431571ea21b91 to your computer and use it in GitHub Desktop.
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