Skip to content
This repository was archived by the owner on Jun 7, 2020. It is now read-only.

Commit 62d12ce

Browse files
authored
Merge pull request #2337 from Shailesh351/sb_fix_rendering_bug
[FIX] Messages rendering out of order bug
2 parents 65eef62 + a821e89 commit 62d12ce

3 files changed

Lines changed: 37 additions & 10 deletions

File tree

app/src/main/java/chat/rocket/android/chatroom/adapter/ChatRoomAdapter.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ class ChatRoomAdapter(
173173
notifyDataSetChanged()
174174
}
175175

176-
fun updateItem(message: BaseUiModel<*>): Boolean {
176+
// FIXME What's 0,1 and 2 means for here?
177+
fun updateItem(message: BaseUiModel<*>): Int {
177178
val index = dataSet.indexOfLast { it.messageId == message.messageId }
178179
val indexOfNext = dataSet.indexOfFirst { it.messageId == message.messageId }
179180
Timber.d("index: $index")
@@ -184,17 +185,23 @@ class ChatRoomAdapter(
184185
if (viewModel.nextDownStreamMessage == null) {
185186
viewModel.reactions = message.reactions
186187
}
187-
notifyItemChanged(ind)
188+
189+
if (ind > 0 &&
190+
dataSet[ind].message.timestamp > dataSet[ind - 1].message.timestamp) {
191+
return 2
192+
} else {
193+
notifyItemChanged(ind)
194+
}
188195
}
189196
}
190197
// Delete message only if current is a system message update, i.e.: Message Removed
191198
if (message.message.isSystemMessage() && indexOfNext > -1 && indexOfNext != index) {
192199
dataSet.removeAt(indexOfNext)
193200
notifyItemRemoved(indexOfNext)
194201
}
195-
return true
202+
return 0
196203
}
197-
return false
204+
return 1
198205
}
199206

200207
fun removeItem(messageId: String) {

app/src/main/java/chat/rocket/android/chatroom/presentation/ChatRoomPresenter.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,13 @@ class ChatRoomPresenter @Inject constructor(
359359
val id = UUID.randomUUID().toString()
360360
val username = userHelper.username()
361361
val user = userHelper.user()
362+
val timestamp = maxOf(getTimeStampOfLastMessageInRoom() + 1,
363+
Instant.now().toEpochMilli())
362364
val newMessage = Message(
363365
id = id,
364366
roomId = chatRoomId,
365367
message = text,
366-
timestamp = Instant.now().toEpochMilli(),
368+
timestamp = timestamp,
367369
sender = SimpleUser(user?.id, user?.username ?: username, user?.name),
368370
attachments = null,
369371
avatar = currentServer.avatarUrl(
@@ -1389,4 +1391,12 @@ class ChatRoomPresenter @Inject constructor(
13891391
fun getDraftUnfinishedMessage(): String? {
13901392
return localRepository.get(draftKey)
13911393
}
1394+
1395+
private suspend fun getTimeStampOfLastMessageInRoom(): Long {
1396+
return withContext(Dispatchers.IO + strategy.jobs) {
1397+
chatRoomId?.let {
1398+
dbManager.messageDao().getRecentMessagesByRoomId(it, 1).first().message.message.timestamp
1399+
}
1400+
} ?: 0
1401+
}
13921402
}

app/src/main/java/chat/rocket/android/chatroom/ui/ChatRoomFragment.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,22 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
553553
// TODO - investigate WHY we get a empty list here
554554
if (message.isEmpty()) return@ui
555555

556-
if (chatRoomAdapter.updateItem(message.last())) {
557-
if (message.size > 1) {
558-
chatRoomAdapter.prependData(listOf(message.first()))
556+
when (chatRoomAdapter.updateItem(message.last())) {
557+
// FIXME: What's 0,1 and 2 means for here?
558+
0 -> {
559+
if (message.size > 1) {
560+
chatRoomAdapter.prependData(listOf(message.first()))
561+
}
562+
}
563+
1 -> showNewMessage(message, true)
564+
2 -> {
565+
// Position of new sent message is wrong because of device local time is behind server time
566+
with(chatRoomAdapter) {
567+
removeItem(message.last().messageId)
568+
prependData(listOf(message.last()))
569+
notifyDataSetChanged()
570+
}
559571
}
560-
} else {
561-
showNewMessage(message, true)
562572
}
563573
dismissEmojiKeyboard()
564574
}

0 commit comments

Comments
 (0)