Browse Source

Let roomBadges be a computed val of RoomDetailsState

pull/3718/head
Benoit Marty 7 days ago
parent
commit
35867f9837
  1. 17
      features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsPresenter.kt
  2. 17
      features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsState.kt
  3. 13
      features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsStateProvider.kt
  4. 57
      features/roomdetails/impl/src/test/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsPresenterTest.kt
  5. 56
      features/roomdetails/impl/src/test/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsStateTest.kt

17
features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsPresenter.kt

@ -43,7 +43,6 @@ import io.element.android.libraries.matrix.ui.room.getDirectRoomMember @@ -43,7 +43,6 @@ 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
@ -113,21 +112,6 @@ class RoomDetailsPresenter @Inject constructor( @@ -113,21 +112,6 @@ 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 ->
@ -167,7 +151,6 @@ class RoomDetailsPresenter @Inject constructor( @@ -167,7 +151,6 @@ class RoomDetailsPresenter @Inject constructor(
isFavorite = isFavorite,
displayRolesAndPermissionsSettings = !room.isDm && isUserAdmin,
isPublic = isPublic,
roomBadges = roomBadges,
heroes = roomInfo?.heroes.orEmpty().toPersistentList(),
canShowPinnedMessages = canShowPinnedMessages,
pinnedMessagesCount = pinnedMessagesCount,

17
features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsState.kt

@ -16,6 +16,7 @@ import io.element.android.libraries.matrix.api.room.RoomMember @@ -16,6 +16,7 @@ import io.element.android.libraries.matrix.api.room.RoomMember
import io.element.android.libraries.matrix.api.room.RoomNotificationSettings
import io.element.android.libraries.matrix.api.user.MatrixUser
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toPersistentList
data class RoomDetailsState(
val roomId: RoomId,
@ -36,12 +37,24 @@ data class RoomDetailsState( @@ -36,12 +37,24 @@ 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?,
val eventSink: (RoomDetailsEvent) -> Unit
)
) {
val roomBadges = buildList {
if (isEncrypted || isPublic) {
if (isEncrypted) {
add(RoomBadge.ENCRYPTED)
} else {
add(RoomBadge.NOT_ENCRYPTED)
}
}
if (isPublic) {
add(RoomBadge.PUBLIC)
}
}.toPersistentList()
}
@Immutable
sealed interface RoomDetailsType {

13
features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsStateProvider.kt

@ -96,18 +96,6 @@ fun aRoomDetailsState( @@ -96,18 +96,6 @@ 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,
@ -131,7 +119,6 @@ fun aRoomDetailsState( @@ -131,7 +119,6 @@ fun aRoomDetailsState(
isFavorite = isFavorite,
displayRolesAndPermissionsSettings = displayAdminSettings,
isPublic = isPublic,
roomBadges = roomBadges.toPersistentList(),
heroes = heroes.toPersistentList(),
canShowPinnedMessages = canShowPinnedMessages,
pinnedMessagesCount = pinnedMessagesCount,

57
features/roomdetails/impl/src/test/kotlin/io/element/android/features/roomdetails/RoomDetailsPresenterTest.kt → features/roomdetails/impl/src/test/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsPresenterTest.kt

@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
* Please see LICENSE in the repository root for full details.
*/
package io.element.android.features.roomdetails
package io.element.android.features.roomdetails.impl
import androidx.lifecycle.Lifecycle
import app.cash.molecule.RecompositionMode
@ -17,12 +17,7 @@ import im.vector.app.features.analytics.plan.Interaction @@ -17,12 +17,7 @@ import im.vector.app.features.analytics.plan.Interaction
import io.element.android.features.leaveroom.api.LeaveRoomEvent
import io.element.android.features.leaveroom.api.LeaveRoomState
import io.element.android.features.leaveroom.api.aLeaveRoomState
import io.element.android.features.roomdetails.impl.RoomBadge
import io.element.android.features.roomdetails.impl.RoomDetailsEvent
import io.element.android.features.roomdetails.impl.RoomDetailsPresenter
import io.element.android.features.roomdetails.impl.RoomDetailsState
import io.element.android.features.roomdetails.impl.RoomDetailsType
import io.element.android.features.roomdetails.impl.RoomTopicState
import io.element.android.features.roomdetails.aMatrixRoom
import io.element.android.features.roomdetails.impl.members.aRoomMember
import io.element.android.features.roomdetails.impl.members.details.RoomMemberDetailsPresenter
import io.element.android.features.userprofile.shared.aUserProfileState
@ -126,6 +121,7 @@ class RoomDetailsPresenterTest { @@ -126,6 +121,7 @@ class RoomDetailsPresenterTest {
)
val presenter = createRoomDetailsPresenter(room)
presenter.test {
skipItems(1)
val initialState = awaitItem()
assertThat(initialState.roomId).isEqualTo(room.roomId)
assertThat(initialState.roomName).isEqualTo(room.displayName)
@ -135,8 +131,6 @@ class RoomDetailsPresenterTest { @@ -135,8 +131,6 @@ class RoomDetailsPresenterTest {
assertThat(initialState.isEncrypted).isEqualTo(room.isEncrypted)
assertThat(initialState.canShowPinnedMessages).isTrue()
assertThat(initialState.pinnedMessagesCount).isNull()
assertThat(initialState.roomBadges).isEmpty()
assertThat(awaitItem().roomBadges).isEqualTo(listOf(RoomBadge.ENCRYPTED))
}
}
@ -164,55 +158,10 @@ class RoomDetailsPresenterTest { @@ -164,55 +158,10 @@ class RoomDetailsPresenterTest {
assertThat(updatedState.roomAvatarUrl).isEqualTo(roomInfo.avatarUrl)
assertThat(updatedState.roomTopic).isEqualTo(RoomTopicState.ExistingTopic(roomInfo.topic!!))
assertThat(updatedState.pinnedMessagesCount).isEqualTo(roomInfo.pinnedEventIds.size)
assertThat(updatedState.roomBadges).isEqualTo(listOf(RoomBadge.ENCRYPTED, RoomBadge.PUBLIC))
cancelAndIgnoreRemainingEvents()
}
}
@Test
fun `present - initial state not public not encrypted should have no badges`() = runTest {
val roomInfo = aRoomInfo(
name = A_ROOM_NAME,
isPublic = false,
)
val room = aMatrixRoom(
isEncrypted = false,
canInviteResult = { Result.success(true) },
canUserJoinCallResult = { Result.success(true) },
canSendStateResult = { _, _ -> Result.success(true) },
).apply {
givenRoomInfo(roomInfo)
}
val presenter = createRoomDetailsPresenter(room)
presenter.test {
skipItems(1)
val updatedState = awaitItem()
assertThat(updatedState.roomBadges).isEmpty()
}
}
@Test
fun `present - initial state public not encrypted should have not encrypted and public badges`() = runTest {
val roomInfo = aRoomInfo(
name = A_ROOM_NAME,
isPublic = true,
)
val room = aMatrixRoom(
isEncrypted = false,
canInviteResult = { Result.success(true) },
canUserJoinCallResult = { Result.success(true) },
canSendStateResult = { _, _ -> Result.success(true) },
).apply {
givenRoomInfo(roomInfo)
}
val presenter = createRoomDetailsPresenter(room)
presenter.test {
skipItems(1)
val updatedState = awaitItem()
assertThat(updatedState.roomBadges).isEqualTo(listOf(RoomBadge.NOT_ENCRYPTED, RoomBadge.PUBLIC))
}
}
@Test
fun `present - initial state with no room name`() = runTest {
val room = aMatrixRoom(

56
features/roomdetails/impl/src/test/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsStateTest.kt

@ -0,0 +1,56 @@ @@ -0,0 +1,56 @@
/*
* 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.features.roomdetails.impl
import com.google.common.truth.Truth.assertThat
import kotlinx.collections.immutable.persistentListOf
import org.junit.Test
class RoomDetailsStateTest {
@Test
fun `room not public not encrypted should have no badges`() {
val sut = aRoomDetailsState(
isPublic = false,
isEncrypted = false,
)
assertThat(sut.roomBadges).isEmpty()
}
@Test
fun `room public not encrypted should have not encrypted and public badges`() {
val sut = aRoomDetailsState(
isPublic = true,
isEncrypted = false,
)
assertThat(sut.roomBadges).isEqualTo(
persistentListOf(RoomBadge.NOT_ENCRYPTED, RoomBadge.PUBLIC)
)
}
@Test
fun `room public encrypted should have encrypted and public badges`() {
val sut = aRoomDetailsState(
isPublic = true,
isEncrypted = true,
)
assertThat(sut.roomBadges).isEqualTo(
persistentListOf(RoomBadge.ENCRYPTED, RoomBadge.PUBLIC)
)
}
@Test
fun `room not public encrypted should have encrypted badges`() {
val sut = aRoomDetailsState(
isPublic = false,
isEncrypted = true,
)
assertThat(sut.roomBadges).isEqualTo(
persistentListOf(RoomBadge.ENCRYPTED)
)
}
}
Loading…
Cancel
Save