https://kotlinlang.org logo
#compose
Title
# compose
f

Florian Walther (live streaming)

12/21/2021, 12:11 PM
Do I need to read this result value in a side-effect or can I put it directly into my Composable? Or does
observeAsState
take care of that? https://stackoverflow.com/a/66837741
Is this syntax safe?
Copy code
val addEditRewardResult = navController.currentBackStackEntry
    ?.savedStateHandle?.getLiveData<String>(ADD_EDIT_REWARD_RESULT)?.observeAsState()

val scaffoldState = rememberScaffoldState()

val context = LocalContext.current

LaunchedEffect(key1 = addEditRewardResult) {
    addEditRewardResult?.value?.let { addEditRewardResult ->
        when (addEditRewardResult) {
            RESULT_REWARD_ADDED -> {
                scaffoldState.snackbarHostState.showSnackbar(context.getString(R.string.reward_added))
            }
            RESULT_REWARD_UPDATED -> {
                scaffoldState.snackbarHostState.showSnackbar(context.getString(R.string.reward_updated))
            }
        }
    }
}
👍 1
3 Views