Last active
March 10, 2022 15:57
-
-
Save gabriel-TheCode/79f682bd80fb70a590e7bcef403c3852 to your computer and use it in GitHub Desktop.
Main Repository
This file contains hidden or 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
| 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