Last active
June 13, 2020 15:45
-
-
Save raulh82vlc/f8b822a1b9743269e70206df31cf5927 to your computer and use it in GitHub Desktop.
TweetRepositoryImpl returns an Observable of Tweets by a flow builder emitting from DB and DB fresh from network
This file contains 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
fun getSearchTweets(query: String) | |
: Observable<List<Tweet>> = | |
flow<List<Tweet>> { | |
val accessToken = tokenDBDataSource.getAccessToken()?: throw BadTokenException("No token was found") | |
val token = accessToken.token | |
// retrieve old values from DB | |
emit(tweetsDBDataSource.retrieveAllTweetsForTweetsIds(tweetIds)) | |
// get fresh values from network & saved them into DB | |
val eitherResult = networkDataSource.search(token, query) | |
val tweets = mapperTweets.map(eitherResult) | |
tweetQueryJoinDBDataSource.deleteTweets(query) | |
tweetDBDataSource.insertTweets(tweets) | |
insertQuery(query) | |
tweetQueryJoinDBDataSource.insertQueryAndTweetsId(tweetQueryJoinDBDataSource.convert(tweets, query)) | |
// saved network into DB & emit fresh values from DB | |
val tweetIds = tweetQueryJoinDBDataSource.retrieveAllTweetsIdAQuery(query) | |
emit(tweetsDataSource.retrieveAllTweetsForTweetsIds(tweetIds)) | |
}.flowOn(taskThreading.ioDispatcher()) | |
.asObservable() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment