Skip to content

Instantly share code, notes, and snippets.

@cdmunoz
Created March 21, 2018 00:50
Show Gist options
  • Save cdmunoz/c923a95c11ba9631a9043d354c09866c to your computer and use it in GitHub Desktop.
Save cdmunoz/c923a95c11ba9631a9043d354c09866c to your computer and use it in GitHub Desktop.
class CryptocurrencyRepository @Inject constructor(val apiInterface: ApiInterface,
val cryptocurrenciesDao: CryptocurrenciesDao, val utils: Utils) {
fun getCryptocurrencies(limit: Int, offset: Int): Observable<List<Cryptocurrency>> {
val hasConnection = utils.isConnectedToInternet()
var observableFromApi: Observable<List<Cryptocurrency>>? = null
if (hasConnection){
observableFromApi = getCryptocurrenciesFromApi()
}
val observableFromDb = getCryptocurrenciesFromDb(limit, offset)
return if (hasConnection) Observable.concatArrayEager(observableFromApi, observableFromDb)
else observableFromDb
}
fun getCryptocurrenciesFromApi(): Observable<List<Cryptocurrency>> {
return apiInterface.getCryptocurrencies(Constants.START_ZERO_VALUE)
.doOnNext {
Log.e("REPOSITORY API * ", it.size.toString())
for (item in it) {
cryptocurrenciesDao.insertCryptocurrency(item)
}
}
}
fun getCryptocurrenciesFromDb(limit: Int, offset: Int): Observable<List<Cryptocurrency>> {
return cryptocurrenciesDao.queryCryptocurrencies(limit, offset)
.toObservable()
.doOnNext {
//Print log it.size :)
Log.e("REPOSITORY DB *** ", it.size.toString())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment