Подгрузка старых сообщений
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(
|
suspend fun getMessages(
|
||||||
@Path("chatId") chatId: String,
|
@Path("chatId") chatId: String,
|
||||||
@Query("cursor") cursor: String? = null,
|
@Query("cursor") cursor: String? = null,
|
||||||
|
@Query("pivot") pivot: Long? = null,
|
||||||
@Query("limit") limit: Int? = 50
|
@Query("limit") limit: Int? = 50
|
||||||
): List<MessageDto>
|
): 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 baseUrl = serverConfig.getBaseUrl().removeSuffix("/api/")
|
||||||
val currentUserId = tokenManager.getUserId() ?: ""
|
val currentUserId = tokenManager.getUserId() ?: ""
|
||||||
return try {
|
return try {
|
||||||
val chats = api.getChats()
|
android.util.Log.d("ChatRepo", "FETCH: chatId=$chatId, cursor=$cursor, limit=$limit")
|
||||||
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")
|
|
||||||
val messages = api.getMessages(chatId, cursor = cursor, limit = limit)
|
val messages = api.getMessages(chatId, cursor = cursor, limit = limit)
|
||||||
|
|
||||||
if (messages.isNotEmpty()) {
|
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 ->
|
// так как unreadCount нам тут не критичен для истории.
|
||||||
val isUnread = (messages.size - index) <= unreadCount && msg.senderId != currentUserId
|
messages.map { msg ->
|
||||||
msg.toDomain(currentUserId, baseUrl).copy(isRead = !isUnread)
|
msg.toDomain(currentUserId, baseUrl).copy(isRead = true)
|
||||||
}
|
}
|
||||||
mappedMessages
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
android.util.Log.e("ChatRepo", "Fetch messages failed", e)
|
||||||
emptyList()
|
emptyList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import chats.domain.model.Message
|
|||||||
interface ChatRepository {
|
interface ChatRepository {
|
||||||
suspend fun getChats(): List<Chat>
|
suspend fun getChats(): List<Chat>
|
||||||
fun getMessagesFlow(chatId: String): kotlinx.coroutines.flow.Flow<List<Message>>
|
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(
|
suspend fun sendMessage(
|
||||||
chatId: String,
|
chatId: String,
|
||||||
content: String?,
|
content: String?,
|
||||||
|
|||||||
@@ -221,14 +221,18 @@ fun ChatDetailScreen(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Подгрузка истории при прокрутке вверх
|
// Подгрузка истории при прокрутке вверх
|
||||||
LaunchedEffect(listState) {
|
LaunchedEffect(listState, state.messages.size) {
|
||||||
snapshotFlow { listState.layoutInfo.visibleItemsInfo.lastOrNull()?.index }
|
snapshotFlow {
|
||||||
.collect { lastVisibleIndex ->
|
val lastIndex = listState.layoutInfo.visibleItemsInfo.lastOrNull()?.index ?: 0
|
||||||
if (lastVisibleIndex != null && lastVisibleIndex >= listItems.size - 10 && listItems.size >= 15) {
|
val totalCount = listState.layoutInfo.totalItemsCount
|
||||||
android.util.Log.d("ChatDetailScreen", "TRIGGER LOAD MORE: index $lastVisibleIndex, size ${listItems.size}")
|
lastIndex to totalCount
|
||||||
viewModel.loadMoreMessages()
|
}
|
||||||
}
|
.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) {
|
val unreadCount = remember(state.messages) {
|
||||||
|
|||||||
Reference in New Issue
Block a user