Commit 6b157720 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Sort saved local messages according to their timestamps

parent 903ca99c
...@@ -73,21 +73,14 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView, ...@@ -73,21 +73,14 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
launchUI(strategy) { launchUI(strategy) {
view.showLoading() view.showLoading()
try { try {
val oldMessages = messagesRepository.getByRoomId(chatRoomId).sortedWith(compareBy(Message::timestamp)).reversed() val oldMessages = messagesRepository.getByRoomId(chatRoomId)
view.showMessages(mapper.map(oldMessages)) view.showMessages(mapper.map(oldMessages))
val messages = val messages =
retryIO(description = "messages chatRoom: $chatRoomId, type: $chatRoomType, offset: $offset") { retryIO(description = "messages chatRoom: $chatRoomId, type: $chatRoomType, offset: $offset") {
client.messages(chatRoomId, roomTypeOf(chatRoomType), offset, 30).result client.messages(chatRoomId, roomTypeOf(chatRoomType), offset, 30).result
} }
messagesRepository.saveAll(messages) messagesRepository.saveAll(messages)
val messagesViewModels = mapper.map(messages)
val unsentMessages = messagesRepository.getUnsentByRoomId(chatRoomId)
var mergedMessages = messages.toMutableList()
mergedMessages.addAll(unsentMessages)
val allMessages = mergedMessages.toList().filterNot { message ->
unsentMessages.find { it.id == message.id } != null
}
val messagesViewModels = mapper.map(allMessages)
view.showMessages(messagesViewModels) view.showMessages(messagesViewModels)
// TODO: For now we are marking the room as read if we can get the messages (I mean, no exception occurs) // TODO: For now we are marking the room as read if we can get the messages (I mean, no exception occurs)
......
...@@ -29,7 +29,7 @@ class SharedPreferencesMessagesRepository( ...@@ -29,7 +29,7 @@ class SharedPreferencesMessagesRepository(
val values = prefs.all.values as Collection<String> val values = prefs.all.values as Collection<String>
return@withContext values.mapNotNull { adapter.fromJson(it) }.filter { return@withContext values.mapNotNull { adapter.fromJson(it) }.filter {
it.roomId == rid it.roomId == rid
}.toList() }.toList().sortedWith(compareBy(Message::timestamp)).reversed()
} }
override suspend fun getRecentMessages(rid: String, count: Long): List<Message> { override suspend fun getRecentMessages(rid: String, count: Long): List<Message> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment