Hi guys, in case you have a lot of callbacks, in p...
# android-architecture
f
Hi guys, in case you have a lot of callbacks, in particular when you have also some components inside, so you have to pass those callbacks down, what is your favorite approach instead of passing all the callbacks? I've seen the possibility to pass a onAction function or use MVI, but I was wondering what is your favorite approach. Any suggestion is appreciated 🙌
m
Mmm, you can create a callbacks interface with the callbacks you need, and pasa it into the composable as an interface instance. Btw, i think you will consider using libraries like detekt o ktlint for the standard way to handle how clean your code in terms of maintenance and/or extensibility
f
Yes there is also that way, although it might trigger some additional recomposition. Regarding ktlint not sure how it could be helpful here 🤔
k
Sounds like a prop drilling problem, slot pattern could help if the callbacks are not directly consumed by the root screen but simply passed down for child components. See AndroidX Compose API guidelines for details
❤️ 2
j
hey! I'm a bit late here. For some callbacks that are globally used in the app you can try with a
CompositionLocalProvider
, I think this is a good use case for the snackbar callback for example. This way you wrap the app in the provider and you can call the
showSnackbar
anywhere without having to pass it down to every composable explicitly. Another option could be to have a shared viewmodel and handling there the snackbar with a state flow.
👀 1
👍 1
g
Just use one only function
handleEvent
aka the MVI pattern, and transform each function and its parameters into a type:
Copy code
sealed interface FeedUIEvent {
    data object OnNotificationsClicked,
    // etc.
}
And in the VM:
Copy code
fun handleEvent(event: FeedUIEvent) { .. }
Then your
@Composable
is looking better:
Copy code
FeedScreen(
   uiState = uiState,
   handleEvent: viewModel::handleEvent,
)
👀 1
K 2
🫶 2
👌 1
👍 1