This commit is contained in:
Eric85710 2026-06-09 15:53:12 +08:00
parent 346ca621d0
commit 6d9c94b12e

View file

@ -84,8 +84,10 @@ class ChatRoomsRepository @Inject constructor(
if (response.isSuccessful) { if (response.isSuccessful) {
val body = response.body() val body = response.body()
if (body != null) { if (body != null) {
body.messages.forEach { networkMessage ->
val entity = MessageEntity( // 1. 使用 map 把網路資料轉換成 Entity 列表
val entities = body.messages.map { networkMessage ->
MessageEntity(
id = networkMessage.id, id = networkMessage.id,
chatRoomId = roomId, chatRoomId = roomId,
senderId = networkMessage.senderId, senderId = networkMessage.senderId,
@ -98,12 +100,21 @@ class ChatRoomsRepository @Inject constructor(
status = MessageStatus.SUCCESS, status = MessageStatus.SUCCESS,
reactions = networkMessage.reactions reactions = networkMessage.reactions
) )
messageDao.insertOrUpdate(entity)
} }
// 2. 一口氣整批塞進資料庫,Room 只會開啟一次資料庫事務 (Transaction)
messageDao.insertOrUpdateList(entities)
Result.success(Unit) Result.success(Unit)
} else { Result.failure(Exception("Empty body")) } } else {
} else { Result.failure(Exception("Error code: ${response.code()}")) } Result.failure(Exception("Empty body"))
} catch (e: Exception) { Result.failure(e) } }
} else {
Result.failure(Exception("Error code: ${response.code()}"))
}
} catch (e: Exception) {
Result.failure(e)
}
} }
//fake sending //fake sending