Вторая часть по кэшу
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
package chats.data.repository
|
||||
|
||||
import android.util.Log
|
||||
import androidx.paging.ExperimentalPagingApi
|
||||
import androidx.paging.Pager
|
||||
import androidx.paging.PagingConfig
|
||||
import androidx.paging.PagingData
|
||||
import androidx.paging.map
|
||||
import androidx.work.ExistingPeriodicWorkPolicy
|
||||
import androidx.work.ExistingWorkPolicy
|
||||
import androidx.work.WorkManager
|
||||
@@ -10,6 +15,8 @@ import chats.data.local.database.MessageEntity
|
||||
import chats.data.local.mappers.createPendingMessageEntity
|
||||
import chats.data.local.mappers.toDomain
|
||||
import chats.data.local.mappers.toEntity
|
||||
import chats.data.local.paging.MessagePagingSource
|
||||
import chats.data.local.paging.MessageRemoteMediator
|
||||
import chats.data.remote.api.ChatApi
|
||||
import chats.data.remote.api.SendMessageRequest
|
||||
import chats.data.remote.dto.MessageDto
|
||||
@@ -39,6 +46,7 @@ class ChatRepositoryImpl @Inject constructor(
|
||||
private val messageDao: MessageDao,
|
||||
private val chatDao: ChatDao,
|
||||
private val workManager: WorkManager,
|
||||
private val appDatabase: chats.data.local.database.AppDatabase,
|
||||
private val hubClient: chats.data.remote.signalr.ChatHubClient
|
||||
) : ChatRepository {
|
||||
|
||||
@@ -101,6 +109,34 @@ class ChatRepositoryImpl @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает сообщения с помощью Paging 3 и RemoteMediator
|
||||
*/
|
||||
@OptIn(ExperimentalPagingApi::class)
|
||||
override fun getMessagesPagingSource(chatId: String): Flow<PagingData<Message>> {
|
||||
return Pager(
|
||||
config = PagingConfig(
|
||||
pageSize = 30,
|
||||
prefetchDistance = 10,
|
||||
enablePlaceholders = false,
|
||||
initialLoadSize = 60
|
||||
),
|
||||
pagingSourceFactory = {
|
||||
MessagePagingSource(chatId, messageDao)
|
||||
},
|
||||
remoteMediator = MessageRemoteMediator(
|
||||
chatId = chatId,
|
||||
messageDao = messageDao,
|
||||
chatDao = chatDao,
|
||||
chatApi = api,
|
||||
tokenManager = tokenManager,
|
||||
appDatabase = appDatabase
|
||||
)
|
||||
).flow.map { pagingData ->
|
||||
pagingData.map { entity -> entity.toDomain() }
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getMessages(chatId: String, cursor: String?, pivot: Long?, limit: Int?): List<Message> {
|
||||
val baseUrl = serverConfig.getBaseUrl().removeSuffix("/api/")
|
||||
val currentUserId = tokenManager.getUserId() ?: ""
|
||||
|
||||
Reference in New Issue
Block a user