How to put snackbar button response in one suspend function in Kotlin jetpack. I have one SnackbarResult.ActionPerformed for all my snackbars. And I have suspend fun for my snackbar:
suspend fun snackBar(
snackbarHostState: SnackbarHostState, context: Context,
buttonText: Int = settings,
snackbarText: Int = permission_access
) =
snackbarHostState.showSnackbar(
context.resources.getString(snackbarText),
context.resources.getString(buttonText),
duration = SnackbarDuration.Short
)
Now I need to call this function like:
val contextSnackBar = LocalContext.current as ComponentActivity
snackBarScope.launch {
when (snackBar(context = context, snackbarHostState = snackbarHostState)) {
SnackbarResult.ActionPerformed -> {
contextSnackBar.appSettings().apply {
contextSnackBar.startActivity(this)
}
}
}
But how can I put SnackbarResult.ActionPerformed in my suspend function?
As pointed out in a comment, check out the
showSnackbardocumentation:It returns a SnackbarResult which is
So this will set the SnackbarResult automatically, you do not have to set it manually. Whenever the user clicks on the action that you defined via
actionLabel, the function will automatically returnSnackbarResult.ActionPerformed.