Bet creation
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
b575f6e157
commit
269dfdd433
@ -0,0 +1,148 @@
|
|||||||
|
package fr.iut.alldev.allin.ui.core.snackbar
|
||||||
|
|
||||||
|
import androidx.compose.foundation.background
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.size
|
||||||
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.filled.Close
|
||||||
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.IconButton
|
||||||
|
import androidx.compose.material3.SnackbarHost
|
||||||
|
import androidx.compose.material3.SnackbarHostState
|
||||||
|
import androidx.compose.material3.Surface
|
||||||
|
import androidx.compose.material3.SwipeToDismissBox
|
||||||
|
import androidx.compose.material3.SwipeToDismissValue
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.material3.rememberSwipeToDismissState
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.graphics.vector.ImageVector
|
||||||
|
import androidx.compose.ui.text.style.TextOverflow
|
||||||
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
|
import androidx.compose.ui.tooling.preview.PreviewParameter
|
||||||
|
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import fr.iut.alldev.allin.theme.AllInTheme
|
||||||
|
|
||||||
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
|
@Composable
|
||||||
|
fun AllInSnackbar(
|
||||||
|
snackbarState: SnackbarHostState
|
||||||
|
) {
|
||||||
|
SnackbarHost(
|
||||||
|
hostState = snackbarState
|
||||||
|
) { snackbarData ->
|
||||||
|
val dismissState = rememberSwipeToDismissState(
|
||||||
|
confirmValueChange = { value ->
|
||||||
|
if (value != SwipeToDismissValue.Settled) {
|
||||||
|
snackbarState.currentSnackbarData?.dismiss()
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
SwipeToDismissBox(
|
||||||
|
state = dismissState,
|
||||||
|
backgroundContent = {},
|
||||||
|
modifier = Modifier.padding(8.dp)
|
||||||
|
) {
|
||||||
|
val snackbarType = remember {
|
||||||
|
if (snackbarData.visuals is AllInSnackbarVisualsImpl) {
|
||||||
|
(snackbarData.visuals as AllInSnackbarVisualsImpl).type
|
||||||
|
} else {
|
||||||
|
SnackbarType.STANDARD
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AllInSnackbarContent(
|
||||||
|
backgroundColor = snackbarType.getBackgroundColor(),
|
||||||
|
contentColor = AllInTheme.colors.white,
|
||||||
|
text = snackbarData.visuals.message,
|
||||||
|
icon = snackbarType.getIcon(),
|
||||||
|
dismiss = { snackbarState.currentSnackbarData?.dismiss() }
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun AllInSnackbarContent(
|
||||||
|
backgroundColor: Color,
|
||||||
|
contentColor: Color,
|
||||||
|
text: String,
|
||||||
|
icon: ImageVector,
|
||||||
|
dismiss: () -> Unit
|
||||||
|
) {
|
||||||
|
Surface(
|
||||||
|
shape = RoundedCornerShape(16.dp),
|
||||||
|
shadowElevation = 4.dp,
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.background(backgroundColor)
|
||||||
|
.padding(8.dp)
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
imageVector = icon,
|
||||||
|
contentDescription = null,
|
||||||
|
tint = contentColor,
|
||||||
|
modifier = Modifier.size(24.dp)
|
||||||
|
)
|
||||||
|
|
||||||
|
Text(
|
||||||
|
text = text,
|
||||||
|
color = contentColor,
|
||||||
|
style = AllInTheme.typography.r,
|
||||||
|
overflow = TextOverflow.Ellipsis,
|
||||||
|
maxLines = 5,
|
||||||
|
modifier = Modifier.weight(1f)
|
||||||
|
)
|
||||||
|
IconButton(
|
||||||
|
onClick = dismiss,
|
||||||
|
modifier = Modifier
|
||||||
|
.size(24.dp)
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
imageVector = Icons.Default.Close,
|
||||||
|
contentDescription = null,
|
||||||
|
tint = contentColor,
|
||||||
|
modifier = Modifier.size(24.dp)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class SnackbarTypePreviewProvider : PreviewParameterProvider<SnackbarType> {
|
||||||
|
override val values = SnackbarType.entries.asSequence()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Preview
|
||||||
|
@Composable
|
||||||
|
private fun AllInSnackbarContentPreview(
|
||||||
|
@PreviewParameter(SnackbarTypePreviewProvider::class) snackbarType: SnackbarType
|
||||||
|
) {
|
||||||
|
AllInTheme {
|
||||||
|
AllInSnackbarContent(
|
||||||
|
backgroundColor = snackbarType.getBackgroundColor(),
|
||||||
|
contentColor = AllInTheme.colors.white,
|
||||||
|
text = "Lorem Ipsum",
|
||||||
|
icon = snackbarType.getIcon(),
|
||||||
|
dismiss = {}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,36 @@
|
|||||||
|
package fr.iut.alldev.allin.ui.core.snackbar
|
||||||
|
|
||||||
|
import androidx.compose.material3.SnackbarDuration
|
||||||
|
import androidx.compose.material3.SnackbarVisuals
|
||||||
|
|
||||||
|
class AllInSnackbarVisualsImpl(
|
||||||
|
override val message: String,
|
||||||
|
override val actionLabel: String? = null,
|
||||||
|
override val withDismissAction: Boolean,
|
||||||
|
override val duration: SnackbarDuration,
|
||||||
|
val type: SnackbarType
|
||||||
|
) : SnackbarVisuals {
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (this === other) return true
|
||||||
|
if (other == null || this::class != other::class) return false
|
||||||
|
|
||||||
|
other as AllInSnackbarVisualsImpl
|
||||||
|
|
||||||
|
if (message != other.message) return false
|
||||||
|
if (actionLabel != other.actionLabel) return false
|
||||||
|
if (withDismissAction != other.withDismissAction) return false
|
||||||
|
if (duration != other.duration) return false
|
||||||
|
if (type != other.type) return false
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
var result = message.hashCode()
|
||||||
|
result = 31 * result + actionLabel.hashCode()
|
||||||
|
result = 31 * result + withDismissAction.hashCode()
|
||||||
|
result = 31 * result + duration.hashCode()
|
||||||
|
result = 31 * result + type.hashCode()
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package fr.iut.alldev.allin.ui.core.snackbar
|
||||||
|
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.filled.CheckCircle
|
||||||
|
import androidx.compose.material.icons.filled.Error
|
||||||
|
import androidx.compose.material.icons.filled.Info
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.graphics.vector.ImageVector
|
||||||
|
import fr.iut.alldev.allin.theme.AllInTheme
|
||||||
|
import fr.iut.alldev.allin.ui.core.snackbar.SnackbarType.ERROR
|
||||||
|
import fr.iut.alldev.allin.ui.core.snackbar.SnackbarType.STANDARD
|
||||||
|
import fr.iut.alldev.allin.ui.core.snackbar.SnackbarType.SUCCESS
|
||||||
|
|
||||||
|
enum class SnackbarType {
|
||||||
|
STANDARD,
|
||||||
|
SUCCESS,
|
||||||
|
ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun SnackbarType.getBackgroundColor(): Color =
|
||||||
|
when (this) {
|
||||||
|
STANDARD -> AllInTheme.colors.allInDark
|
||||||
|
SUCCESS -> AllInTheme.colors.allInPurple
|
||||||
|
ERROR -> AllInTheme.colors.allInBetWaiting
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun SnackbarType.getIcon(): ImageVector =
|
||||||
|
when (this) {
|
||||||
|
STANDARD -> Icons.Default.Info
|
||||||
|
ERROR -> Icons.Default.Error
|
||||||
|
SUCCESS -> Icons.Default.CheckCircle
|
||||||
|
}
|
Loading…
Reference in new issue