API Response Wrapper to Reduce Boilerplate by creating single sealed class to manage API Response State. Copied from Stevdza-san's gist.
So You don't have to copy them again from the gist, just add this library to your dependency:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven("https://jitpack.io") // This line
}
}[version]
apiresponsewrapper = "<latest version here>"
[libraries]
apiResponseWrapper = { module = "com.github.rmaprojects:apiresponsewrapper", version.ref = "apiresponsewrapper" }
implementation(libs.apiResponseWrapper)
implementation("com.github.rmaprojects:apiresponsewrapper:<latest version here>")
See more on samples
val homeScreenState by viewModel.homeScreeState.collectAsState(initial = HomeScreenState.Idle)
when (homeScreenState) {
is HomeStates.Error -> {
// Your UI
}
is HomeStates.Loading -> {
// Your UI
}
is HomeStates.Success -> {
// Your UI
}
} val data = viewModel.data.collectAsState(initial = ResponseState.Idle)
data.value.DisplayResult(
onLoading = {
//Put your UI Here
},
onSuccess = { data ->
//Put your UI Here
},
onError = { errorMessage ->
//Put your UI Here
}
)
//You can check wether the response is Success or Error outside of DisplayResult scope:
val checkIfError = data.value.isError()
val checkIfSuccess = data.value.isSuccess()