From 8158fff0f70ce86bd665732b9d8e1db9dc686e74 Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Tue, 3 May 2022 22:48:03 +0200 Subject: [PATCH] Still need to link empty chunks to glue everything together Change-Id: Ic037db94d044c7271fe2f016a58c153945b73615 --- .../room/timeline/TokenChunkEventPersistor.kt | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt index a14a2ad2de..8449e8205a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TokenChunkEventPersistor.kt @@ -182,6 +182,7 @@ internal class TokenChunkEventPersistor @Inject constructor( } val optimizedThreadSummaryMap = hashMapOf() var hasNewEvents = false + var existingChunkToLink: ChunkEntity? = null run processTimelineEvents@{ eventList.forEach { event -> if (event.eventId == null || event.senderId == null) { @@ -196,13 +197,6 @@ internal class TokenChunkEventPersistor @Inject constructor( // If it exists, we want to stop here, just link the prevChunk val existingChunk = existingTimelineEvent?.chunk?.firstOrNull() if (existingChunk != null) { - // If we haven't found a single new event yet, we don't want to link in the pagination direction, as that might cause a - // timeline loop if the other chunk is in the other direction. - if (!hasNewEvents) { - Timber.i("Skip adding event $eventId, already exists") - // Only skip this event, but still process other events - return@forEach - } val alreadyLinkedNext = currentChunk.doesNextChunksVerifyCondition { it == existingChunk } val alreadyLinkedPrev = currentChunk.doesPrevChunksVerifyCondition { it == existingChunk } if (alreadyLinkedNext || alreadyLinkedPrev) { @@ -217,6 +211,18 @@ internal class TokenChunkEventPersistor @Inject constructor( // Stop processing here return@processTimelineEvents } + // If we haven't found a single new event yet, we don't want to link in the pagination direction, as that might cause a + // timeline loop if the other chunk is in the other direction. + if (!hasNewEvents) { + Timber.i("Skip adding event $eventId, already exists") + // Only skip this event, but still process other events. + // Remember this chunk, since in case we don't find any new events, we still want to link this in pagination direction + // in order to link a chunk to the /sync chunk + if (existingChunkToLink == null) { + existingChunkToLink = existingChunk + } + return@forEach + } when (direction) { PaginationDirection.BACKWARDS -> { Timber.i("Backwards insert chunk: ${existingChunk.identifier()} -> ${currentChunk.identifier()}") @@ -263,6 +269,22 @@ internal class TokenChunkEventPersistor @Inject constructor( } } } + val existingChunk = existingChunkToLink + if (!hasNewEvents && existingChunk != null) { + when (direction) { + PaginationDirection.BACKWARDS -> { + Timber.i("Backwards insert chunk: ${existingChunk.identifier()} -> ${currentChunk.identifier()}") + currentChunk.prevChunk = existingChunk + existingChunk.nextChunk = currentChunk + } + PaginationDirection.FORWARDS -> { + Timber.i("Forward insert chunk: ${currentChunk.identifier()} -> ${existingChunk.identifier()}") + currentChunk.nextChunk = existingChunk + existingChunk.prevChunk = currentChunk + } + + } + } if (currentChunk.isValid) { RoomEntity.where(realm, roomId).findFirst()?.addIfNecessary(currentChunk) }