From abd17b3608519f1ab5879bea9a18ce5342437335 Mon Sep 17 00:00:00 2001 From: Pauline Auvray Date: Wed, 23 Nov 2022 17:07:38 +0100 Subject: [PATCH] [#114] Review: Add parameter to set onActionClick --- .../components/snackbars/ComponentSnackbars.kt | 8 +++++++- .../compose/component/snackbar/OdsSnackbar.kt | 16 +++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/demo/src/main/java/com/orange/ods/demo/ui/components/snackbars/ComponentSnackbars.kt b/demo/src/main/java/com/orange/ods/demo/ui/components/snackbars/ComponentSnackbars.kt index 27b1e6500..8fea2bf30 100644 --- a/demo/src/main/java/com/orange/ods/demo/ui/components/snackbars/ComponentSnackbars.kt +++ b/demo/src/main/java/com/orange/ods/demo/ui/components/snackbars/ComponentSnackbars.kt @@ -16,12 +16,14 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import com.orange.ods.compose.component.snackbar.OdsSnackbar import com.orange.ods.compose.component.snackbar.OdsSnackbarHost import com.orange.ods.demo.R import com.orange.ods.demo.ui.components.utilities.ComponentCustomizationBottomSheetScaffold import com.orange.ods.demo.ui.components.utilities.ComponentLaunchContentColumn +import com.orange.ods.demo.ui.components.utilities.clickOnElement import com.orange.ods.demo.ui.utilities.composable.SwitchListItem import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch @@ -29,6 +31,7 @@ import kotlinx.coroutines.launch @OptIn(ExperimentalMaterialApi::class) @Composable fun ComponentSnackbars() { + val context = LocalContext.current val bottomSheetScaffoldState = rememberBottomSheetScaffoldState() val coroutineScope: CoroutineScope = rememberCoroutineScope() @@ -40,12 +43,15 @@ fun ComponentSnackbars() { val snackbarMessage = stringResource(id = R.string.component_snackbar_message) val snackbarActionLabel = stringResource(id = R.string.component_snackbar_action_label) + val snackbarActionButton = stringResource(id = R.string.component_snackbar_action_button) ComponentCustomizationBottomSheetScaffold( bottomSheetScaffoldState = bottomSheetScaffoldState, snackbarHost = { OdsSnackbarHost(hostState = it) { data -> - OdsSnackbar(snackbarData = data, actionOnNewLine = actionOnNewLineChecked.value) + OdsSnackbar(snackbarData = data, actionOnNewLine = actionOnNewLineChecked.value, onActionClick = { + clickOnElement(context = context, clickedElement = snackbarActionButton) + }) } }, bottomSheetContent = { diff --git a/lib/src/main/java/com/orange/ods/compose/component/snackbar/OdsSnackbar.kt b/lib/src/main/java/com/orange/ods/compose/component/snackbar/OdsSnackbar.kt index a4c1187ef..de2e7adbb 100644 --- a/lib/src/main/java/com/orange/ods/compose/component/snackbar/OdsSnackbar.kt +++ b/lib/src/main/java/com/orange/ods/compose/component/snackbar/OdsSnackbar.kt @@ -42,9 +42,10 @@ import com.orange.ods.compose.theme.OdsTheme * * @param message text displayed in the snackbar * @param modifier modifiers for the Snackbar layout - * @param actionLabel If set, it displays an [OdsTextButton] with the given [actionLabel] as an action of the snackbar. + * @param actionLabel if set, it displays an [OdsTextButton] with the given [actionLabel] as an action of the snackbar. * @param actionOnNewLine whether or not action should be put on the separate line. Recommended * for action with long action text + * @param onActionClick executed on action button click. */ @Composable @OdsComponentApi @@ -52,7 +53,8 @@ fun OdsSnackbar( message: String, modifier: Modifier = Modifier, actionLabel: String? = null, - actionOnNewLine: Boolean = false + actionOnNewLine: Boolean = false, + onActionClick: () -> Unit = {} ) { Snackbar( modifier = modifier, @@ -62,7 +64,8 @@ fun OdsSnackbar( style = OdsTextButtonStyle.Primary, displaySurface = OdsSnackbarDefaults.actionButtonDisplaySurface, text = it, - onClick = {}) + onClick = onActionClick + ) } }, actionOnNewLine = actionOnNewLine, @@ -80,18 +83,21 @@ fun OdsSnackbar( * @param modifier modifiers for the Snackbar layout * @param actionOnNewLine whether or not action should be put on the separate line. Recommended * for action with long action text + * @param onActionClick executed on action button click. */ @Composable fun OdsSnackbar( snackbarData: SnackbarData, modifier: Modifier = Modifier, - actionOnNewLine: Boolean = false + actionOnNewLine: Boolean = false, + onActionClick: () -> Unit = {} ) { OdsSnackbar( modifier = modifier, message = snackbarData.message, actionLabel = snackbarData.actionLabel, - actionOnNewLine = actionOnNewLine + actionOnNewLine = actionOnNewLine, + onActionClick = onActionClick ) }