Snackbar: ensure that 2 snackbar with the same message will be displayed in a row.
This commit is contained in:
parent
26e2172edd
commit
0dc87cdf84
2 changed files with 4 additions and 1 deletions
|
|
@ -72,7 +72,7 @@ fun rememberSnackbarHostState(snackbarMessage: SnackbarMessage?): SnackbarHostSt
|
||||||
} ?: return snackbarHostState
|
} ?: return snackbarHostState
|
||||||
|
|
||||||
val dispatcher = LocalSnackbarDispatcher.current
|
val dispatcher = LocalSnackbarDispatcher.current
|
||||||
LaunchedEffect(snackbarMessageText) {
|
LaunchedEffect(snackbarMessage.id) {
|
||||||
// If the message wasn't already displayed, do it now, and mark it as displayed
|
// 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
|
// This will prevent the message from appearing in any other active SnackbarHosts
|
||||||
if (snackbarMessage.isDisplayed.getAndSet(true) == false) {
|
if (snackbarMessage.isDisplayed.getAndSet(true) == false) {
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ package io.element.android.libraries.designsystem.utils.snackbar
|
||||||
import androidx.annotation.StringRes
|
import androidx.annotation.StringRes
|
||||||
import androidx.compose.material3.SnackbarDuration
|
import androidx.compose.material3.SnackbarDuration
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
|
import kotlin.random.Random
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A message to be displayed in a [Snackbar].
|
* 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 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 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 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.
|
* @param action The action to be performed when the action is clicked.
|
||||||
*/
|
*/
|
||||||
data class SnackbarMessage(
|
data class SnackbarMessage(
|
||||||
|
|
@ -24,5 +26,6 @@ data class SnackbarMessage(
|
||||||
val duration: SnackbarDuration = SnackbarDuration.Short,
|
val duration: SnackbarDuration = SnackbarDuration.Short,
|
||||||
@StringRes val actionResId: Int? = null,
|
@StringRes val actionResId: Int? = null,
|
||||||
val isDisplayed: AtomicBoolean = AtomicBoolean(false),
|
val isDisplayed: AtomicBoolean = AtomicBoolean(false),
|
||||||
|
val id: Long = Random.nextLong(),
|
||||||
val action: () -> Unit = {},
|
val action: () -> Unit = {},
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue