From 3d37cc083190f6064639e43ede48db99360733a8 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Wed, 9 Oct 2024 16:21:53 +0200 Subject: [PATCH] only do initial request if newestMessageIdFromDb is lower than lastReadMessage from conversation If conversation has a newer message id than DB then an online request is necessary If conversation has an older message id than DB then an online request is not necessary (this could happen when updating of DB is implemented for push notification, not yet done). If conversation has the same message id like DB than request can be skipped Signed-off-by: Marcel Hibbe --- .../network/OfflineFirstChatRepository.kt | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 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 262188b03..80fd70c84 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 @@ -114,13 +114,28 @@ class OfflineFirstChatRepository @Inject constructor( Log.d(TAG, "conversationModel.lastReadMessage:" + conversationModel.lastReadMessage) - var newestMessageId = chatDao.getNewestMessageId(internalConversationId) - Log.d(TAG, "newestMessageId: $newestMessageId") - if (newestMessageId.toInt() == 0) { + var newestMessageIdFromDb = chatDao.getNewestMessageId(internalConversationId) + Log.d(TAG, "newestMessageId: $newestMessageIdFromDb") + if (newestMessageIdFromDb.toInt() == 0) { Log.d(TAG, "newestMessageId from db was 0. Must only happen when chat is loaded for the first time") } - if (conversationModel.lastReadMessage.toLong() != newestMessageId) { + // infos from Ivan to + // "Why is it lastReadMessageId that is checked? Shouldn't it be lastMessage instead? + // https://github.com/nextcloud/talk-ios/blob/master/NextcloudTalk/NCChatController.m#L473 " + // + // answer: + // "I guess we do it with the lastReadMessageId in order to place the separator of "Unread messages" when you enter the chat" + // + // if it turns out lastMessage can be used instead lastReadMessage, use this: + // val doInitialLoadFromServer = conversationModel.lastMessage?.let { + // newestMessageIdFromDb < it.id + // } ?: true + // Log.d(TAG, "doInitialLoadFromServer:$doInitialLoadFromServer") + // + // if (doInitialLoadFromServer) { + + if (newestMessageIdFromDb < conversationModel.lastReadMessage.toLong()) { Log.d(TAG, "An online request is made because chat is not up to date") // set up field map to load the newest messages @@ -139,17 +154,17 @@ class OfflineFirstChatRepository @Inject constructor( Log.e(TAG, "initial loading of messages failed") } - newestMessageId = chatDao.getNewestMessageId(internalConversationId) - Log.d(TAG, "newestMessageId after sync: $newestMessageId") + newestMessageIdFromDb = chatDao.getNewestMessageId(internalConversationId) + Log.d(TAG, "newestMessageId after sync: $newestMessageIdFromDb") } else { Log.d(TAG, "Initial online request is skipped because offline messages are up to date") } - val chatBlock = getBlockOfMessage(newestMessageId.toInt()) + val chatBlock = getBlockOfMessage(newestMessageIdFromDb.toInt()) val amountBetween = chatDao.getCountBetweenMessageIds( internalConversationId, - newestMessageId, + newestMessageIdFromDb, chatBlock!!.oldestMessageId ) @@ -163,7 +178,7 @@ class OfflineFirstChatRepository @Inject constructor( showMessagesBeforeAndEqual( internalConversationId, - newestMessageId, + newestMessageIdFromDb, limit ) @@ -172,7 +187,7 @@ class OfflineFirstChatRepository @Inject constructor( delay(DELAY_TO_ENSURE_MESSAGES_ARE_ADDED) updateUiForLastCommonRead() - updateUiForLastReadMessage(newestMessageId) + updateUiForLastReadMessage(newestMessageIdFromDb) initMessagePolling() }