Reduce complexity for 'ChatController#processMessagesFromTheFuture'

Signed-off-by: Tim Krüger <t@timkrueger.me>
This commit is contained in:
Tim Krüger 2022-09-07 12:06:14 +02:00
parent d0df4039c6
commit ae0a9d6aae
No known key found for this signature in database
GPG key ID: FECE3A7222C52A4E

View file

@ -2373,7 +2373,6 @@ class ChatController(args: Bundle) :
} }
private fun processMessagesFromTheFuture(chatMessageList: List<ChatMessage>) { private fun processMessagesFromTheFuture(chatMessageList: List<ChatMessage>) {
var chatMessage: ChatMessage
val shouldAddNewMessagesNotice = (adapter?.itemCount ?: 0) > 0 && chatMessageList.isNotEmpty() val shouldAddNewMessagesNotice = (adapter?.itemCount ?: 0) > 0 && chatMessageList.isNotEmpty()
@ -2386,14 +2385,25 @@ class ChatController(args: Bundle) :
adapter?.addToStart(unreadChatMessage, false) adapter?.addToStart(unreadChatMessage, false)
} }
val isThereANewNotice =
shouldAddNewMessagesNotice || adapter?.getMessagePositionByIdInReverse("-1") != -1
determinePreviousMessageIds(chatMessageList) determinePreviousMessageIds(chatMessageList)
for (i in chatMessageList.indices) { addMessagesToAdapter(shouldAddNewMessagesNotice, chatMessageList)
chatMessage = chatMessageList[i]
if (shouldAddNewMessagesNotice && adapter != null) {
layoutManager?.scrollToPositionWithOffset(
adapter!!.getMessagePositionByIdInReverse("-1"),
binding.messagesListView.height / 2
)
}
}
private fun addMessagesToAdapter(
shouldAddNewMessagesNotice: Boolean,
chatMessageList: List<ChatMessage>
) {
val isThereANewNotice =
shouldAddNewMessagesNotice || adapter?.getMessagePositionByIdInReverse("-1") != -1
for (chatMessage in chatMessageList) {
chatMessage.activeUser = conversationUser chatMessage.activeUser = conversationUser
val shouldScroll = val shouldScroll =
@ -2403,36 +2413,33 @@ class ChatController(args: Bundle) :
adapter != null && adapter != null &&
adapter?.itemCount == 0 adapter?.itemCount == 0
if (!shouldAddNewMessagesNotice && !shouldScroll) { modifyMessageCount(shouldAddNewMessagesNotice, shouldScroll)
if (!binding.popupBubbleView.isShown) {
newMessagesCount = 1
binding.popupBubbleView.show()
} else if (binding.popupBubbleView.isShown) {
newMessagesCount++
}
} else {
newMessagesCount = 0
}
if (adapter != null) { adapter?.let {
chatMessage.isGrouped = ( chatMessage.isGrouped = (
adapter!!.isPreviousSameAuthor( it.isPreviousSameAuthor(
chatMessage.actorId, chatMessage.actorId,
-1 -1
) && adapter!!.getSameAuthorLastMessagesCount(chatMessage.actorId) % ) && it.getSameAuthorLastMessagesCount(chatMessage.actorId) %
GROUPED_MESSAGES_SAME_AUTHOR_THRESHOLD > 0 GROUPED_MESSAGES_SAME_AUTHOR_THRESHOLD > 0
) )
chatMessage.isOneToOneConversation = chatMessage.isOneToOneConversation =
(currentConversation?.type == Conversation.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL) (currentConversation?.type == Conversation.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL)
adapter?.addToStart(chatMessage, shouldScroll) it.addToStart(chatMessage, shouldScroll)
} }
} }
}
if (shouldAddNewMessagesNotice && adapter != null) { private fun modifyMessageCount(shouldAddNewMessagesNotice: Boolean, shouldScroll: Boolean) {
layoutManager?.scrollToPositionWithOffset( if (!shouldAddNewMessagesNotice && !shouldScroll) {
adapter!!.getMessagePositionByIdInReverse("-1"), if (!binding.popupBubbleView.isShown) {
binding.messagesListView.height / 2 newMessagesCount = 1
) binding.popupBubbleView.show()
} else if (binding.popupBubbleView.isShown) {
newMessagesCount++
}
} else {
newMessagesCount = 0
} }
} }