Ensure gallery is paginating to get new items.

This commit is contained in:
Benoit Marty 2025-01-23 09:26:10 +01:00
parent cb5988935f
commit 4c6f46e46c
4 changed files with 33 additions and 28 deletions

View file

@ -198,6 +198,13 @@ private fun MediaGalleryPage(
) {
val groupedMediaItems = state.groupedMediaItems
if (groupedMediaItems.isLoadingItems(mode)) {
// Need to trigger a pagination now if there is only one LoadingIndicator.
(groupedMediaItems.dataOrNull()
?.getItems(mode)?.singleOrNull() as? MediaItem.LoadingIndicator)?.let { item ->
LaunchedEffect(item.timestamp) {
state.eventSink(MediaGalleryEvents.LoadMore(item.direction))
}
}
LoadingContent(mode)
} else {
when (groupedMediaItems) {

View file

@ -71,14 +71,26 @@ class MediaViewerDataSource(
fun dataFlow(): Flow<PersistentList<MediaViewerPageData>> {
return galleryDataSource.groupedMediaItemsFlow()
.map { groupedItems ->
if (groupedItems is AsyncData.Failure) {
persistentListOf(
MediaViewerPageData.Failure(groupedItems.error),
)
} else {
val mediaItems = groupedItems.dataOrNull()?.getItems(galleryMode).orEmpty()
withContext(dispatcher) {
buildMediaViewerPageList(mediaItems)
when (groupedItems) {
AsyncData.Uninitialized,
is AsyncData.Loading -> {
persistentListOf(
MediaViewerPageData.Loading(
direction = Timeline.PaginationDirection.BACKWARDS,
timestamp = systemClock.epochMillis(),
)
)
}
is AsyncData.Failure -> {
persistentListOf(
MediaViewerPageData.Failure(groupedItems.error),
)
}
is AsyncData.Success -> {
withContext(dispatcher) {
val mediaItems = groupedItems.data.getItems(galleryMode)
buildMediaViewerPageList(mediaItems)
}
}
}
}
@ -117,14 +129,6 @@ class MediaViewerDataSource(
)
}
}
if (isEmpty()) {
add(
MediaViewerPageData.Loading(
direction = Timeline.PaginationDirection.BACKWARDS,
timestamp = systemClock.epochMillis(),
)
)
}
}.toPersistentList()
fun clearLoadingError(data: MediaViewerPageData.MediaViewerData) {