Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: conversation list pagination, pt2 - pager and use case [WPB-9433] #3058

Closed
Closed
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
@@ -0,0 +1,21 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.logic.feature.conversation

val ConversationScope.getPaginatedFlowOfConversationDetailsWithEventsBySearchQuery
get() = GetPaginatedFlowOfConversationDetailsWithEventsBySearchQueryUseCase(dispatcher, conversationRepository)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.logic.feature.conversation

import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.wire.kalium.logic.data.conversation.ConversationDetailsWithEvents
import com.wire.kalium.logic.data.conversation.ConversationRepository
import com.wire.kalium.logic.data.conversation.ConversationQueryConfig
import com.wire.kalium.util.KaliumDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn

/**
* This use case will observe and return a flow of paginated searched conversation details with last message and unread events counts.
* @see PagingData
* @see ConversationDetailsWithEvents
*/
class GetPaginatedFlowOfConversationDetailsWithEventsBySearchQueryUseCase internal constructor(
private val dispatcher: KaliumDispatcher,
private val conversationRepository: ConversationRepository,
) {
suspend operator fun invoke(
queryConfig: ConversationQueryConfig,
pagingConfig: PagingConfig,
startingOffset: Long,
): Flow<PagingData<ConversationDetailsWithEvents>> = conversationRepository.extensions
.getPaginatedConversationDetailsWithEventsBySearchQuery(queryConfig, pagingConfig, startingOffset).flowOn(dispatcher.io)
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ import kotlinx.datetime.Instant

@Suppress("TooManyFunctions")
interface ConversationRepository {
val extensions: ConversationRepositoryExtensions

// region Get/Observe by id

suspend fun observeConversationById(conversationId: ConversationId): Flow<Either<StorageFailure, Conversation>>
Expand Down Expand Up @@ -327,6 +329,8 @@ internal class ConversationDataSource internal constructor(
private val messageMapper: MessageMapper = MapperProvider.messageMapper(selfUserId),
private val receiptModeMapper: ReceiptModeMapper = MapperProvider.receiptModeMapper()
) : ConversationRepository {
override val extensions: ConversationRepositoryExtensions =
ConversationRepositoryExtensionsImpl(conversationDAO, conversationMapper, messageMapper)

// region Get/Observe by id

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.logic.data.conversation

import app.cash.paging.PagingConfig
import app.cash.paging.PagingData
import app.cash.paging.map
import com.wire.kalium.logic.data.message.MessageMapper
import com.wire.kalium.logic.data.message.UnreadEventType
import com.wire.kalium.persistence.dao.conversation.ConversationDAO
import com.wire.kalium.persistence.dao.conversation.ConversationDetailsWithEventsEntity
import com.wire.kalium.persistence.dao.conversation.ConversationExtensions.QueryConfig
import com.wire.kalium.persistence.dao.message.KaliumPager
import com.wire.kalium.persistence.dao.unread.UnreadEventTypeEntity
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

interface ConversationRepositoryExtensions {
suspend fun getPaginatedConversationDetailsWithEventsBySearchQuery(
queryConfig: ConversationQueryConfig,
pagingConfig: PagingConfig,
startingOffset: Long,
): Flow<PagingData<ConversationDetailsWithEvents>>
}

class ConversationRepositoryExtensionsImpl internal constructor(
private val conversationDAO: ConversationDAO,
private val conversationMapper: ConversationMapper,
private val messageMapper: MessageMapper,
) : ConversationRepositoryExtensions {
override suspend fun getPaginatedConversationDetailsWithEventsBySearchQuery(
queryConfig: ConversationQueryConfig,
pagingConfig: PagingConfig,
startingOffset: Long
): Flow<PagingData<ConversationDetailsWithEvents>> {
val pager: KaliumPager<ConversationDetailsWithEventsEntity> = with(queryConfig) {
conversationDAO.platformExtensions.getPagerForConversationDetailsWithEventsSearch(
queryConfig = QueryConfig(searchQuery, fromArchive, onlyInteractionEnabled, newActivitiesOnTop),
pagingConfig = pagingConfig
)
}

return pager.pagingDataFlow.map {
it.map {
ConversationDetailsWithEvents(
conversationDetails = conversationMapper.fromDaoModelToDetails(it.conversationViewEntity),
lastMessage = when {
it.messageDraft != null -> messageMapper.fromDraftToMessagePreview(it.messageDraft!!)
it.lastMessage != null -> messageMapper.fromEntityToMessagePreview(it.lastMessage!!)
else -> null
},
unreadEventCount = it.unreadEvents.unreadEvents.mapKeys {
when (it.key) {
UnreadEventTypeEntity.KNOCK -> UnreadEventType.KNOCK
UnreadEventTypeEntity.MISSED_CALL -> UnreadEventType.MISSED_CALL
UnreadEventTypeEntity.MENTION -> UnreadEventType.MENTION
UnreadEventTypeEntity.REPLY -> UnreadEventType.REPLY
UnreadEventTypeEntity.MESSAGE -> UnreadEventType.MESSAGE
}
},
hasNewActivitiesToShow = it.hasNewActivitiesToShow,
)
}
}
}
}

data class ConversationQueryConfig(
val searchQuery: String = "",
val fromArchive: Boolean = false,
val onlyInteractionEnabled: Boolean = false,
val newActivitiesOnTop: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ import com.wire.kalium.logic.feature.team.DeleteTeamConversationUseCaseImpl
import com.wire.kalium.logic.sync.SyncManager
import com.wire.kalium.logic.sync.receiver.conversation.RenamedConversationEventHandler
import com.wire.kalium.logic.sync.receiver.handler.CodeUpdateHandlerImpl
import com.wire.kalium.util.KaliumDispatcher
import com.wire.kalium.util.KaliumDispatcherImpl
import kotlinx.coroutines.CoroutineScope

@Suppress("LongParameterList")
class ConversationScope internal constructor(
private val conversationRepository: ConversationRepository,
internal val conversationRepository: ConversationRepository,
private val conversationGroupRepository: ConversationGroupRepository,
private val connectionRepository: ConnectionRepository,
private val userRepository: UserRepository,
Expand All @@ -101,7 +102,8 @@ class ConversationScope internal constructor(
private val scope: CoroutineScope,
private val kaliumLogger: KaliumLogger,
private val refreshUsersWithoutMetadata: RefreshUsersWithoutMetadataUseCase,
private val serverConfigLinks: ServerConfig.Links
private val serverConfigLinks: ServerConfig.Links,
internal val dispatcher: KaliumDispatcher = KaliumDispatcherImpl,
) {

val getConversations: GetConversationsUseCase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

package com.wire.kalium.logic.data.conversation

import app.cash.paging.Pager
import app.cash.paging.PagingConfig
import app.cash.paging.PagingSource
import app.cash.paging.PagingState
import com.wire.kalium.logic.data.message.MessageMapper
import com.wire.kalium.logic.framework.TestConversationDetails
import com.wire.kalium.logic.framework.TestMessage
import com.wire.kalium.persistence.dao.conversation.ConversationDAO
import com.wire.kalium.persistence.dao.conversation.ConversationDetailsWithEventsEntity
import com.wire.kalium.persistence.dao.conversation.ConversationExtensions
import com.wire.kalium.persistence.dao.message.KaliumPager
import io.mockative.Mock
import io.mockative.any
import io.mockative.eq
import io.mockative.every
import io.mockative.matches
import io.mockative.mock
import io.mockative.once
import io.mockative.verify
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.runTest
import kotlin.test.Test

class ConversationRepositoryExtensionsTest {
private val fakePagingSource = object : PagingSource<Int, ConversationDetailsWithEventsEntity>() {
override fun getRefreshKey(state: PagingState<Int, ConversationDetailsWithEventsEntity>): Int? = null

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ConversationDetailsWithEventsEntity> =
LoadResult.Error(NotImplementedError("STUB for tests. Not implemented."))
}

@Test
fun givenParameters_whenPaginatedConversationDetailsWithEvents_thenShouldCallDaoExtensionsWithRightParameters() = runTest {
val pagingConfig = PagingConfig(20)
val pager = Pager(pagingConfig) { fakePagingSource }
val kaliumPager = KaliumPager(pager, fakePagingSource, StandardTestDispatcher())
val (arrangement, conversationRepositoryExtensions) = Arrangement()
.withConversationExtensionsReturningPager(kaliumPager)
.arrange()
val searchQuery = "search"
conversationRepositoryExtensions.getPaginatedConversationDetailsWithEventsBySearchQuery(
queryConfig = ConversationQueryConfig(
searchQuery = searchQuery,
fromArchive = false,
onlyInteractionEnabled = false,
newActivitiesOnTop = false,
),
pagingConfig = pagingConfig,
startingOffset = 0L
)
verify {
arrangement.conversationDaoExtensions
.getPagerForConversationDetailsWithEventsSearch(
queryConfig = matches {
it.searchQuery == searchQuery && !it.fromArchive && !it.onlyInteractionEnabled && !it.newActivitiesOnTop
},
pagingConfig = eq(pagingConfig),
startingOffset = any()
)
}.wasInvoked(exactly = once)
}

private class Arrangement {
@Mock
val conversationDaoExtensions: ConversationExtensions = mock(ConversationExtensions::class)

@Mock
private val conversationDAO: ConversationDAO = mock(ConversationDAO::class)

@Mock
private val conversationMapper: ConversationMapper = mock(ConversationMapper::class)

@Mock
private val messageMapper: MessageMapper = mock(MessageMapper::class)
private val conversationRepositoryExtensions: ConversationRepositoryExtensions by lazy {
ConversationRepositoryExtensionsImpl(conversationDAO, conversationMapper, messageMapper)
}

init {
every {
messageMapper.fromEntityToMessage(any())
}.returns(TestMessage.TEXT_MESSAGE)
every {
conversationMapper.fromDaoModelToDetails(any())
}.returns(TestConversationDetails.CONVERSATION_GROUP)
every {
conversationDAO.platformExtensions
}.returns(conversationDaoExtensions)
}

fun withConversationExtensionsReturningPager(kaliumPager: KaliumPager<ConversationDetailsWithEventsEntity>) = apply {
every {
conversationDaoExtensions.getPagerForConversationDetailsWithEventsSearch(any(), any(), any())
}.returns(kaliumPager)
}

fun arrange() = this to conversationRepositoryExtensions
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ data class ProposalTimerEntity(

@Suppress("TooManyFunctions")
interface ConversationDAO {
val platformExtensions: ConversationExtensions
//region Get/Observe by ID

suspend fun observeConversationById(qualifiedID: QualifiedIDEntity): Flow<ConversationEntity?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ internal class ConversationDAOImpl internal constructor(
) : ConversationDAO {
private val conversationMapper = ConversationMapper
private val conversationDetailsWithEventsMapper = ConversationDetailsWithEventsMapper
override val platformExtensions: ConversationExtensions =
ConversationExtensionsImpl(conversationQueries, conversationDetailsWithEventsMapper, coroutineContext)

// region Get/Observe by ID

Expand Down
Loading
Loading