Commit 29a7f307 authored by Lucio Maciel's avatar Lucio Maciel

Fix crash when the message list is empty when recovering from a network loss

parent 61cb7b9e
...@@ -188,25 +188,27 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView, ...@@ -188,25 +188,27 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
launch(parent = strategy.jobs) { launch(parent = strategy.jobs) {
if (chatRoomId != null && chatRoomType != null) { if (chatRoomId != null && chatRoomType != null) {
val roomType = roomTypeOf(chatRoomType!!) val roomType = roomTypeOf(chatRoomType!!)
val lastMessage = messagesRepository.getByRoomId(chatRoomId!!).sortedByDescending { it.timestamp }.first() messagesRepository.getByRoomId(chatRoomId!!)
val instant = Instant.ofEpochMilli(lastMessage.timestamp) .sortedByDescending { it.timestamp }.firstOrNull()?.let { lastMessage ->
val messages = client.history(chatRoomId!!, roomType, count = 50, val instant = Instant.ofEpochMilli(lastMessage.timestamp)
oldest = instant.toString()) val messages = client.history(chatRoomId!!, roomType, count = 50,
Timber.d("History: $messages") oldest = instant.toString())
Timber.d("History: $messages")
if (messages.result.isNotEmpty()) { if (messages.result.isNotEmpty()) {
val models = mapper.map(messages.result) val models = mapper.map(messages.result)
messagesRepository.saveAll(messages.result) messagesRepository.saveAll(messages.result)
launchUI(strategy) { launchUI(strategy) {
view.showNewMessage(models) view.showNewMessage(models)
} }
if (messages.result.size == 50) { if (messages.result.size == 50) {
// we loade at least count messages, try one more to fetch more messages // we loade at least count messages, try one more to fetch more messages
loadMissingMessages() loadMissingMessages()
} }
} }
}
} }
} }
} }
......
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