Skip to content

Instantly share code, notes, and snippets.

@objcode
Last active June 18, 2020 18:15
Show Gist options
  • Save objcode/a44563b50d35f5716fd1a435b5bc564e to your computer and use it in GitHub Desktop.
Save objcode/a44563b50d35f5716fd1a435b5bc564e to your computer and use it in GitHub Desktop.
Ui state that restarts when repositoryCall changes
@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