Commit 903ca99c authored by Leonardo Aramaki's avatar Leonardo Aramaki

Fix timestamp for offline messages and showing saved local messages on chat

parent 998f24f2
......@@ -73,13 +73,21 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
launchUI(strategy) {
view.showLoading()
try {
val oldMessages = messagesRepository.getByRoomId(chatRoomId).sortedWith(compareBy(Message::timestamp)).reversed()
view.showMessages(mapper.map(oldMessages))
val messages =
retryIO(description = "messages chatRoom: $chatRoomId, type: $chatRoomType, offset: $offset") {
client.messages(chatRoomId, roomTypeOf(chatRoomType), offset, 30).result
}
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)
// TODO: For now we are marking the room as read if we can get the messages (I mean, no exception occurs)
......@@ -88,7 +96,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
subscribeMessages(chatRoomId)
} catch (ex: Exception) {
ex.printStackTrace()
Timber.e(ex)
ex.message?.let {
view.showMessage(it)
}.ifNull {
......@@ -115,7 +123,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
id = id,
roomId = chatRoomId,
message = text,
timestamp = Instant.now().epochSecond,
timestamp = Instant.now().toEpochMilli(),
sender = SimpleUser(null, username, username),
attachments = null,
avatar = currentServer.avatarUrl(username!!),
......
......@@ -49,7 +49,7 @@ class MessageService : JobService() {
}
private suspend fun retrySendingMessages(currentServer: String) {
val temporaryMessages = messageRepository.getAllTemporary()
val temporaryMessages = messageRepository.getAllUnsent()
if (temporaryMessages.isNotEmpty()) {
val connectionManager = factory.create(currentServer)
val client = connectionManager.client
......
......@@ -69,7 +69,7 @@ interface MessagesRepository {
*/
suspend fun removeByRoomId(rid: String)
suspend fun getAllTemporary(): List<Message>
suspend fun getAllUnsent(): List<Message>
suspend fun getAllTemporaryByRoomId(roomId: String): List<Message>
suspend fun getUnsentByRoomId(roomId: String): List<Message>
}
\ No newline at end of file
......@@ -26,7 +26,7 @@ class MemoryMessagesRepository : MessagesRepository {
return@withContext messages.values.toList()
}
override suspend fun getAllTemporaryByRoomId(roomId: String): List<Message> = withContext(CommonPool) {
override suspend fun getUnsentByRoomId(roomId: String): List<Message> = withContext(CommonPool) {
val allByRoomId = getByRoomId(roomId)
if (allByRoomId.isEmpty()) {
return@withContext emptyList<Message>()
......@@ -34,7 +34,7 @@ class MemoryMessagesRepository : MessagesRepository {
return@withContext allByRoomId.filter { it.isTemporary ?: false && it.roomId == roomId }
}
override suspend fun getAllTemporary(): List<Message> = withContext(CommonPool) {
override suspend fun getAllUnsent(): List<Message> = withContext(CommonPool) {
val all = getAll()
if (all.isEmpty()) {
return@withContext emptyList<Message>()
......
......@@ -46,7 +46,7 @@ class SharedPreferencesMessagesRepository(
return@withContext values.mapNotNull { adapter.fromJson(it) }
}
override suspend fun getAllTemporary(): List<Message> = withContext(CommonPool) {
override suspend fun getAllUnsent(): List<Message> = withContext(CommonPool) {
if (prefs.all.values.isEmpty()) {
return@withContext emptyList<Message>()
}
......@@ -56,7 +56,7 @@ class SharedPreferencesMessagesRepository(
.filter { it.isTemporary ?: false }
}
override suspend fun getAllTemporaryByRoomId(roomId: String): List<Message> = withContext(CommonPool) {
override suspend fun getUnsentByRoomId(roomId: String): List<Message> = withContext(CommonPool) {
val allByRoomId = getByRoomId(roomId)
if (allByRoomId.isEmpty()) {
return@withContext emptyList<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