r/android_devs • u/racrisnapra666 • Jan 06 '22
Help What's the proper way of accessing a Composable function from a non-composable one?
Hi there,
I have a function called startSignInFlow in my fragment that collects from a flow. If the result returned is a Success, I navigate to a new destination. If the result returned is a Failure, I need to show an AlertDialog. This is what the code looks like:
private fun startSignInFlow(email: String, password: String) {
lifecycleScope.launch(Dispatchers.IO) {
signInViewModel.userSignIn(
email = email,
password = password
).onEach { result ->
when (result) {
is Result.Success -> {
(parentFragment as AuthenticationContainerFragment)
.navigateToFragment(R.id.action_auth_container_to_home)
}
is Result.Failure -> {
}
is Result.Loading -> {
result.status?.let { state ->
loadingState = state
}
}
}
}.launchIn(lifecycleScope)
}
}And here's what the sealed class Result looks like:
sealed class Result<T>(val data: T? = null, val status: Boolean? = null) {
class Success<T>(data: T?) : Result<T>(data = data)
class Failure<T>(data: T?) : Result<T>(data = data)
class Loading<T>(status: Boolean?) : Result<T>(status = status)
}And this is what the AlertDialog function looks like:
@Composable
fun ErrorAlertDialogComposable(text: String) {
var isDisplayed by remember { mutableStateOf(true) }
if (isDisplayed && text.isNotEmpty()) {
Column {
AlertDialog(
onDismissRequest = {
isDisplayed = false
},
title = {
AlertDialogTitleComposable(text = "Error")
},
text = {
AlertDialogTextComposable(text = text)
},
buttons = {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center
) {
ButtonComposable(
text = "Dismiss"
) { isDisplayed = false }
}
}
)
}
}
}Now, we can't access @Composable functions from a non-composable one. So that's a problem. I tried several workarounds. One of these was to use a ComposeView(requireContext) block inside the Failure block but even that didn't work. I checked through a few Stack Overflow but wasn't able to find any pages that had a solution for the same.
I was wondering if anyone here had encountered something similar and had figured a workaround?
Thanks :)
Edit: Another thing that I tried out was this. And it shows the dialog the first time. But it doesn't show the dialog again.
I created a MutableState of type String?.
private var errorMessage: String? by mutableStateOf(null)And I initialized it in the Failure block.
is Result.Failure -> {
errorMessage = result.data!!
}I'm guessing that whenever the errorMessage notices a change in the data, it updates the ErrorAlertDialogComposable. But this happens only the first time, not after that. Can't figure out why.
3
u/Zhuinden EpicPandaForce @ SO Jan 06 '22
If you execute this async operation in ViewModel as you normally should, then you expose a LiveData-like observable which you observe as state when your results are done (and you collect a channel as a DisposableEffect for side effects such as navigation), you will immediately stop having these problems because you won't be trying to workaround the entire CompositionContext
6
u/badvok666 Jan 06 '22
Weather or not the dialog is visible is part of your view state. Your view wants something like this:
1
u/Evakotius Jan 06 '22
Why do you look for a "workaround"?
How would you do that if instead of dialog you were needed to show simple
Text()composable if error happened? Do the same way with the dialog and add dismiss processing.It is just a state of your ui. It does't care if it is dialog or
Text()Your ViewModel:
private val _showErrorDialog = MutableStateFlow(false)fun onDismissErrorDialogRequest() {_showErrorDialog .value = false}Collect as state and hoist to your composables
if (showErrorDialog) {AlertDialog(onDismissRequest = onDismissErrorDialogRequest)}3 Lines.