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, ...@@ -73,13 +73,21 @@ 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()
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)
...@@ -88,7 +96,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView, ...@@ -88,7 +96,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
subscribeMessages(chatRoomId) subscribeMessages(chatRoomId)
} catch (ex: Exception) { } catch (ex: Exception) {
ex.printStackTrace() Timber.e(ex)
ex.message?.let { ex.message?.let {
view.showMessage(it) view.showMessage(it)
}.ifNull { }.ifNull {
...@@ -115,7 +123,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView, ...@@ -115,7 +123,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
id = id, id = id,
roomId = chatRoomId, roomId = chatRoomId,
message = text, message = text,
timestamp = Instant.now().epochSecond, timestamp = Instant.now().toEpochMilli(),
sender = SimpleUser(null, username, username), sender = SimpleUser(null, username, username),
attachments = null, attachments = null,
avatar = currentServer.avatarUrl(username!!), avatar = currentServer.avatarUrl(username!!),
......
...@@ -49,7 +49,7 @@ class MessageService : JobService() { ...@@ -49,7 +49,7 @@ class MessageService : JobService() {
} }
private suspend fun retrySendingMessages(currentServer: String) { private suspend fun retrySendingMessages(currentServer: String) {
val temporaryMessages = messageRepository.getAllTemporary() val temporaryMessages = messageRepository.getAllUnsent()
if (temporaryMessages.isNotEmpty()) { if (temporaryMessages.isNotEmpty()) {
val connectionManager = factory.create(currentServer) val connectionManager = factory.create(currentServer)
val client = connectionManager.client val client = connectionManager.client
......
...@@ -69,7 +69,7 @@ interface MessagesRepository { ...@@ -69,7 +69,7 @@ interface MessagesRepository {
*/ */
suspend fun removeByRoomId(rid: String) 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 { ...@@ -26,7 +26,7 @@ class MemoryMessagesRepository : MessagesRepository {
return@withContext messages.values.toList() 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) val allByRoomId = getByRoomId(roomId)
if (allByRoomId.isEmpty()) { if (allByRoomId.isEmpty()) {
return@withContext emptyList<Message>() return@withContext emptyList<Message>()
...@@ -34,7 +34,7 @@ class MemoryMessagesRepository : MessagesRepository { ...@@ -34,7 +34,7 @@ class MemoryMessagesRepository : MessagesRepository {
return@withContext allByRoomId.filter { it.isTemporary ?: false && it.roomId == roomId } 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() val all = getAll()
if (all.isEmpty()) { if (all.isEmpty()) {
return@withContext emptyList<Message>() return@withContext emptyList<Message>()
......
...@@ -46,7 +46,7 @@ class SharedPreferencesMessagesRepository( ...@@ -46,7 +46,7 @@ class SharedPreferencesMessagesRepository(
return@withContext values.mapNotNull { adapter.fromJson(it) } 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()) { if (prefs.all.values.isEmpty()) {
return@withContext emptyList<Message>() return@withContext emptyList<Message>()
} }
...@@ -56,7 +56,7 @@ class SharedPreferencesMessagesRepository( ...@@ -56,7 +56,7 @@ class SharedPreferencesMessagesRepository(
.filter { it.isTemporary ?: false } .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) val allByRoomId = getByRoomId(roomId)
if (allByRoomId.isEmpty()) { if (allByRoomId.isEmpty()) {
return@withContext emptyList<Message>() 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