Change model and create MatrixBadgeRowMolecule

This commit is contained in:
Benoit Marty 2024-10-16 18:30:16 +02:00 committed by Benoit Marty
parent 86a839ea73
commit 75caeaaec2
3 changed files with 104 additions and 54 deletions

View file

@ -45,6 +45,7 @@ import io.element.android.features.userprofile.shared.blockuser.BlockUserDialogs
import io.element.android.features.userprofile.shared.blockuser.BlockUserSection import io.element.android.features.userprofile.shared.blockuser.BlockUserSection
import io.element.android.libraries.architecture.coverage.ExcludeFromCoverage import io.element.android.libraries.architecture.coverage.ExcludeFromCoverage
import io.element.android.libraries.designsystem.atomic.atoms.MatrixBadgeAtom import io.element.android.libraries.designsystem.atomic.atoms.MatrixBadgeAtom
import io.element.android.libraries.designsystem.atomic.molecules.MatrixBadgeRowMolecule
import io.element.android.libraries.designsystem.components.ClickableLinkText import io.element.android.libraries.designsystem.components.ClickableLinkText
import io.element.android.libraries.designsystem.components.avatar.AvatarData import io.element.android.libraries.designsystem.components.avatar.AvatarData
import io.element.android.libraries.designsystem.components.avatar.AvatarSize import io.element.android.libraries.designsystem.components.avatar.AvatarSize
@ -84,6 +85,7 @@ import io.element.android.libraries.ui.strings.CommonStrings
import io.element.android.services.analytics.compose.LocalAnalyticsService import io.element.android.services.analytics.compose.LocalAnalyticsService
import io.element.android.services.analyticsproviders.api.trackers.captureInteraction import io.element.android.services.analyticsproviders.api.trackers.captureInteraction
import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentList
@Composable @Composable
@ -406,32 +408,37 @@ private fun BadgeList(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
) { ) {
if (isEncrypted || isPublic) { if (isEncrypted || isPublic) {
Row( MatrixBadgeRowMolecule(
modifier = modifier modifier = modifier,
.padding(start = 16.dp, end = 16.dp, top = 8.dp), data = buildList {
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
if (isEncrypted) { if (isEncrypted) {
MatrixBadgeAtom.View( add(
MatrixBadgeAtom.MatrixBadgeData(
text = stringResource(R.string.screen_room_details_badge_encrypted), text = stringResource(R.string.screen_room_details_badge_encrypted),
icon = CompoundIcons.LockSolid(), icon = CompoundIcons.LockSolid(),
type = MatrixBadgeAtom.Type.Positive, type = MatrixBadgeAtom.Type.Positive,
) )
)
} else { } else {
MatrixBadgeAtom.View( add(
MatrixBadgeAtom.MatrixBadgeData(
text = stringResource(R.string.screen_room_details_badge_not_encrypted), text = stringResource(R.string.screen_room_details_badge_not_encrypted),
icon = CompoundIcons.LockOff(), icon = CompoundIcons.LockOff(),
type = MatrixBadgeAtom.Type.Neutral, type = MatrixBadgeAtom.Type.Neutral,
) )
)
} }
if (isPublic) { if (isPublic) {
MatrixBadgeAtom.View( add(
MatrixBadgeAtom.MatrixBadgeData(
text = stringResource(R.string.screen_room_details_badge_public), text = stringResource(R.string.screen_room_details_badge_public),
icon = CompoundIcons.Public(), icon = CompoundIcons.Public(),
type = MatrixBadgeAtom.Type.Neutral, type = MatrixBadgeAtom.Type.Neutral,
) )
)
} }
} }.toImmutableList(),
)
} }
} }

View file

@ -22,6 +22,12 @@ import io.element.android.libraries.designsystem.theme.badgePositiveBackgroundCo
import io.element.android.libraries.designsystem.theme.badgePositiveContentColor import io.element.android.libraries.designsystem.theme.badgePositiveContentColor
object MatrixBadgeAtom { object MatrixBadgeAtom {
data class MatrixBadgeData(
val text: String,
val icon: ImageVector,
val type: Type,
)
enum class Type { enum class Type {
Positive, Positive,
Neutral, Neutral,
@ -30,28 +36,26 @@ object MatrixBadgeAtom {
@Composable @Composable
fun View( fun View(
text: String, data: MatrixBadgeData,
icon: ImageVector,
type: Type,
) { ) {
val backgroundColor = when (type) { val backgroundColor = when (data.type) {
Type.Positive -> ElementTheme.colors.badgePositiveBackgroundColor Type.Positive -> ElementTheme.colors.badgePositiveBackgroundColor
Type.Neutral -> ElementTheme.colors.badgeNeutralBackgroundColor Type.Neutral -> ElementTheme.colors.badgeNeutralBackgroundColor
Type.Negative -> ElementTheme.colors.badgeNegativeBackgroundColor Type.Negative -> ElementTheme.colors.badgeNegativeBackgroundColor
} }
val textColor = when (type) { val textColor = when (data.type) {
Type.Positive -> ElementTheme.colors.badgePositiveContentColor Type.Positive -> ElementTheme.colors.badgePositiveContentColor
Type.Neutral -> ElementTheme.colors.badgeNeutralContentColor Type.Neutral -> ElementTheme.colors.badgeNeutralContentColor
Type.Negative -> ElementTheme.colors.badgeNegativeContentColor Type.Negative -> ElementTheme.colors.badgeNegativeContentColor
} }
val iconColor = when (type) { val iconColor = when (data.type) {
Type.Positive -> ElementTheme.colors.iconSuccessPrimary Type.Positive -> ElementTheme.colors.iconSuccessPrimary
Type.Neutral -> ElementTheme.colors.iconSecondary Type.Neutral -> ElementTheme.colors.iconSecondary
Type.Negative -> ElementTheme.colors.iconCriticalPrimary Type.Negative -> ElementTheme.colors.iconCriticalPrimary
} }
Badge( Badge(
text = text, text = data.text,
icon = icon, icon = data.icon,
backgroundColor = backgroundColor, backgroundColor = backgroundColor,
iconColor = iconColor, iconColor = iconColor,
textColor = textColor, textColor = textColor,
@ -63,28 +67,34 @@ object MatrixBadgeAtom {
@Composable @Composable
internal fun MatrixBadgeAtomPositivePreview() = ElementPreview { internal fun MatrixBadgeAtomPositivePreview() = ElementPreview {
MatrixBadgeAtom.View( MatrixBadgeAtom.View(
MatrixBadgeAtom.MatrixBadgeData(
text = "Trusted", text = "Trusted",
icon = CompoundIcons.Verified(), icon = CompoundIcons.Verified(),
type = MatrixBadgeAtom.Type.Positive, type = MatrixBadgeAtom.Type.Positive,
) )
)
} }
@PreviewsDayNight @PreviewsDayNight
@Composable @Composable
internal fun MatrixBadgeAtomNeutralPreview() = ElementPreview { internal fun MatrixBadgeAtomNeutralPreview() = ElementPreview {
MatrixBadgeAtom.View( MatrixBadgeAtom.View(
MatrixBadgeAtom.MatrixBadgeData(
text = "Public room", text = "Public room",
icon = CompoundIcons.Public(), icon = CompoundIcons.Public(),
type = MatrixBadgeAtom.Type.Neutral, type = MatrixBadgeAtom.Type.Neutral,
) )
)
} }
@PreviewsDayNight @PreviewsDayNight
@Composable @Composable
internal fun MatrixBadgeAtomNegativePreview() = ElementPreview { internal fun MatrixBadgeAtomNegativePreview() = ElementPreview {
MatrixBadgeAtom.View( MatrixBadgeAtom.View(
MatrixBadgeAtom.MatrixBadgeData(
text = "Not trusted", text = "Not trusted",
icon = CompoundIcons.Error(), icon = CompoundIcons.Error(),
type = MatrixBadgeAtom.Type.Negative, type = MatrixBadgeAtom.Type.Negative,
) )
)
} }

View file

@ -0,0 +1,33 @@
/*
* Copyright 2024 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only
* Please see LICENSE in the repository root for full details.
*/
package io.element.android.libraries.designsystem.atomic.molecules
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import io.element.android.libraries.designsystem.atomic.atoms.MatrixBadgeAtom
import kotlinx.collections.immutable.ImmutableList
@Composable
fun MatrixBadgeRowMolecule(
data: ImmutableList<MatrixBadgeAtom.MatrixBadgeData>,
modifier: Modifier = Modifier,
) {
Row(
modifier = modifier
.padding(start = 16.dp, end = 16.dp, top = 8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
for (badge in data) {
MatrixBadgeAtom.View(badge)
}
}
}