From 7eb8b9fa707e024afaf2fee35c0bdbdc0fa73bdb Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Wed, 16 Oct 2024 15:20:23 +0200 Subject: [PATCH] extract getCappedMessagesAmountOfChatBlock Signed-off-by: Marcel Hibbe --- .../network/OfflineFirstChatRepository.kt | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt b/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt index 80fd70c84..bb3b2dfc2 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt @@ -160,21 +160,7 @@ class OfflineFirstChatRepository @Inject constructor( Log.d(TAG, "Initial online request is skipped because offline messages are up to date") } - val chatBlock = getBlockOfMessage(newestMessageIdFromDb.toInt()) - - val amountBetween = chatDao.getCountBetweenMessageIds( - internalConversationId, - newestMessageIdFromDb, - chatBlock!!.oldestMessageId - ) - - Log.d(TAG, "amount of messages between newestMessageId and oldest message of same ChatBlock:$amountBetween") - val limit = if (amountBetween > DEFAULT_MESSAGES_LIMIT) { - DEFAULT_MESSAGES_LIMIT - } else { - amountBetween - } - Log.d(TAG, "limit of messages to load for UI (max 100 to ensure performance is okay):$limit") + val limit = getCappedMessagesAmountOfChatBlock(newestMessageIdFromDb) showMessagesBeforeAndEqual( internalConversationId, @@ -192,6 +178,25 @@ class OfflineFirstChatRepository @Inject constructor( initMessagePolling() } + private suspend fun getCappedMessagesAmountOfChatBlock(messageId: Long): Int { + val chatBlock = getBlockOfMessage(messageId.toInt()) + + val amountBetween = chatDao.getCountBetweenMessageIds( + internalConversationId, + messageId, + chatBlock!!.oldestMessageId + ) + + Log.d(TAG, "amount of messages between newestMessageId and oldest message of same ChatBlock:$amountBetween") + val limit = if (amountBetween > DEFAULT_MESSAGES_LIMIT) { + DEFAULT_MESSAGES_LIMIT + } else { + amountBetween + } + Log.d(TAG, "limit of messages to load for UI (max 100 to ensure performance is okay):$limit") + return limit + } + private suspend fun updateUiForLastReadMessage(newestMessageId: Long) { val scrollToLastRead = conversationModel.lastReadMessage.toLong() < newestMessageId if (scrollToLastRead) {