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,6 +2413,24 @@ class ChatController(args: Bundle) :
adapter != null && adapter != null &&
adapter?.itemCount == 0 adapter?.itemCount == 0
modifyMessageCount(shouldAddNewMessagesNotice, shouldScroll)
adapter?.let {
chatMessage.isGrouped = (
it.isPreviousSameAuthor(
chatMessage.actorId,
-1
) && it.getSameAuthorLastMessagesCount(chatMessage.actorId) %
GROUPED_MESSAGES_SAME_AUTHOR_THRESHOLD > 0
)
chatMessage.isOneToOneConversation =
(currentConversation?.type == Conversation.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL)
it.addToStart(chatMessage, shouldScroll)
}
}
}
private fun modifyMessageCount(shouldAddNewMessagesNotice: Boolean, shouldScroll: Boolean) {
if (!shouldAddNewMessagesNotice && !shouldScroll) { if (!shouldAddNewMessagesNotice && !shouldScroll) {
if (!binding.popupBubbleView.isShown) { if (!binding.popupBubbleView.isShown) {
newMessagesCount = 1 newMessagesCount = 1
@ -2413,27 +2441,6 @@ class ChatController(args: Bundle) :
} else { } else {
newMessagesCount = 0 newMessagesCount = 0
} }
if (adapter != null) {
chatMessage.isGrouped = (
adapter!!.isPreviousSameAuthor(
chatMessage.actorId,
-1
) && adapter!!.getSameAuthorLastMessagesCount(chatMessage.actorId) %
GROUPED_MESSAGES_SAME_AUTHOR_THRESHOLD > 0
)
chatMessage.isOneToOneConversation =
(currentConversation?.type == Conversation.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL)
adapter?.addToStart(chatMessage, shouldScroll)
}
}
if (shouldAddNewMessagesNotice && adapter != null) {
layoutManager?.scrollToPositionWithOffset(
adapter!!.getMessagePositionByIdInReverse("-1"),
binding.messagesListView.height / 2
)
}
} }
private fun processMessagesNotFromTheFuture(chatMessageList: List<ChatMessage>) { private fun processMessagesNotFromTheFuture(chatMessageList: List<ChatMessage>) {