diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TimelineChunk.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TimelineChunk.kt index 8507b63d1f..25957de1b5 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TimelineChunk.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/timeline/TimelineChunk.kt @@ -90,8 +90,7 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity, private val timelineEventsChangeListener = OrderedRealmCollectionChangeListener { results: RealmResults, changeSet: OrderedCollectionChangeSet -> Timber.v("on timeline events chunk update") - val frozenResults = results.freeze() - handleDatabaseChangeSet(frozenResults, changeSet) + handleDatabaseChangeSet(results, changeSet) } private var timelineEventEntities: RealmResults = chunkEntity.sortedTimelineEvents(timelineSettings.rootThreadEventId) @@ -287,7 +286,7 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity, * @return the number of events loaded. If we are in a thread timeline it also returns * whether or not we reached the end/root message */ - private suspend fun loadFromStorage(count: Int, direction: Timeline.Direction): LoadedFromStorage { + private fun loadFromStorage(count: Int, direction: Timeline.Direction): LoadedFromStorage { val displayIndex = getNextDisplayIndex(direction) ?: return LoadedFromStorage() val baseQuery = timelineEventEntities.where() @@ -428,10 +427,10 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity, * This method is responsible for managing insertions and updates of events on this chunk. * */ - private fun handleDatabaseChangeSet(frozenResults: RealmResults, changeSet: OrderedCollectionChangeSet) { + private fun handleDatabaseChangeSet(results: RealmResults, changeSet: OrderedCollectionChangeSet) { val insertions = changeSet.insertionRanges for (range in insertions) { - val newItems = frozenResults + val newItems = results .subList(range.startIndex, range.startIndex + range.length) .map { it.buildAndDecryptIfNeeded() } builtEventsIndexes.entries.filter { it.value >= range.startIndex }.forEach { it.setValue(it.value + range.length) } @@ -447,7 +446,7 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity, val modifications = changeSet.changeRanges for (range in modifications) { for (modificationIndex in (range.startIndex until range.startIndex + range.length)) { - val updatedEntity = frozenResults[modificationIndex] ?: continue + val updatedEntity = results[modificationIndex] ?: continue try { builtEvents[modificationIndex] = updatedEntity.buildAndDecryptIfNeeded() } catch (failure: Throwable) { @@ -458,20 +457,20 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity, if (insertions.isNotEmpty() || modifications.isNotEmpty()) { onBuiltEvents(true) } + } private fun getNextDisplayIndex(direction: Timeline.Direction): Int? { - val frozenTimelineEvents = timelineEventEntities.freeze() - if (frozenTimelineEvents.isEmpty()) { + if (timelineEventEntities.isEmpty()) { return null } return if (builtEvents.isEmpty()) { if (initialEventId != null) { - frozenTimelineEvents.where().equalTo(TimelineEventEntityFields.EVENT_ID, initialEventId).findFirst()?.displayIndex + timelineEventEntities.where().equalTo(TimelineEventEntityFields.EVENT_ID, initialEventId).findFirst()?.displayIndex } else if (direction == Timeline.Direction.BACKWARDS) { - frozenTimelineEvents.first(null)?.displayIndex + timelineEventEntities.first(null)?.displayIndex } else { - frozenTimelineEvents.last(null)?.displayIndex + timelineEventEntities.last(null)?.displayIndex } } else if (direction == Timeline.Direction.FORWARDS) { builtEvents.first().displayIndex + 1