Snackbar: ensure that 2 snackbar with the same message will be displayed in a row.

This commit is contained in:
Benoit Marty 2025-01-24 14:39:01 +01:00
parent 3668e861f7
commit d909bb0c3d
2 changed files with 4 additions and 1 deletions

View file

@ -72,7 +72,7 @@ fun rememberSnackbarHostState(snackbarMessage: SnackbarMessage?): SnackbarHostSt
} ?: return snackbarHostState
val dispatcher = LocalSnackbarDispatcher.current
LaunchedEffect(snackbarMessageText) {
LaunchedEffect(snackbarMessage.id) {
// If the message wasn't already displayed, do it now, and mark it as displayed
// This will prevent the message from appearing in any other active SnackbarHosts
if (snackbarMessage.isDisplayed.getAndSet(true) == false) {

View file

@ -10,6 +10,7 @@ package io.element.android.libraries.designsystem.utils.snackbar
import androidx.annotation.StringRes
import androidx.compose.material3.SnackbarDuration
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.random.Random
/**
* A message to be displayed in a [Snackbar].
@ -17,6 +18,7 @@ import java.util.concurrent.atomic.AtomicBoolean
* @param duration The duration of the message. The default value is [SnackbarDuration.Short].
* @param actionResId The action text to be displayed. The default value is `null`.
* @param isDisplayed Used to track if the current message is already displayed or not.
* @param id The unique identifier of the message. The default value is a random long.
* @param action The action to be performed when the action is clicked.
*/
data class SnackbarMessage(
@ -24,5 +26,6 @@ data class SnackbarMessage(
val duration: SnackbarDuration = SnackbarDuration.Short,
@StringRes val actionResId: Int? = null,
val isDisplayed: AtomicBoolean = AtomicBoolean(false),
val id: Long = Random.nextLong(),
val action: () -> Unit = {},
)