Skip to content

Instantly share code, notes, and snippets.

@anibalbastiass
Created January 3, 2020 19:19
Show Gist options
  • Save anibalbastiass/bae4ec1af05681dce2070ee0e7c39210 to your computer and use it in GitHub Desktop.
Save anibalbastiass/bae4ec1af05681dce2070ee0e7c39210 to your computer and use it in GitHub Desktop.
CurrenciesFragment.kt
private fun initViewModel() {
// Fetch Latest Currencies
with(currenciesViewModel) {
observe(getLatestCurrenciesLiveData(), ::observeCurrencies)
fetchCurrencies()
}
}
private fun observeCurrencies(result: Result<UiCurrencies>?) {
when (result) {
is Result.OnLoading -> currenciesViewModel.isLoading.set(true)
is Result.OnSuccess -> {
currenciesViewModel.isLoading.set(false)
setLatestCurrenciesData(result.value)
}
is Result.OnError -> showErrorView()
else -> showErrorView()
}
}
private fun fetchCurrencies() {
currenciesViewModel.apply {
getLatestCurrenciesLiveData().value?.let { result ->
setLatestCurrenciesData((result as? Result.OnSuccess)?.value)
} ?: run {
isLoading.set(true)
getLatestCurrenciesData()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment