Правки
This commit is contained in:
@@ -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
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user