Fix preview list not recomposing on detail refresh

This commit is contained in:
FooIbar committed 2024-10-23 11:27:04 +08:00
1 parent 87ac60a3ab
commit f301eab5b1
1 file changed
+15 -11
@@ -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<GalleryTagGroup>.getArtistTag(): String? {
return null
}
typealias Previews = Pair<GalleryDetail, LazyPagingItems<GalleryPreview>>
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<GalleryPreview>,
onClick: (Int) -> Unit,
) {
items(
count = data.itemCount,
key = data.itemKey(key = { item -> item.position }),