Skip to content

Instantly share code, notes, and snippets.

@cdmunoz
Created March 8, 2018 02:17
Show Gist options
  • Save cdmunoz/0e8e727a3b882249bb385457dcbcdd5c to your computer and use it in GitHub Desktop.
Save cdmunoz/0e8e727a3b882249bb385457dcbcdd5c to your computer and use it in GitHub Desktop.
class CryptocurrenciesViewModel @Inject constructor(
private val cryptocurrencyRepository: CryptocurrencyRepository) : ViewModel() {
var cryptocurrenciesResult: MutableLiveData<List<Cryptocurrency>> = MutableLiveData()
var cryptocurrenciesError: MutableLiveData<String> = MutableLiveData()
lateinit var disposableObserver: DisposableObserver<List<Cryptocurrency>>
fun cryptocurrenciesResult(): LiveData<List<Cryptocurrency>> {
return cryptocurrenciesResult
}
fun cryptocurrenciesError(): LiveData<String> {
return cryptocurrenciesError
}
fun loadCryptocurrencies() {
disposableObserver = object : DisposableObserver<List<Cryptocurrency>>() {
override fun onComplete() {
}
override fun onNext(cryptocurrencies: List<Cryptocurrency>) {
cryptocurrenciesResult.postValue(cryptocurrencies)
}
override fun onError(e: Throwable) {
cryptocurrenciesError.postValue(e.message)
}
}
cryptocurrencyRepository.getCryptocurrencies()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.debounce(400, MILLISECONDS)
.subscribe(disposableObserver)
}
fun disposeElements(){
if(null != disposableObserver && !disposableObserver.isDisposed) disposableObserver.dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment