From 9a14764321354258d2d750e0e0d8d6c4c13a9cc5 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Wed, 11 Sep 2024 17:05:07 +0200 Subject: [PATCH] do not try to pull chat messages when offline or paused Otherwise, it resulted in a lot of flickering because _lastCommonReadFlow was emitted every 500ms. And anyway if we know we are offline or paused then it doesn't make sense to execute the sync method. chain which caused the flickering was: updateUiForLastCommonRead (_lastCommonReadFlow) -> updateReadStatusOfAllMessages - notifyDataSetChanged -> onBindViewHolder -> IncomingLinkPreviewMessageViewHolder Signed-off-by: Marcel Hibbe --- .../network/OfflineFirstChatRepository.kt | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 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 2da9ae6fe..e7a49ac41 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 @@ -199,29 +199,31 @@ class OfflineFirstChatRepository @Inject constructor( val networkParams = Bundle() while (true) { - if (!monitor.isOnline.first() || itIsPaused) Thread.sleep(HALF_SECOND) + if (!monitor.isOnline.first() || itIsPaused) { + Thread.sleep(HALF_SECOND) + } else { + // sync database with server (This is a long blocking call because long polling (lookIntoFuture) is set) + networkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap) - // sync database with server (This is a long blocking call because long polling (lookIntoFuture) is set) - networkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap) + val resultsFromSync = sync(networkParams) + if (!resultsFromSync.isNullOrEmpty()) { + val chatMessages = resultsFromSync.map(ChatMessageEntity::asModel) + val pair = Pair(true, chatMessages) + _messageFlow.emit(pair) + } - val resultsFromSync = sync(networkParams) - if (!resultsFromSync.isNullOrEmpty()) { - val chatMessages = resultsFromSync.map(ChatMessageEntity::asModel) - val pair = Pair(true, chatMessages) - _messageFlow.emit(pair) + updateUiForLastCommonRead() + + val newestMessage = chatDao.getNewestMessageId(internalConversationId).toInt() + + // update field map vars for next cycle + fieldMap = getFieldMap( + lookIntoFuture = true, + includeLastKnown = false, + setReadMarker = true, + lastKnown = newestMessage + ) } - - updateUiForLastCommonRead() - - val newestMessage = chatDao.getNewestMessageId(internalConversationId).toInt() - - // update field map vars for next cycle - fieldMap = getFieldMap( - lookIntoFuture = true, - includeLastKnown = false, - setReadMarker = true, - lastKnown = newestMessage - ) } }