diff --git a/mail-mailbox/presentation/src/main/kotlin/ch/protonmail/android/mailmailbox/presentation/mailbox/MailboxViewModel.kt b/mail-mailbox/presentation/src/main/kotlin/ch/protonmail/android/mailmailbox/presentation/mailbox/MailboxViewModel.kt index 6322e8f90f..b0efc9ba17 100644 --- a/mail-mailbox/presentation/src/main/kotlin/ch/protonmail/android/mailmailbox/presentation/mailbox/MailboxViewModel.kt +++ b/mail-mailbox/presentation/src/main/kotlin/ch/protonmail/android/mailmailbox/presentation/mailbox/MailboxViewModel.kt @@ -150,6 +150,7 @@ import kotlinx.coroutines.flow.channelFlow import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.distinctUntilChangedBy import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.filterNotNull @@ -1439,7 +1440,7 @@ class MailboxViewModel @Inject constructor( private fun observeMailLabelChangeRequests(): Flow = observeSelectedMailLabelId() .mapToExistingLabel() - .distinctUntilChanged() + .distinctUntilChangedBy { it.id } private fun Flow.observeSearchQuery() = this.map { it.mailboxListState as? MailboxListState.Data } .mapNotNull { it?.searchState?.searchQuery } diff --git a/mail-mailbox/presentation/src/test/kotlin/ch/protonmail/android/mailmailbox/presentation/MailboxViewModelTest.kt b/mail-mailbox/presentation/src/test/kotlin/ch/protonmail/android/mailmailbox/presentation/MailboxViewModelTest.kt index 953cbc5759..a935faa17a 100644 --- a/mail-mailbox/presentation/src/test/kotlin/ch/protonmail/android/mailmailbox/presentation/MailboxViewModelTest.kt +++ b/mail-mailbox/presentation/src/test/kotlin/ch/protonmail/android/mailmailbox/presentation/MailboxViewModelTest.kt @@ -1706,6 +1706,55 @@ internal class MailboxViewModelTest { } } + @Test + fun `mailbox pager is not recreated when expanded state of selected folder changes`() = runTest { + // Given + val folder = MailLabelTestData.buildCustomFolder( + id = "folder-1", + isExpanded = false, + children = listOf("child-1") + ) + val child = MailLabelTestData.buildCustomFolder(id = "child-1", parent = folder) + val mailLabelsFlow = MutableStateFlow( + MailLabels( + system = MailLabelTestData.dynamicSystemLabels, + folders = listOf(folder, child), + labels = emptyList() + ) + ) + val currentLocationFlow = MutableStateFlow(folder.id) + every { observeMailLabels(userId) } returns mailLabelsFlow + every { observeLoadedMailLabelId() } returns currentLocationFlow + every { observeSelectedMailLabelId() } returns currentLocationFlow + coEvery { getSelectedMailLabelId() } returns folder.id + every { mailboxReducer.newStateFrom(any(), any()) } returns createMailboxDataState() + expectPagerMock( + selectedLabelId = folder.id, + pagingDataFlow = flowOf(PagingData.from(listOf(unreadMailboxItem))) + ) + + mailboxViewModel.items.test { + awaitItem() + verify(exactly = 1) { + pagerFactory.create(userId, folder.id, any(), any()) + } + + // When + mailLabelsFlow.value = MailLabels( + system = MailLabelTestData.dynamicSystemLabels, + folders = listOf(folder.copy(isExpanded = true), child), + labels = emptyList() + ) + + // Then + expectNoEvents() + verify(exactly = 1) { + pagerFactory.create(userId, folder.id, any(), any()) + } + cancelAndIgnoreRemainingEvents() + } + } + @Test fun `mailbox pager is recreated when view mode changes`() = runTest { // Given