Last active
June 18, 2020 18:15
-
-
Save objcode/a44563b50d35f5716fd1a435b5bc564e to your computer and use it in GitHub Desktop.
Ui state that restarts when repositoryCall changes
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
@Composable | |
fun <T> uiStateFrom( | |
repositoryCall: RepositoryCall<T> | |
): UiState<T> { | |
var state: UiState<T> by state<UiState<T>> { UiState.Loading } | |
onCommit(repositoryCall) { | |
state = UiState.Loading // we're loading again whenever repositoryCall changes | |
repositoryCall { result -> | |
state = when (result) { | |
is Result.Success -> UiState.Success(result.data) | |
is Result.Error -> UiState.Error(result.exception) | |
} | |
} | |
} | |
return state | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment