Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import androidx.lifecycle.viewModelScope
import com.wire.android.BuildConfig
import com.wire.android.navigation.HomeDestination
import com.wire.android.util.EMPTY
import com.wire.kalium.logic.data.conversation.ConversationDetails
import com.wire.kalium.logic.data.user.type.isTeamAdmin
import com.wire.kalium.logic.feature.client.IsWireCellsEnabledUseCase
import com.wire.kalium.logic.feature.conversation.ObserveArchivedUnreadConversationsCountUseCase
import com.wire.kalium.logic.feature.conversation.ObserveConversationListDetailsUseCase
import com.wire.kalium.logic.feature.server.GetTeamUrlUseCase
import com.wire.kalium.logic.feature.user.ObserveSelfUserUseCase
import dagger.Lazy
Expand All @@ -49,6 +51,7 @@ class HomeDrawerViewModel @Inject constructor(
private val observeSelfUser: ObserveSelfUserUseCase,
private val getTeamUrl: GetTeamUrlUseCase,
private val isWireCellsEnabled: IsWireCellsEnabledUseCase,
private val observeConversationListDetails: Lazy<ObserveConversationListDetailsUseCase>,
) : ViewModel() {

var drawerState by mutableStateOf(HomeDrawerState())
Expand All @@ -73,11 +76,17 @@ class HomeDrawerViewModel @Inject constructor(
combine(
flowOf(isWireCellsEnabled()),
observeArchivedUnreadConversationsCount.get().invoke(),
observeTeamManagementUrlForUser()
) { wireCellsEnabled, unreadArchiveConversationsCount, teamManagementUrl ->
observeTeamManagementUrlForUser(),
observeConversationListDetails.get().invoke(fromArchive = false)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

observeConversationListDetails is

  1. A complex query (I think we should avoid calling it unless absolutely necessary)
  2. Will change a lot (which will cause constant drawer items list rebuilds for no reason)

I suggest:

  • use existing hasConversationWithCell() query (make the observable version if needed).
  • create a use case for observing it which returns Flow with distinctUntilChanged() to avoid unnecessary updates and keep all business logic in kalium

) { wireCellsEnabled, unreadArchiveConversationsCount, teamManagementUrl, conversations ->
val hasConversationWithCells = conversations.any { conversation ->
(conversation as? ConversationDetails.Group)?.wireCell != null
}
val shouldShowCells = wireCellsEnabled || hasConversationWithCells

buildList {
add(DrawerUiItem.RegularItem(destination = HomeDestination.Conversations))
if (wireCellsEnabled) {
if (shouldShowCells) {
add(DrawerUiItem.RegularItem(destination = HomeDestination.Cells))
}
if (BuildConfig.MEETINGS_ENABLED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ package com.wire.android.ui.home.drawer
import androidx.lifecycle.SavedStateHandle
import com.wire.android.config.CoroutineTestExtension
import com.wire.android.config.NavigationTestExtension
import com.wire.android.framework.TestConversation
import com.wire.android.framework.TestConversationDetails
import com.wire.android.framework.TestUser
import com.wire.kalium.logic.data.conversation.ConversationDetails
import com.wire.kalium.logic.data.user.type.UserType
import com.wire.kalium.logic.data.user.type.UserTypeInfo
import com.wire.kalium.logic.feature.client.IsWireCellsEnabledUseCase
import com.wire.kalium.logic.feature.conversation.ObserveArchivedUnreadConversationsCountUseCase
import com.wire.kalium.logic.feature.conversation.ObserveConversationListDetailsUseCase
import com.wire.kalium.logic.feature.server.GetTeamUrlUseCase
import com.wire.kalium.logic.feature.user.ObserveSelfUserUseCase
import io.mockk.MockKAnnotations
Expand All @@ -37,6 +41,8 @@ import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

Expand Down Expand Up @@ -92,6 +98,81 @@ class HomeDrawerViewModelTest {
)
}

@Test
fun `given wireCellsEnabledGlobally, when starts observing, then show cells drawer item`() = runTest {
// Given
val (arrangement, viewModel) = Arrangement()
.withWireCellsEnabled(true)
.arrange()

// When
arrangement.unreadArchivedConversationsCountChannel.send(0L)
advanceUntilIdle()

// Then
assertTrue(
listOf(
viewModel.drawerState.items.first,
viewModel.drawerState.items.second
).flatten()
.filterIsInstance<DrawerUiItem.RegularItem>()
.any { it.destination.toString().contains("Cells") }
)
}

@Test
fun `given userInConversationWithCellsEnabled, when starts observing, then show cells drawer item`() = runTest {
// Given
val conversationWithCells = ConversationDetails.Group.Regular(
TestConversation.GROUP(),
isSelfUserMember = true,
selfRole = com.wire.kalium.logic.data.conversation.Conversation.Member.Role.Member,
wireCell = "2024-01-01T00:00:00.000Z",
)
val (arrangement, viewModel) = Arrangement()
.withWireCellsEnabled(false)
.withConversations(listOf(conversationWithCells))
.arrange()

// When
arrangement.unreadArchivedConversationsCountChannel.send(0L)
advanceUntilIdle()

// Then
assertTrue(
listOf(
viewModel.drawerState.items.first,
viewModel.drawerState.items.second
).flatten()
.filterIsInstance<DrawerUiItem.RegularItem>()
.any { it.destination.toString().contains("Cells") }
)
}

@Test
fun `given noCellsEnabled_whenStartsObserving_thenDontShowCellsDrawerItem`() = runTest {
// Given
val conversationWithoutCells = TestConversationDetails.GROUP
val (arrangement, viewModel) = Arrangement()
.withWireCellsEnabled(false)
.withConversations(listOf(conversationWithoutCells))
.arrange()

// When
arrangement.unreadArchivedConversationsCountChannel.send(0L)
advanceUntilIdle()

// Then
assertFalse(
listOf(
viewModel.drawerState.items.first,
viewModel.drawerState.items.second
).flatten()
.filterIsInstance<DrawerUiItem.RegularItem>()
.any { it.destination.toString().contains("Cells") }
)
}

private class Arrangement {

@MockK
Expand All @@ -109,12 +190,16 @@ class HomeDrawerViewModelTest {
@MockK
lateinit var getTeamUrlUseCase: GetTeamUrlUseCase

@MockK
lateinit var observeConversationListDetailsUseCase: ObserveConversationListDetailsUseCase

val unreadArchivedConversationsCountChannel = Channel<Long>(capacity = Channel.UNLIMITED)

init {
MockKAnnotations.init(this, relaxUnitFun = true)
coEvery { observeArchivedUnreadConversationsCount() } returns unreadArchivedConversationsCountChannel.consumeAsFlow()
coEvery { isWireCellsEnabled() } returns false
coEvery { observeConversationListDetailsUseCase(fromArchive = false) } returns flowOf(emptyList())
withSelfUserType()
coEvery { getTeamUrlUseCase() } returns TEAM_URL
}
Expand All @@ -123,12 +208,21 @@ class HomeDrawerViewModelTest {
coEvery { observeSelfUserUseCase() } returns flowOf(TestUser.SELF_USER.copy(userType = UserTypeInfo.Regular(type)))
}

fun withWireCellsEnabled(enabled: Boolean) = apply {
coEvery { isWireCellsEnabled() } returns enabled
}

fun withConversations(conversations: List<ConversationDetails>) = apply {
coEvery { observeConversationListDetailsUseCase(fromArchive = false) } returns flowOf(conversations)
}

fun arrange() = this to HomeDrawerViewModel(
savedStateHandle = savedStateHandle,
observeArchivedUnreadConversationsCount = { observeArchivedUnreadConversationsCount },
observeSelfUser = observeSelfUserUseCase,
getTeamUrl = getTeamUrlUseCase,
isWireCellsEnabled = isWireCellsEnabled,
observeConversationListDetails = { observeConversationListDetailsUseCase },
)

companion object {
Expand Down
Loading