From f301eab5b124e4f66841660bf21f374f475e743a Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Wed, 23 Oct 2024 00:28:49 +0800 Subject: [PATCH] Fix preview list not recomposing on detail refresh --- .../ui/screen/GalleryDetailContent.kt | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailContent.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailContent.kt index 2da54a673..811e26ca7 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailContent.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailContent.kt @@ -145,6 +145,7 @@ import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.async +import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.launch import moe.tarsin.coroutines.runSuspendCatching import moe.tarsin.coroutines.runSwallowingWithUI @@ -240,7 +241,7 @@ fun GalleryDetailContent( } } - val previews = galleryDetail?.let { collectPreviewItems(it, thumbColumns) } + val previews = galleryDetail.collectPreviewItems(thumbColumns) when (windowSizeClass.windowWidthSizeClass) { WindowWidthSizeClass.MEDIUM, WindowWidthSizeClass.COMPACT -> FastScrollLazyVerticalGrid( columns = GridCells.Fixed(thumbColumns), @@ -298,8 +299,8 @@ fun GalleryDetailContent( } } } - if (previews != null) { - galleryPreview(previews) { navToReader(galleryDetail.galleryInfo, it) } + if (galleryDetail != null) { + galleryPreview(galleryDetail, previews) { navToReader(galleryDetail.galleryInfo, it) } } } @@ -366,8 +367,8 @@ fun GalleryDetailContent( } } } - if (previews != null) { - galleryPreview(previews) { navToReader(galleryDetail.galleryInfo, it) } + if (galleryDetail != null) { + galleryPreview(galleryDetail, previews) { navToReader(galleryDetail.galleryInfo, it) } } } } @@ -735,10 +736,10 @@ private fun List.getArtistTag(): String? { return null } -typealias Previews = Pair> - +context(Context) @Composable -private fun Context.collectPreviewItems(detail: GalleryDetail, prefetchDistance: Int) = rememberInVM(detail) { +private fun GalleryDetail?.collectPreviewItems(prefetchDistance: Int) = rememberInVM(this) { + val detail = this@collectPreviewItems ?: return@rememberInVM emptyFlow() val pageSize = detail.previewList.size val pages = detail.pages val previewPagesMap = detail.previewList.associateBy { it.position } as MutableMap @@ -777,10 +778,13 @@ private fun Context.collectPreviewItems(detail: GalleryDetail, prefetchDistance: override val jumpingSupported = true } }.flow.cachedIn(viewModelScope) -}.collectAsLazyPagingItems().let { remember(detail, it) { detail to it } } +}.collectAsLazyPagingItems() -private fun LazyGridScope.galleryPreview(previews: Previews, onClick: (Int) -> Unit) { - val (detail, data) = previews +private fun LazyGridScope.galleryPreview( + detail: GalleryDetail, + data: LazyPagingItems, + onClick: (Int) -> Unit, +) { items( count = data.itemCount, key = data.itemKey(key = { item -> item.position }),