Fixed scrolling to original message when clicking on reply if it hasn't been loaded yet

This commit is contained in:
Sylvain Berfini 2022-07-04 15:44:29 +02:00
parent 1c8705933f
commit 0f840e0aa3

View file

@ -493,21 +493,47 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
viewLifecycleOwner viewLifecycleOwner
) { ) {
it.consume { chatMessage -> it.consume { chatMessage ->
var index = 0
var retryCount = 0
var expectedChildCount = 0
do {
val events = listViewModel.events.value.orEmpty() val events = listViewModel.events.value.orEmpty()
expectedChildCount = events.size
Log.e("[Chat Room] expectedChildCount : $expectedChildCount")
val eventLog = events.find { eventLog -> val eventLog = events.find { eventLog ->
if (eventLog.eventLog.type == EventLog.Type.ConferenceChatMessage) { if (eventLog.eventLog.type == EventLog.Type.ConferenceChatMessage) {
(eventLog.data as ChatMessageData).chatMessage.messageId == chatMessage.messageId (eventLog.data as ChatMessageData).chatMessage.messageId == chatMessage.messageId
} else false } else false
} }
val index = events.indexOf(eventLog) index = events.indexOf(eventLog)
try { if (index == -1) {
if (corePreferences.enableAnimations) { retryCount += 1
binding.chatMessagesList.smoothScrollToPosition(index) listViewModel.loadMoreData(events.size)
} else {
binding.chatMessagesList.scrollToPosition(index)
} }
} catch (iae: IllegalArgumentException) { } while (index == -1 && retryCount < 5)
Log.e("[Chat Room] Can't scroll to position $index")
if (index != -1) {
if (retryCount == 0) {
scrollTo(index, true)
} else {
lifecycleScope.launch {
withContext(Dispatchers.Default) {
val layoutManager = binding.chatMessagesList.layoutManager as LinearLayoutManager
var retryCount = 0
do {
// We have to wait for newly loaded items to be added to list before being able to scroll
delay(500)
retryCount += 1
} while (layoutManager.itemCount != expectedChildCount && retryCount < 5)
withContext(Dispatchers.Main) {
scrollTo(index, true)
}
}
}
}
} else {
Log.w("[Chat Room] Failed to find matching event!")
} }
} }
} }
@ -1041,11 +1067,7 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
} }
Log.i("[Chat Room] Scrolling to position $indexToScrollTo, first unread message is at $firstUnreadMessagePosition") Log.i("[Chat Room] Scrolling to position $indexToScrollTo, first unread message is at $firstUnreadMessagePosition")
if (smooth && corePreferences.enableAnimations) { scrollTo(indexToScrollTo, smooth)
recyclerView.smoothScrollToPosition(indexToScrollTo)
} else {
recyclerView.scrollToPosition(indexToScrollTo)
}
if (firstUnreadMessagePosition == 0) { if (firstUnreadMessagePosition == 0) {
// Return true only if all unread messages don't fit in the recyclerview height // Return true only if all unread messages don't fit in the recyclerview height
@ -1178,4 +1200,16 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
dialog.show() dialog.show()
} }
private fun scrollTo(position: Int, smooth: Boolean = true) {
try {
if (smooth && corePreferences.enableAnimations) {
binding.chatMessagesList.smoothScrollToPosition(position)
} else {
binding.chatMessagesList.scrollToPosition(position)
}
} catch (iae: IllegalArgumentException) {
Log.e("[Chat Room] Can't scroll to position $position")
}
}
} }