Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to send the cancelation result? #69

Closed
margarita-v opened this issue Mar 14, 2022 · 4 comments
Closed

How to send the cancelation result? #69

margarita-v opened this issue Mar 14, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@margarita-v
Copy link
Contributor

Now it's possible to use resultNavigator.navigateBack for sending confirmation result from screen. Is there any opportunity to send the cancelation result when the result screen composable is gone in order to handle both results in the same callback of recipient screen?
For example:

@Destination
@Composable
fun FeedScreen(
    openForResultClicked: () -> Unit,
    onResult: (Boolean) -> Unit,
    resultRecipient: ResultRecipient<ResultScreenDestination, Boolean>
) {
    resultRecipient.onResult { succeed ->
        onResult(succeed)
    }
    // open screen for result
}

@Destination
@Composable
fun ResultScreen(
    navBackStackEntry: NavBackStackEntry,
    resultNavigator: ResultBackNavigator<Boolean>
) {
   // Doesn't work: when the ResultScreen composable is gone, I'd like to send this cancelation result in order to handle in the same `onResult` callback of FeedScreen
    DisposableEffect(key1 = Unit) {
        onDispose {
            resultNavigator.setResult(false)
        }
    }
    Box(modifier = Modifier.fillMaxSize()) {
        Button(
            onClick = {
                // the confirmation result works when action is performed
                resultNavigator.navigateBack(true)
            },
            modifier = Modifier.align(Alignment.Center)
        ) {
            Text(textAlign = TextAlign.Center, text = "Success!")
        }
    }
}

This DisposableEffect for ResultScreen doesn't work too:

    DisposableEffect(key1 = Unit) {
        val observer = LifecycleEventObserver { source, event ->
            when (event) {

                Lifecycle.Event.ON_DESTROY -> {
                    resultNavigator.setResult(false)
                }

                else -> Unit
            }
        }

        navBackStackEntry.lifecycle.addObserver(observer)

        onDispose {
            navBackStackEntry.lifecycle.removeObserver(observer)
        }
    }

Is there any best practice to solve it or should I use different callbacks for cancelation based on composable lifecycle and confirmation based on action?

@raamcosta raamcosta added the enhancement New feature or request label Mar 14, 2022
@raamcosta
Copy link
Owner

raamcosta commented Mar 15, 2022

Hi @margarita-v ! Thank you again for reporting! :)

I remember thinking if this would ever be helpful when I was implementing the nav result feature. Turns out it is.

Would this work for your use case?

  • If ResultScreen becomes visible and after that, the FeedScreen becomes visible again and no value is received, then we consider it canceled. This would be done by the library, and the result received on onResult would be a wrapper to the current value which would allow us to check if nav result was canceled or there is actually a value:
sealed interface NavResult<out R> {
    object Canceled: NavResult<Nothing>
    data class Value<R>(val value: R): NavResult<R>
}
//...
    resultRecipient.onResult { confirmed ->
        when (confirmed) {
            is NavResult.Canceled -> println("canceled!!")
            is NavResult.Value -> if (confirmed.value) {
                navigator.navigate(SomeDestination)
            }
        }
    }

@margarita-v
Copy link
Contributor Author

I think it would be perfect 👍

@raamcosta
Copy link
Owner

Hey @margarita-v !

So the new 1.3.5-beta release contains this new enhancement and deprecates the old onResult to replace it with onNavResult.
Please try it and let me know if that works well for you! 🙂

@margarita-v
Copy link
Contributor Author

Thank you, now it works perfectly 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants