Подгрузка старых сообщений
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -27,6 +27,7 @@ interface ChatApi {
|
||||
suspend fun getMessages(
|
||||
@Path("chatId") chatId: String,
|
||||
@Query("cursor") cursor: String? = null,
|
||||
@Query("pivot") pivot: Long? = null,
|
||||
@Query("limit") limit: Int? = 50
|
||||
): List<MessageDto>
|
||||
|
||||
|
||||
@@ -41,27 +41,24 @@ class ChatRepositoryImpl @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getMessages(chatId: String, cursor: String?, limit: Int?): List<Message> {
|
||||
override suspend fun getMessages(chatId: String, cursor: String?, pivot: Long?, limit: Int?): List<Message> {
|
||||
val baseUrl = serverConfig.getBaseUrl().removeSuffix("/api/")
|
||||
val currentUserId = tokenManager.getUserId() ?: ""
|
||||
return try {
|
||||
val chats = api.getChats()
|
||||
val chatDto = chats.find { it.id == chatId }
|
||||
val unreadCount = chatDto?.unreadCount ?: 0
|
||||
|
||||
android.util.Log.d("ChatRepo", "Fetching messages from network for chat: $chatId, cursor: $cursor")
|
||||
android.util.Log.d("ChatRepo", "FETCH: chatId=$chatId, cursor=$cursor, limit=$limit")
|
||||
val messages = api.getMessages(chatId, cursor = cursor, limit = limit)
|
||||
|
||||
if (messages.isNotEmpty()) {
|
||||
android.util.Log.d("ChatRepo", "Received ${messages.size} messages. First: ${messages.first().createdAt}, Last: ${messages.last().createdAt}")
|
||||
android.util.Log.d("ChatRepo", "Received ${messages.size} messages. TopSeq: ${messages.first().sequenceId}, BottomSeq: ${messages.last().sequenceId}")
|
||||
}
|
||||
|
||||
// Определяем, какие сообщения считаются прочитанными
|
||||
val mappedMessages = messages.mapIndexed { index, msg ->
|
||||
val isUnread = (messages.size - index) <= unreadCount && msg.senderId != currentUserId
|
||||
msg.toDomain(currentUserId, baseUrl).copy(isRead = !isUnread)
|
||||
// Мапим в доменные модели. По умолчанию считаем прочитанными,
|
||||
// так как unreadCount нам тут не критичен для истории.
|
||||
messages.map { msg ->
|
||||
msg.toDomain(currentUserId, baseUrl).copy(isRead = true)
|
||||
}
|
||||
mappedMessages
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.e("ChatRepo", "Fetch messages failed", e)
|
||||
emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import chats.domain.model.Message
|
||||
interface ChatRepository {
|
||||
suspend fun getChats(): List<Chat>
|
||||
fun getMessagesFlow(chatId: String): kotlinx.coroutines.flow.Flow<List<Message>>
|
||||
suspend fun getMessages(chatId: String, cursor: String? = null, limit: Int? = null): List<Message>
|
||||
suspend fun getMessages(chatId: String, cursor: String? = null, pivot: Long? = null, limit: Int? = null): List<Message>
|
||||
suspend fun sendMessage(
|
||||
chatId: String,
|
||||
content: String?,
|
||||
|
||||
@@ -221,14 +221,18 @@ fun ChatDetailScreen(
|
||||
}
|
||||
|
||||
// Подгрузка истории при прокрутке вверх
|
||||
LaunchedEffect(listState) {
|
||||
snapshotFlow { listState.layoutInfo.visibleItemsInfo.lastOrNull()?.index }
|
||||
.collect { lastVisibleIndex ->
|
||||
if (lastVisibleIndex != null && lastVisibleIndex >= listItems.size - 10 && listItems.size >= 15) {
|
||||
android.util.Log.d("ChatDetailScreen", "TRIGGER LOAD MORE: index $lastVisibleIndex, size ${listItems.size}")
|
||||
viewModel.loadMoreMessages()
|
||||
}
|
||||
LaunchedEffect(listState, state.messages.size) {
|
||||
snapshotFlow {
|
||||
val lastIndex = listState.layoutInfo.visibleItemsInfo.lastOrNull()?.index ?: 0
|
||||
val totalCount = listState.layoutInfo.totalItemsCount
|
||||
lastIndex to totalCount
|
||||
}
|
||||
.collect { (lastVisibleIndex, totalCount) ->
|
||||
if (totalCount > 0 && lastVisibleIndex >= totalCount - 20 && !state.isLoadingMore) {
|
||||
android.util.Log.d("ChatDetailScreen", "TRIGGER LOAD MORE: index $lastVisibleIndex, total $totalCount")
|
||||
viewModel.loadMoreMessages()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val unreadCount = remember(state.messages) {
|
||||
|
||||
Reference in New Issue
Block a user