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