diff --git a/libraries/mediaviewer/impl/src/main/kotlin/io/element/android/libraries/mediaviewer/impl/gallery/TimelineMediaItemsFactory.kt b/libraries/mediaviewer/impl/src/main/kotlin/io/element/android/libraries/mediaviewer/impl/gallery/TimelineMediaItemsFactory.kt index 1993381417..d8531cf230 100644 --- a/libraries/mediaviewer/impl/src/main/kotlin/io/element/android/libraries/mediaviewer/impl/gallery/TimelineMediaItemsFactory.kt +++ b/libraries/mediaviewer/impl/src/main/kotlin/io/element/android/libraries/mediaviewer/impl/gallery/TimelineMediaItemsFactory.kt @@ -8,12 +8,12 @@ package io.element.android.libraries.mediaviewer.impl.gallery import com.squareup.anvil.annotations.ContributesBinding +import io.element.android.libraries.androidutils.diff.DefaultDiffCacheInvalidator import io.element.android.libraries.androidutils.diff.DiffCacheUpdater import io.element.android.libraries.androidutils.diff.MutableListDiffCache import io.element.android.libraries.core.coroutine.CoroutineDispatchers import io.element.android.libraries.di.AppScope import io.element.android.libraries.matrix.api.timeline.MatrixTimelineItem -import io.element.android.libraries.mediaviewer.impl.gallery.diff.TimelineMediaItemsCacheInvalidator import io.element.android.services.toolbox.api.systemclock.SystemClock import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.toPersistentList @@ -46,7 +46,7 @@ class DefaultTimelineMediaItemsFactory @Inject constructor( private val diffCacheUpdater = DiffCacheUpdater( diffCache = diffCache, detectMoves = false, - cacheInvalidator = TimelineMediaItemsCacheInvalidator() + cacheInvalidator = DefaultDiffCacheInvalidator() ) { old, new -> if (old is MatrixTimelineItem.Event && new is MatrixTimelineItem.Event) { old.uniqueId == new.uniqueId diff --git a/libraries/mediaviewer/impl/src/main/kotlin/io/element/android/libraries/mediaviewer/impl/gallery/diff/TimelineMediaItemsCacheInvalidator.kt b/libraries/mediaviewer/impl/src/main/kotlin/io/element/android/libraries/mediaviewer/impl/gallery/diff/TimelineMediaItemsCacheInvalidator.kt deleted file mode 100644 index b7e6d51913..0000000000 --- a/libraries/mediaviewer/impl/src/main/kotlin/io/element/android/libraries/mediaviewer/impl/gallery/diff/TimelineMediaItemsCacheInvalidator.kt +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2024 New Vector Ltd. - * - * SPDX-License-Identifier: AGPL-3.0-only - * Please see LICENSE in the repository root for full details. - */ - -package io.element.android.libraries.mediaviewer.impl.gallery.diff - -import io.element.android.libraries.androidutils.diff.DefaultDiffCacheInvalidator -import io.element.android.libraries.androidutils.diff.DiffCacheInvalidator -import io.element.android.libraries.androidutils.diff.MutableDiffCache -import io.element.android.libraries.mediaviewer.impl.gallery.MediaItem - -/** - * [DiffCacheInvalidator] implementation for [MediaItem]. - * It uses [DefaultDiffCacheInvalidator] and invalidate the cache around the updated item so that those items are computed again. - * This is needed because a timeline item is computed based on the previous and next items. - */ -internal class TimelineMediaItemsCacheInvalidator : DiffCacheInvalidator { - private val delegate = DefaultDiffCacheInvalidator() - - override fun onChanged(position: Int, count: Int, cache: MutableDiffCache) { - delegate.onChanged(position, count, cache) - } - - override fun onMoved(fromPosition: Int, toPosition: Int, cache: MutableDiffCache) { - delegate.onMoved(fromPosition, toPosition, cache) - } - - override fun onInserted(position: Int, count: Int, cache: MutableDiffCache) { - cache.invalidateAround(position) - delegate.onInserted(position, count, cache) - } - - override fun onRemoved(position: Int, count: Int, cache: MutableDiffCache) { - cache.invalidateAround(position) - delegate.onRemoved(position, count, cache) - } -} - -/** - * Invalidate the cache around the given position. - * It invalidates the previous and next items. - */ -private fun MutableDiffCache<*>.invalidateAround(position: Int) { - if (position > 0) { - set(position - 1, null) - } - if (position < indices().last) { - set(position + 1, null) - } -}