From 75caeaaec29e5691e74f6327c302f0906d5ee888 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 16 Oct 2024 18:30:16 +0200 Subject: [PATCH] Change model and create MatrixBadgeRowMolecule --- .../roomdetails/impl/RoomDetailsView.kt | 81 ++++++++++--------- .../atomic/atoms/MatrixBadgeAtom.kt | 44 ++++++---- .../molecules/MatrixBadgeRowMolecule.kt | 33 ++++++++ 3 files changed, 104 insertions(+), 54 deletions(-) create mode 100644 libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/atomic/molecules/MatrixBadgeRowMolecule.kt diff --git a/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsView.kt b/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsView.kt index 6af32facf4..8291b3e1c8 100644 --- a/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsView.kt +++ b/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsView.kt @@ -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.libraries.architecture.coverage.ExcludeFromCoverage 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.avatar.AvatarData 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.analyticsproviders.api.trackers.captureInteraction import kotlinx.collections.immutable.ImmutableList +import kotlinx.collections.immutable.toImmutableList import kotlinx.collections.immutable.toPersistentList @Composable @@ -114,9 +116,9 @@ fun RoomDetailsView( ) { padding -> Column( modifier = Modifier - .padding(padding) - .verticalScroll(rememberScrollState()) - .consumeWindowInsets(padding) + .padding(padding) + .verticalScroll(rememberScrollState()) + .consumeWindowInsets(padding) ) { LeaveRoomView(state = state.leaveRoomState) @@ -273,8 +275,8 @@ private fun MainActionsSection( ) { Row( modifier = Modifier - .fillMaxWidth() - .padding(horizontal = 16.dp), + .fillMaxWidth() + .padding(horizontal = 16.dp), horizontalArrangement = Arrangement.SpaceEvenly, ) { val roomNotificationSettings = state.roomNotificationSettings @@ -333,8 +335,8 @@ private fun RoomHeaderSection( ) { Column( modifier = Modifier - .fillMaxWidth() - .padding(horizontal = 16.dp), + .fillMaxWidth() + .padding(horizontal = 16.dp), horizontalAlignment = Alignment.CenterHorizontally, ) { CompositeAvatar( @@ -343,8 +345,8 @@ private fun RoomHeaderSection( user.getAvatarData(size = AvatarSize.RoomHeader) }.toPersistentList(), modifier = Modifier - .clickable(enabled = avatarUrl != null) { openAvatarPreview(avatarUrl!!) } - .testTag(TestTags.roomDetailAvatar) + .clickable(enabled = avatarUrl != null) { openAvatarPreview(avatarUrl!!) } + .testTag(TestTags.roomDetailAvatar) ) TitleAndSubtitle(title = roomName, subtitle = roomAlias?.value) } @@ -360,8 +362,8 @@ private fun DmHeaderSection( ) { Column( modifier = modifier - .fillMaxWidth() - .padding(horizontal = 16.dp), + .fillMaxWidth() + .padding(horizontal = 16.dp), horizontalAlignment = Alignment.CenterHorizontally, ) { DmAvatars( @@ -406,32 +408,37 @@ private fun BadgeList( modifier: Modifier = Modifier, ) { if (isEncrypted || isPublic) { - Row( - modifier = modifier - .padding(start = 16.dp, end = 16.dp, top = 8.dp), - horizontalArrangement = Arrangement.spacedBy(8.dp), - ) { - if (isEncrypted) { - MatrixBadgeAtom.View( - text = stringResource(R.string.screen_room_details_badge_encrypted), - icon = CompoundIcons.LockSolid(), - type = MatrixBadgeAtom.Type.Positive, - ) - } else { - MatrixBadgeAtom.View( - text = stringResource(R.string.screen_room_details_badge_not_encrypted), - icon = CompoundIcons.LockOff(), - type = MatrixBadgeAtom.Type.Neutral, - ) - } - if (isPublic) { - MatrixBadgeAtom.View( - text = stringResource(R.string.screen_room_details_badge_public), - icon = CompoundIcons.Public(), - type = MatrixBadgeAtom.Type.Neutral, - ) - } - } + MatrixBadgeRowMolecule( + modifier = modifier, + data = buildList { + if (isEncrypted) { + add( + MatrixBadgeAtom.MatrixBadgeData( + text = stringResource(R.string.screen_room_details_badge_encrypted), + icon = CompoundIcons.LockSolid(), + type = MatrixBadgeAtom.Type.Positive, + ) + ) + } else { + add( + MatrixBadgeAtom.MatrixBadgeData( + text = stringResource(R.string.screen_room_details_badge_not_encrypted), + icon = CompoundIcons.LockOff(), + type = MatrixBadgeAtom.Type.Neutral, + ) + ) + } + if (isPublic) { + add( + MatrixBadgeAtom.MatrixBadgeData( + text = stringResource(R.string.screen_room_details_badge_public), + icon = CompoundIcons.Public(), + type = MatrixBadgeAtom.Type.Neutral, + ) + ) + } + }.toImmutableList(), + ) } } diff --git a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/atomic/atoms/MatrixBadgeAtom.kt b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/atomic/atoms/MatrixBadgeAtom.kt index 2e2b4b07fb..a4b438527f 100644 --- a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/atomic/atoms/MatrixBadgeAtom.kt +++ b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/atomic/atoms/MatrixBadgeAtom.kt @@ -22,6 +22,12 @@ import io.element.android.libraries.designsystem.theme.badgePositiveBackgroundCo import io.element.android.libraries.designsystem.theme.badgePositiveContentColor object MatrixBadgeAtom { + data class MatrixBadgeData( + val text: String, + val icon: ImageVector, + val type: Type, + ) + enum class Type { Positive, Neutral, @@ -30,28 +36,26 @@ object MatrixBadgeAtom { @Composable fun View( - text: String, - icon: ImageVector, - type: Type, + data: MatrixBadgeData, ) { - val backgroundColor = when (type) { + val backgroundColor = when (data.type) { Type.Positive -> ElementTheme.colors.badgePositiveBackgroundColor Type.Neutral -> ElementTheme.colors.badgeNeutralBackgroundColor Type.Negative -> ElementTheme.colors.badgeNegativeBackgroundColor } - val textColor = when (type) { + val textColor = when (data.type) { Type.Positive -> ElementTheme.colors.badgePositiveContentColor Type.Neutral -> ElementTheme.colors.badgeNeutralContentColor Type.Negative -> ElementTheme.colors.badgeNegativeContentColor } - val iconColor = when (type) { + val iconColor = when (data.type) { Type.Positive -> ElementTheme.colors.iconSuccessPrimary Type.Neutral -> ElementTheme.colors.iconSecondary Type.Negative -> ElementTheme.colors.iconCriticalPrimary } Badge( - text = text, - icon = icon, + text = data.text, + icon = data.icon, backgroundColor = backgroundColor, iconColor = iconColor, textColor = textColor, @@ -63,9 +67,11 @@ object MatrixBadgeAtom { @Composable internal fun MatrixBadgeAtomPositivePreview() = ElementPreview { MatrixBadgeAtom.View( - text = "Trusted", - icon = CompoundIcons.Verified(), - type = MatrixBadgeAtom.Type.Positive, + MatrixBadgeAtom.MatrixBadgeData( + text = "Trusted", + icon = CompoundIcons.Verified(), + type = MatrixBadgeAtom.Type.Positive, + ) ) } @@ -73,9 +79,11 @@ internal fun MatrixBadgeAtomPositivePreview() = ElementPreview { @Composable internal fun MatrixBadgeAtomNeutralPreview() = ElementPreview { MatrixBadgeAtom.View( - text = "Public room", - icon = CompoundIcons.Public(), - type = MatrixBadgeAtom.Type.Neutral, + MatrixBadgeAtom.MatrixBadgeData( + text = "Public room", + icon = CompoundIcons.Public(), + type = MatrixBadgeAtom.Type.Neutral, + ) ) } @@ -83,8 +91,10 @@ internal fun MatrixBadgeAtomNeutralPreview() = ElementPreview { @Composable internal fun MatrixBadgeAtomNegativePreview() = ElementPreview { MatrixBadgeAtom.View( - text = "Not trusted", - icon = CompoundIcons.Error(), - type = MatrixBadgeAtom.Type.Negative, + MatrixBadgeAtom.MatrixBadgeData( + text = "Not trusted", + icon = CompoundIcons.Error(), + type = MatrixBadgeAtom.Type.Negative, + ) ) } diff --git a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/atomic/molecules/MatrixBadgeRowMolecule.kt b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/atomic/molecules/MatrixBadgeRowMolecule.kt new file mode 100644 index 0000000000..f7bb79cf17 --- /dev/null +++ b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/atomic/molecules/MatrixBadgeRowMolecule.kt @@ -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, + 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) + } + } +}