Created
March 21, 2018 00:50
-
-
Save cdmunoz/c923a95c11ba9631a9043d354c09866c 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
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