Skip to content

Instantly share code, notes, and snippets.

@javipacheco
Last active May 24, 2018 07:27
Show Gist options
  • Save javipacheco/fac8808bd70b9c52a0b57f312354d02f to your computer and use it in GitHub Desktop.
Save javipacheco/fac8808bd70b9c52a0b57f312354d02f to your computer and use it in GitHub Desktop.
Getting news for ApiService on NewsModel
val newsState = State<States.NewsState>()
val viewState = State<ViewState>(ViewState.IdleViewState)
fun dispatch(command: Commands): DeferredK<Unit> = when (command) {
is Commands.NewsGetItemsCommand -> {
DeferredK.monad().binding {
val newsList = newsState.getValue()
if (newsList?.items?.isEmpty() == false) {
"Loading data from state".logD()
viewState.postValue(ViewState.SuccessViewState)
newsState.postValue(newsList)
} else {
viewState.postValue(ViewState.WaitingViewState)
"Loading new data".logD()
val news = apiService.getNews().bind()
viewState.postValue(ViewState.SuccessViewState)
newsState.postValue(news)
}
Unit
}.fix()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment