From f94a2350ab2964d12fedc695748ac6068c83712b Mon Sep 17 00:00:00 2001 From: tarsin Date: Mon, 1 May 2023 11:16:54 +0800 Subject: [PATCH] Spilt LargeGalleryPreview and NormalGalleryPreview --- .../ehviewer/client/data/GalleryPreview.kt | 27 +++++++++++++------ .../client/parser/GalleryDetailParser.kt | 6 +++-- .../hippo/ehviewer/ui/widget/EhAsyncThumb.kt | 8 +++--- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/client/data/GalleryPreview.kt b/app/src/main/java/com/hippo/ehviewer/client/data/GalleryPreview.kt index be8a8edc7..e6e5008f5 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/data/GalleryPreview.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/data/GalleryPreview.kt @@ -18,12 +18,23 @@ package com.hippo.ehviewer.client.data import android.os.Parcelable import kotlinx.parcelize.Parcelize +interface GalleryPreview : Parcelable { + val imageUrl: String + val position: Int +} + @Parcelize -data class GalleryPreview( - val imageUrl: String, - val position: Int, - val offsetX: Int = Int.MIN_VALUE, - val offsetY: Int = Int.MIN_VALUE, - val clipWidth: Int = Int.MIN_VALUE, - val clipHeight: Int = Int.MIN_VALUE, -) : Parcelable +data class LargeGalleryPreview( + override val imageUrl: String, + override val position: Int, +) : GalleryPreview + +@Parcelize +data class NormalGalleryPreview( + override val imageUrl: String, + override val position: Int, + val offsetX: Int, + val offsetY: Int, + val clipWidth: Int, + val clipHeight: Int, +) : GalleryPreview diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt index bfe2be5e2..49d00123c 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt @@ -27,6 +27,8 @@ import com.hippo.ehviewer.client.data.GalleryCommentList import com.hippo.ehviewer.client.data.GalleryDetail import com.hippo.ehviewer.client.data.GalleryPreview import com.hippo.ehviewer.client.data.GalleryTagGroup +import com.hippo.ehviewer.client.data.LargeGalleryPreview +import com.hippo.ehviewer.client.data.NormalGalleryPreview import com.hippo.ehviewer.client.exception.EhException import com.hippo.ehviewer.client.exception.OffensiveException import com.hippo.ehviewer.client.exception.ParseException @@ -480,7 +482,7 @@ object GalleryDetailParser { val position = it.groupValues[2].toInt() - 1 val imageUrl = it.groupValues[3].trim() val pageUrl = it.groupValues[1].trim() - GalleryPreview(imageUrl, position) to pageUrl + LargeGalleryPreview(imageUrl, position) to pageUrl }.run { first.toList() to second.toList() } @@ -496,7 +498,7 @@ object GalleryDetailParser { val width = it.groupValues[1].toInt() val height = it.groupValues[2].toInt() val pageUrl = it.groupValues[5].trim() - GalleryPreview(imageUrl, position, xOffset, yOffset, width, height) to pageUrl + NormalGalleryPreview(imageUrl, position, xOffset, yOffset, width, height) to pageUrl }.run { first.toList() to second.toList() } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/widget/EhAsyncThumb.kt b/app/src/main/java/com/hippo/ehviewer/ui/widget/EhAsyncThumb.kt index 0a37405dc..bf4e1821c 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/widget/EhAsyncThumb.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/widget/EhAsyncThumb.kt @@ -18,6 +18,7 @@ import coil.compose.AsyncImage import coil.compose.AsyncImagePainter import coil.request.ImageRequest import com.hippo.ehviewer.client.data.GalleryPreview +import com.hippo.ehviewer.client.data.NormalGalleryPreview import com.hippo.ehviewer.coil.imageRequest @Composable @@ -38,9 +39,6 @@ fun EhAsyncThumb( contentScale = contentScale, ) -private val GalleryPreview.shouldClip - get() = offsetX != Int.MIN_VALUE - /** * Show a part of the original drawable, non-animated only implementation. */ @@ -56,7 +54,7 @@ fun EhAsyncPreview( modifier = modifier, transform = { model.run { - if (it is AsyncImagePainter.State.Success && shouldClip) { + if (it is AsyncImagePainter.State.Success && this is NormalGalleryPreview) { it.copy( painter = BitmapPainter( (it.result.drawable as BitmapDrawable).bitmap.asImageBitmap(), @@ -72,7 +70,7 @@ fun EhAsyncPreview( onState = { if (it is AsyncImagePainter.State.Success) { model.run { - if (shouldClip) { + if (this is NormalGalleryPreview) { if (clipWidth.toFloat() / clipHeight in 0.5..0.8) { if (contentScale == ContentScale.Fit) contentScale = ContentScale.Crop }