Fixed scrolling to original message when clicking on reply if it hasn't been loaded yet
This commit is contained in:
parent
1c8705933f
commit
0f840e0aa3
1 changed files with 53 additions and 19 deletions
|
@ -493,21 +493,47 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
|
||||||
viewLifecycleOwner
|
viewLifecycleOwner
|
||||||
) {
|
) {
|
||||||
it.consume { chatMessage ->
|
it.consume { chatMessage ->
|
||||||
val events = listViewModel.events.value.orEmpty()
|
var index = 0
|
||||||
val eventLog = events.find { eventLog ->
|
var retryCount = 0
|
||||||
if (eventLog.eventLog.type == EventLog.Type.ConferenceChatMessage) {
|
var expectedChildCount = 0
|
||||||
(eventLog.data as ChatMessageData).chatMessage.messageId == chatMessage.messageId
|
do {
|
||||||
} else false
|
val events = listViewModel.events.value.orEmpty()
|
||||||
}
|
expectedChildCount = events.size
|
||||||
val index = events.indexOf(eventLog)
|
Log.e("[Chat Room] expectedChildCount : $expectedChildCount")
|
||||||
try {
|
val eventLog = events.find { eventLog ->
|
||||||
if (corePreferences.enableAnimations) {
|
if (eventLog.eventLog.type == EventLog.Type.ConferenceChatMessage) {
|
||||||
binding.chatMessagesList.smoothScrollToPosition(index)
|
(eventLog.data as ChatMessageData).chatMessage.messageId == chatMessage.messageId
|
||||||
} else {
|
} else false
|
||||||
binding.chatMessagesList.scrollToPosition(index)
|
|
||||||
}
|
}
|
||||||
} catch (iae: IllegalArgumentException) {
|
index = events.indexOf(eventLog)
|
||||||
Log.e("[Chat Room] Can't scroll to position $index")
|
if (index == -1) {
|
||||||
|
retryCount += 1
|
||||||
|
listViewModel.loadMoreData(events.size)
|
||||||
|
}
|
||||||
|
} while (index == -1 && retryCount < 5)
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue