Правки

This commit is contained in:
Халимов Рустам
2026-05-13 13:48:34 +03:00
parent 00ed4b5959
commit df4feeeee5
12 changed files with 263 additions and 82 deletions
@@ -17,7 +17,7 @@ import chats.data.local.dao.UserProfileDao
ChatEntity::class,
UserProfileEntity::class
],
version = 1,
version = 2,
exportSchema = false
)
@TypeConverters(MessageStatusConverter::class)
@@ -33,5 +33,7 @@ data class ChatEntity(
val lastMessageTimestamp: Long? = null,
val lastMessageJson: String? = null,
val updatedAtMillis: Long = System.currentTimeMillis()
)
@@ -2,6 +2,9 @@ package chats.data.local.mappers
import chats.data.local.database.ChatEntity
import chats.domain.model.Chat
import com.google.gson.Gson
private val gson = Gson()
/**
* Преобразует Domain Chat в Entity
@@ -16,7 +19,8 @@ fun Chat.toEntity(): ChatEntity {
avatar = this.avatar,
unreadCount = this.unreadCount,
lastMessageText = this.lastMessage?.content,
lastMessageTimestamp = timestamp
lastMessageTimestamp = timestamp,
lastMessageJson = this.lastMessage?.let { gson.toJson(it) }
)
}
@@ -24,13 +28,20 @@ fun Chat.toEntity(): ChatEntity {
* Преобразует Entity в Domain Chat
*/
fun ChatEntity.toDomain(): Chat {
val lastMessage = this.lastMessageJson?.let { json ->
try {
gson.fromJson(json, chats.domain.model.Message::class.java)
} catch (e: Exception) {
null
}
}
return Chat(
id = this.remoteId ?: this.localId,
type = this.type,
name = this.name,
avatar = this.avatar,
unreadCount = this.unreadCount,
lastMessage = null // lastMessage загружается отдельно
lastMessage = lastMessage
)
}
@@ -13,6 +13,7 @@ import chats.data.local.mappers.toEntity
import chats.data.repository.toDomain as dtoToDomain
import chats.data.remote.api.ChatApi
import chats.domain.model.MessageStatus
import core.network.ServerConfig
import core.security.TokenManager
/**
@@ -26,7 +27,8 @@ class MessageRemoteMediator(
private val chatDao: ChatDao,
private val chatApi: ChatApi,
private val tokenManager: TokenManager,
private val appDatabase: AppDatabase
private val appDatabase: AppDatabase,
private val serverConfig: ServerConfig
) : RemoteMediator<Int, MessageEntity>() {
companion object {
@@ -39,7 +41,7 @@ class MessageRemoteMediator(
): RemoteMediator.MediatorResult {
return try {
val currentUserId = tokenManager.getUserId() ?: ""
val baseUrl = "https://your-server.com"
val baseUrl = serverConfig.getBaseUrl().removeSuffix("/api/")
val cursor = when (loadType) {
LoadType.REFRESH -> {