Created
March 8, 2018 02:17
-
-
Save cdmunoz/0e8e727a3b882249bb385457dcbcdd5c 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 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