Created
June 16, 2019 14:28
-
-
Save enginebai/fd34c9aa2d1b153b6317da8486a7b1e3 to your computer and use it in GitHub Desktop.
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
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