airbnb / mavericks

Mavericks: Android on Autopilot
https://airbnb.io/mavericks/
Apache License 2.0
5.83k stars 500 forks source link

One Shot event with Mavericks compose #542

Open eric-ampire opened 3 years ago

eric-ampire commented 3 years ago

Is there an equivalent of this piece of code when using

com.airbnb.android:mavericks-compose

viewModel.onAsync(
    SignInViewState::signing,
    deliveryMode = UniqueOnly("id"),
)
gpeal commented 3 years ago

It would be something like:

val signing = viewModel.collectAsState(SignInViewState::signing)
val derivedSomething = derivedStateOf { signing + foo }
val rememberSomething = remember(signing) { Foo(signing) }
LaunchedEffect(signing) {
    // Do something here.
}

Basically, you collect the item itself and then use existing Jetpack Compose APIs to make it one-shot. Does that make sense?

eric-ampire commented 3 years ago

The idea is to navigate to another view when an operation is successful, or to display a Snackbar if there is an error

gpeal commented 3 years ago

@eric-ampire you would still do it with standard Compose constructrs

val signing = viewModel.collectAsState(SignInViewState::signing)
val snackbarHostState = remember { SnackbarHostState() }
LaunchedEffect(signing) {
    if (signing.foo) {
        snackbarHostState.showSnackbar(
            message = "Hello World,
        )
    }
}