https://kotlinlang.org logo
Title
v

Vahalaru

02/11/2022, 12:39 AM
What is the difference or what would the reasoning be behind using rememberScaffoldState as a peramitor instead of in the function itself. Such as... @Composable fun MyScreen( state: UiState<List<Movie>>, scaffoldState: ScaffoldState = rememberScaffoldState() ) { // Instead of // val scaffoldState = rememberScaffoldState() // If the UI state contains an error, show snackbar if (state.hasError) { //
LaunchedEffect
will cancel and re-launch if //
scaffoldState.snackbarHostState
changes LaunchedEffect(scaffoldState.snackbarHostState) { // Show snackbar using a coroutine, when the coroutine is cancelled the // snackbar will automatically dismiss. This coroutine will cancel whenever //
state.hasError
is false, and only start when
state.hasError
is true // (due to the above if-check), or if
scaffoldState.snackbarHostState
changes. scaffoldState.snackbarHostState.showSnackbar( message = "Error message", actionLabel = "Retry message" ) } } Scaffold(scaffoldState = scaffoldState) { /* ... */ } }
:thread-please: 1
I apologize but on my phone the code block wouldn't work.
k

Kirill Grouchnikov

02/11/2022, 12:50 AM
This is useful when you want to keep track of the changes in that scaffold state higher up in your hierarchy. So for example, passing
MutableInteractionSource
to a button lets you "inject" interactions to emulate programmatic changes to the pressed / hovered / focused / ... state. Or to pass a
ScrollState
and then programmatically call
animateScrollTo
based on a certain condition - outside the specific inner logic of that scrollable component.
v

Vahalaru

02/11/2022, 12:55 AM
Whoa whoa whoa, lol my brain is still hurting from the last thing I learned. Could you use an example with scaffold state? The relevancy would help me put the why with the when.
k

Kirill Grouchnikov

02/11/2022, 12:57 AM
Sorry, I haven't worked with that particular component yet. But that's the idea, to have sort of a communication channel between the "parent" and the "child" composable beyond the data state.
Looks like you can do
ScaffoldState.snackbarHostState.showSnackbar
for example, but I'm not sure how relevant that is
So here, this
MyScreen
shows the error snackbar based on a certain data state. But you could also do so in the caller of
MyScreen
if that caller passed its own
ScaffoldState
instance.
v

Vahalaru

02/11/2022, 1:28 AM
So would you ever want to do that with navcontroller? I guess what I'm most confused with is if for example @Composable fun ANavControler() Val navcontroller = rememberNavcontroller() NavHost(navController) @Composable fun someScreen(navController: Navcontroller) @Composable fun anotherScreen(navcontroller = rememberNavController) What is the difference? In some screen is it recieving the remembered navcontroller and anotherScreen isn't? Would there ever be a reason to do it like anotherScreen? The only examples I've seen have never used it with navcontroller why is that?
If I understand you correctly in this case one could have all buttons in navhost and inverse it
k

Kirill Grouchnikov

02/11/2022, 1:31 AM
The difference between
someScreen
and
anotherScreen
is that the first one requires you to pass a controller, and the second one provides a default one if you don't need that communication channel and are OK with that default.
v

Vahalaru

02/11/2022, 3:00 AM
Ohhhhh...  🤯
Boy I feel stupid. I think I was trying to make it more complicated than it was. 😅