Media timeline: improve pagination logic.
This commit is contained in:
parent
d86430d56a
commit
a4a8fc2268
5 changed files with 23 additions and 73 deletions
|
|
@ -183,14 +183,6 @@ class MediaGalleryPresenter @AssistedInject constructor(
|
|||
}
|
||||
.launchIn(this)
|
||||
|
||||
timeline.data.paginationStatus(Timeline.PaginationDirection.BACKWARDS)
|
||||
.onEach { backwardPaginationStatus ->
|
||||
if (backwardPaginationStatus.canPaginate) {
|
||||
timelineMediaItemsFactory.onCanPaginate()
|
||||
}
|
||||
}
|
||||
.launchIn(this)
|
||||
|
||||
timelineMediaItemsFactory.timelineItems.map { timelineItems ->
|
||||
AsyncData.Success(timelineItems)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ import io.element.android.libraries.androidutils.diff.MutableListDiffCache
|
|||
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
|
||||
import io.element.android.libraries.di.RoomScope
|
||||
import io.element.android.libraries.matrix.api.timeline.MatrixTimelineItem
|
||||
import io.element.android.services.toolbox.api.systemclock.SystemClock
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
import kotlinx.collections.immutable.toPersistentList
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
|
@ -23,14 +22,11 @@ import kotlinx.coroutines.flow.distinctUntilChanged
|
|||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
interface TimelineMediaItemsFactory {
|
||||
val timelineItems: Flow<ImmutableList<MediaItem>>
|
||||
|
||||
suspend fun replaceWith(timelineItems: List<MatrixTimelineItem>)
|
||||
suspend fun onCanPaginate()
|
||||
}
|
||||
|
||||
@ContributesBinding(RoomScope::class)
|
||||
|
|
@ -38,7 +34,6 @@ class DefaultTimelineMediaItemsFactory @Inject constructor(
|
|||
private val dispatchers: CoroutineDispatchers,
|
||||
private val virtualItemFactory: VirtualItemFactory,
|
||||
private val eventItemFactory: EventItemFactory,
|
||||
private val systemClock: SystemClock,
|
||||
) : TimelineMediaItemsFactory {
|
||||
private val _timelineItems = MutableSharedFlow<ImmutableList<MediaItem>>(replay = 1)
|
||||
private val lock = Mutex()
|
||||
|
|
@ -66,26 +61,6 @@ class DefaultTimelineMediaItemsFactory @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the timestamp of the loading indicator, so that it may trigger a new pagination request.
|
||||
*/
|
||||
override suspend fun onCanPaginate() {
|
||||
lock.withLock {
|
||||
val values = _timelineItems.replayCache.firstOrNull() ?: return@withLock
|
||||
val lastItem = values.lastOrNull()
|
||||
if (lastItem is MediaItem.LoadingIndicator) {
|
||||
val newList = values.toMutableList().apply {
|
||||
removeAt(size - 1)
|
||||
val newTs = systemClock.epochMillis()
|
||||
add(lastItem.copy(timestamp = newTs))
|
||||
}
|
||||
_timelineItems.emit(newList.toPersistentList())
|
||||
} else {
|
||||
Timber.w("onCanPaginate called but last item is not a loading indicator")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun buildAndEmitTimelineItemStates(
|
||||
timelineItems: List<MatrixTimelineItem>,
|
||||
) {
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ import kotlinx.coroutines.flow.flowOf
|
|||
|
||||
class FakeTimelineMediaItemsFactory(
|
||||
private val replaceWithLambda: (List<MatrixTimelineItem>) -> Unit = { lambdaError() },
|
||||
private val onCanPaginateLambda: () -> Unit = { lambdaError() }
|
||||
) : TimelineMediaItemsFactory {
|
||||
override val timelineItems: Flow<ImmutableList<MediaItem>>
|
||||
get() = flowOf(emptyList<MediaItem>().toImmutableList())
|
||||
|
|
@ -24,8 +23,4 @@ class FakeTimelineMediaItemsFactory(
|
|||
override suspend fun replaceWith(timelineItems: List<MatrixTimelineItem>) {
|
||||
replaceWithLambda(timelineItems)
|
||||
}
|
||||
|
||||
override suspend fun onCanPaginate() {
|
||||
onCanPaginateLambda()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -247,7 +247,6 @@ class MediaGalleryPresenterTest {
|
|||
room = room,
|
||||
timelineMediaItemsFactory = FakeTimelineMediaItemsFactory(
|
||||
replaceWithLambda = lambdaRecorder<List<MatrixTimelineItem>, Unit> { _ -> },
|
||||
onCanPaginateLambda = lambdaRecorder<Unit> { },
|
||||
),
|
||||
localMediaFactory = localMediaFactory,
|
||||
mediaLoader = matrixMediaLoader,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue