Commit c8ec586b authored by Kiryl Vashyla's avatar Kiryl Vashyla

loadMissingMessages() improved: unused messages polling from local repository removed

parent 7760bd1a
...@@ -503,51 +503,47 @@ class ChatRoomPresenter @Inject constructor( ...@@ -503,51 +503,47 @@ class ChatRoomPresenter @Inject constructor(
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!!)
messagesRepository.getByRoomId(chatRoomId!!) val lastSyncDate = messagesRepository.getLastSyncDate()
.sortedByDescending { it.timestamp }.firstOrNull()?.let { lastMessage -> // lastSyncDate or 0. LastSyncDate could be in case when we sent some messages offline(and saved them locally),
// but never has obtained chatMessages(or history) from remote. In this case we should sync all chat history from beginning
val lastSyncDate = messagesRepository.getLastSyncDate() val instant = Instant.ofEpochMilli(lastSyncDate ?: 0).toString()
// lastSyncDate or 0. LastSyncDate could be in case when we sent some messages offline(and saved them locally), //
// but never has obtained chatMessages(or history) from remote. In this case we should sync all chat history from beginning try {
val instant = Instant.ofEpochMilli(lastSyncDate ?: 0).toString() val messages =
retryIO(description = "history($chatRoomId, $roomType, $instant)") {
try { client.history(
val messages = chatRoomId!!, roomType, count = 50,
retryIO(description = "history($chatRoomId, $roomType, $instant)") { oldest = instant
client.history( )
chatRoomId!!, roomType, count = 50, }
oldest = instant Timber.d("History: $messages")
)
}
Timber.d("History: $messages")
if (messages.result.isNotEmpty()) {
val models = mapper.map(messages.result, RoomUiModel(
roles = chatRoles,
isBroadcast = chatIsBroadcast,
// FIXME: Why are we fixing isRoom attribute to true here?
isRoom = true
))
messagesRepository.saveAll(messages.result)
//if success - saving last synced time
//assume that BE returns ordered messages, the first message is the latest one
messagesRepository.saveLastSyncDate(messages.result.first().timestamp)
launchUI(strategy) {
view.showNewMessage(models, true)
}
if (messages.result.size == 50) { if (messages.result.isNotEmpty()) {
// we loaded at least count messages, try one more to fetch more messages val models = mapper.map(messages.result, RoomUiModel(
loadMissingMessages() roles = chatRoles,
} isBroadcast = chatIsBroadcast,
} // FIXME: Why are we fixing isRoom attribute to true here?
} catch (ex: Exception) { isRoom = true
// TODO - we need to better treat connection problems here, but no let gaps ))
// on the messages list messagesRepository.saveAll(messages.result)
Timber.d(ex, "Error fetching channel history") //if success - saving last synced time
//assume that BE returns ordered messages, the first message is the latest one
messagesRepository.saveLastSyncDate(messages.result.first().timestamp)
launchUI(strategy) {
view.showNewMessage(models, true)
}
if (messages.result.size == 50) {
// we loaded at least count messages, try one more to fetch more messages
loadMissingMessages()
} }
} }
} catch (ex: Exception) {
// TODO - we need to better treat connection problems here, but no let gaps
// on the messages list
Timber.d(ex, "Error fetching channel history")
}
} }
} }
} }
......
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