Room badge: let the presenter compute the list of badges.

This commit is contained in:
Benoit Marty 2024-10-22 11:15:46 +02:00
parent 5378c4efad
commit ecbf6b7e7c
5 changed files with 121 additions and 37 deletions

View file

@ -43,6 +43,7 @@ import io.element.android.libraries.matrix.ui.room.getDirectRoomMember
import io.element.android.libraries.matrix.ui.room.isOwnUserAdmin
import io.element.android.services.analytics.api.AnalyticsService
import io.element.android.services.analyticsproviders.api.trackers.captureInteraction
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.launchIn
@ -112,6 +113,21 @@ class RoomDetailsPresenter @Inject constructor(
val roomNotificationSettingsState by room.roomNotificationSettingsStateFlow.collectAsState()
val roomBadges by produceState(persistentListOf(), isPublic) {
value = buildList {
if (room.isEncrypted || isPublic) {
if (room.isEncrypted) {
add(RoomBadge.ENCRYPTED)
} else {
add(RoomBadge.NOT_ENCRYPTED)
}
}
if (isPublic) {
add(RoomBadge.PUBLIC)
}
}.toPersistentList()
}
fun handleEvents(event: RoomDetailsEvent) {
when (event) {
RoomDetailsEvent.LeaveRoom ->
@ -151,6 +167,7 @@ class RoomDetailsPresenter @Inject constructor(
isFavorite = isFavorite,
displayRolesAndPermissionsSettings = !room.isDm && isUserAdmin,
isPublic = isPublic,
roomBadges = roomBadges,
heroes = roomInfo?.heroes.orEmpty().toPersistentList(),
canShowPinnedMessages = canShowPinnedMessages,
pinnedMessagesCount = pinnedMessagesCount,

View file

@ -36,6 +36,7 @@ data class RoomDetailsState(
val isFavorite: Boolean,
val displayRolesAndPermissionsSettings: Boolean,
val isPublic: Boolean,
val roomBadges: ImmutableList<RoomBadge>,
val heroes: ImmutableList<MatrixUser>,
val canShowPinnedMessages: Boolean,
val pinnedMessagesCount: Int?,
@ -57,3 +58,9 @@ sealed interface RoomTopicState {
data object CanAddTopic : RoomTopicState
data class ExistingTopic(val topic: String) : RoomTopicState
}
enum class RoomBadge {
ENCRYPTED,
NOT_ENCRYPTED,
PUBLIC;
}

View file

@ -96,6 +96,18 @@ fun aRoomDetailsState(
isFavorite: Boolean = false,
displayAdminSettings: Boolean = false,
isPublic: Boolean = true,
roomBadges: List<RoomBadge> = buildList {
if (isEncrypted || isPublic) {
if (isEncrypted) {
add(RoomBadge.ENCRYPTED)
} else {
add(RoomBadge.NOT_ENCRYPTED)
}
}
if (isPublic) {
add(RoomBadge.PUBLIC)
}
},
heroes: List<MatrixUser> = emptyList(),
canShowPinnedMessages: Boolean = true,
pinnedMessagesCount: Int? = null,
@ -119,6 +131,7 @@ fun aRoomDetailsState(
isFavorite = isFavorite,
displayRolesAndPermissionsSettings = displayAdminSettings,
isPublic = isPublic,
roomBadges = roomBadges.toPersistentList(),
heroes = heroes.toPersistentList(),
canShowPinnedMessages = canShowPinnedMessages,
pinnedMessagesCount = pinnedMessagesCount,

View file

@ -147,8 +147,7 @@ fun RoomDetailsView(
}
}
BadgeList(
isEncrypted = state.isEncrypted,
isPublic = state.isPublic,
roomBadge = state.roomBadges,
modifier = Modifier.align(Alignment.CenterHorizontally),
)
Spacer(Modifier.height(32.dp))
@ -403,42 +402,42 @@ private fun ColumnScope.TitleAndSubtitle(
@Composable
private fun BadgeList(
isEncrypted: Boolean,
isPublic: Boolean,
roomBadge: ImmutableList<RoomBadge>,
modifier: Modifier = Modifier,
) {
if (isEncrypted || isPublic) {
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(),
)
if (roomBadge.isEmpty()) return
MatrixBadgeRowMolecule(
modifier = modifier,
data = roomBadge.map {
it.toMatrixBadgeData()
}.toImmutableList(),
)
}
@Composable
private fun RoomBadge.toMatrixBadgeData(): MatrixBadgeAtom.MatrixBadgeData {
return when (this) {
RoomBadge.ENCRYPTED -> {
MatrixBadgeAtom.MatrixBadgeData(
text = stringResource(R.string.screen_room_details_badge_encrypted),
icon = CompoundIcons.LockSolid(),
type = MatrixBadgeAtom.Type.Positive,
)
}
RoomBadge.NOT_ENCRYPTED -> {
MatrixBadgeAtom.MatrixBadgeData(
text = stringResource(R.string.screen_room_details_badge_not_encrypted),
icon = CompoundIcons.LockOff(),
type = MatrixBadgeAtom.Type.Neutral,
)
}
RoomBadge.PUBLIC -> {
MatrixBadgeAtom.MatrixBadgeData(
text = stringResource(R.string.screen_room_details_badge_public),
icon = CompoundIcons.Public(),
type = MatrixBadgeAtom.Type.Neutral,
)
}
}
}