Skip to content

Instantly share code, notes, and snippets.

@enginebai
Created June 16, 2019 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enginebai/fd34c9aa2d1b153b6317da8486a7b1e3 to your computer and use it in GitHub Desktop.
Save enginebai/fd34c9aa2d1b153b6317da8486a7b1e3 to your computer and use it in GitHub Desktop.
interface PostRepository {
fun fetchFeeds(): Completable
fun getFeeds(): Observable<List<Post>>
}
class PostRepositoryImpl : PostRepository {
private val remoteDataSource: PostApiService by inject()
private val localDataSource: PostDao by inject()
override fun fetchFeeds(): Completable {
return remoteDataSource.fetchFeeds()
.flatMapObservable {
if (it.isSuccessful)
Observable.fromIterable(it.body())
else
throw HttpException(it)
}.concatMapCompletable {
Completable.fromAction {
localDataSource.upsert(it)
}
}
}
override fun getFeeds(): Observable<List<Post>> {
return localDataSource.getPostList()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment