Handling Snackbar display in Compose Globally

79 views Asked by At

Hi I am using the below code to handle the snackbar display globally. I am new to compose and I went through various videos and blogs, and finally came up with below. Is this the right way of doing things?

I am handling the rendering of snackbar in MainActivity as shown below

class MainActivity : ComponentActivity() {
    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            SunnyTheme {
                val snackBarHostState = remember { SnackbarHostState() }
                val coroutineScope = rememberCoroutineScope()

                Scaffold(
                    snackbarHost = { SnackbarHost(snackBarHostState) },
                ) {
                    Box(modifier = Modifier.padding(it)) {

                        val showSnackBar: (
                            message: String?,
                            actionLabel: String,
                            actionPerformed: () -> Unit,
                            dismissed: () -> Unit
                        ) -> Unit = { message, actionLabel, actionPerformed, dismissed ->

                            coroutineScope.launch {
                                val result =
                                    message?.let {
                                        snackBarHostState.showSnackbar(
                                            message = it,
                                            actionLabel = actionLabel,
                                        )
                                    }
                                when (result) {
                                    ActionPerformed -> {
                                        actionPerformed.invoke()
                                    }
                                    Dismissed -> {
                                        dismissed.invoke()
                                    }
                                    else -> {
                                        Timber.e("WTf.......$#$%")
                                    }
                                }
                            }
                        }
                        SunnyApp(showSnackBar)
                    }
                }
            }
        }
    }


}

@Composable
fun SunnyApp(
    showSnackBar: (
        message: String?,
        actionLabel: String,
        actionPerformed: () -> Unit,
        dismissed: () -> Unit
    ) -> Unit,
) {
    val navController = rememberNavController()
    SetupNavGraph(navHostController = navController, showSnackBar = showSnackBar)
}

Below is my nav graph:

@Composable
fun SetupNavGraph(
    navHostController: NavHostController,
    showSnackBar: (
        message: String?,
        actionLabel: String,
        actionPerformed: () -> Unit,
        dismissed: () -> Unit
    ) -> Unit,
) {
    NavHost(
        navController = navHostController,
        startDestination = Screen.Splash.route,
    ) {
        composable(route = Screen.Splash.route) {
            SplashScreen {
                navHostController.popBackStack()
                navHostController.navigate(Screen.Home.route)
            }
        }

        composable(route = Screen.Home.route) {
            HomeScreen(
                showSnackBar = showSnackBar,
                navigateToSelectLocationScreen = { navHostController.navigate(Screen.SelectLocation.route) },
            )
        }

        composable(route = Screen.SelectLocation.route) {
            SelectLocationScreen()
        }
    }
}

And this is how I am calling it from my HomeScreen. in NetworkResult.Exception

@Composable
fun HomeScreen(
    homeViewModel: HomeViewModel = hiltViewModel(),
    showSnackBar: (
        message: String?,
        actionLabel: String,
        actionPerformed: () -> Unit,
        dismissed: () -> Unit
    ) -> Unit,
    navigateToSelectLocationScreen: () -> Unit,
) {
    val homeUiState by homeViewModel.uiState.collectAsStateWithLifecycle()
    val context = LocalContext.current

    if (homeUiState.lat == 0.0) {
        LoadingUi()

        LocationPermissionUI(
            context = context,
            permissionCallback = { permissionAction ->
                when (permissionAction) {
                    is PermissionAction.OnPermissionGranted -> {
                        Timber.e("Location permission granted")

                        getUserCurrentLocation(context, permissionAction.precise) {
                            Timber.e("Lat : ${it.result.latitude}")
                            Timber.e("Lng : ${it.result.longitude}")

                            homeViewModel.setLocation(it.result.latitude, it.result.longitude)
                        }
                    }

                    is PermissionAction.OnPermissionDenied -> {
                        Timber.e("Location permission denied")
                    }
                }
            },
        )
    } else {
        when (homeUiState.networkResult) {
            is NetworkResult.Loading -> {
                LoadingUi()
            }

            is NetworkResult.Success -> {
                
            }

            is NetworkResult.Error -> {
               
            }

            is NetworkResult.Exception -> {
                var exceptionMessage =
                    (homeUiState.networkResult as NetworkResult.Exception).throwable.localizedMessage
                Timber.e("Exception message: $exceptionMessage")
                showSnackBar.invoke(
                    exceptionMessage,
                    "Retry",
                    {
                        homeViewModel.setLocation(homeUiState.lat, homeUiState.lon)
                    },
                    {
                        //do nothing
                    }
                )

            }
        }
    }
}
1

There are 1 answers

0
zeet On

In my project I had a similar situation. The way I solved it was by creating a LocalSnackbarState value that I then passed down through my composition tree. I suggest reading through https://developer.android.com/jetpack/compose/compositionlocal