Last active
May 24, 2018 07:27
-
-
Save javipacheco/fac8808bd70b9c52a0b57f312354d02f to your computer and use it in GitHub Desktop.
Getting news for ApiService on NewsModel
This file contains 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
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