Skip to content

Instantly share code, notes, and snippets.

@gabriel-TheCode
Last active March 10, 2022 15:57
Show Gist options
  • Select an option

  • Save gabriel-TheCode/79f682bd80fb70a590e7bcef403c3852 to your computer and use it in GitHub Desktop.

Select an option

Save gabriel-TheCode/79f682bd80fb70a590e7bcef403c3852 to your computer and use it in GitHub Desktop.
Main Repository
class MainRepository
constructor(
private val blogDao: BlogDao,
private val blogApi: BlogApi,
private val cacheMapper: CacheMapper,
private val blogMapper: BlogMapper
) {
suspend fun getBlog(): Flow<DataState<List<Blog>>> = flow {
emit(DataState.Loading)
try {
val networkBlogs = blogApi.get()
val blogs = blogMapper.mapFromEntityList(networkBlogs)
for (blog in blogs) {
blogDao.insert(cacheMapper.mapToEntity(blog))
}
val cachedBlogs = blogDao.get()
emit(DataState.Success(cacheMapper.mapFromEntityList(cachedBlogs)))
} catch (e: Exception) {
emit(DataState.Error(e))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment