Fix bookmarks thumbnails

pull/340/head
Koitharu 3 years ago
parent cfd39e615a
commit 8ce5e7eccf
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -8,10 +8,12 @@ import org.koitharu.kotatsu.base.ui.list.AdapterDelegateClickListenerAdapter
import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener
import org.koitharu.kotatsu.bookmarks.domain.Bookmark import org.koitharu.kotatsu.bookmarks.domain.Bookmark
import org.koitharu.kotatsu.databinding.ItemBookmarkBinding import org.koitharu.kotatsu.databinding.ItemBookmarkBinding
import org.koitharu.kotatsu.utils.ext.decodeRegion
import org.koitharu.kotatsu.utils.ext.disposeImageRequest import org.koitharu.kotatsu.utils.ext.disposeImageRequest
import org.koitharu.kotatsu.utils.ext.enqueueWith import org.koitharu.kotatsu.utils.ext.enqueueWith
import org.koitharu.kotatsu.utils.ext.newImageRequest import org.koitharu.kotatsu.utils.ext.newImageRequest
import org.koitharu.kotatsu.utils.ext.source import org.koitharu.kotatsu.utils.ext.source
import org.koitharu.kotatsu.utils.image.CoverSizeResolver
fun bookmarkListAD( fun bookmarkListAD(
coil: ImageLoader, coil: ImageLoader,
@ -27,10 +29,12 @@ fun bookmarkListAD(
bind { bind {
binding.imageViewThumb.newImageRequest(lifecycleOwner, item.imageUrl)?.run { binding.imageViewThumb.newImageRequest(lifecycleOwner, item.imageUrl)?.run {
size(CoverSizeResolver(binding.imageViewThumb))
placeholder(R.drawable.ic_placeholder) placeholder(R.drawable.ic_placeholder)
fallback(R.drawable.ic_placeholder) fallback(R.drawable.ic_placeholder)
error(R.drawable.ic_error_placeholder) error(R.drawable.ic_error_placeholder)
allowRgb565(true) allowRgb565(true)
decodeRegion(item.scroll)
source(item.manga.source) source(item.manga.source)
enqueueWith(coil) enqueueWith(coil)
} }

@ -19,6 +19,7 @@ import org.koitharu.kotatsu.utils.ext.disposeImageRequest
import org.koitharu.kotatsu.utils.ext.enqueueWith import org.koitharu.kotatsu.utils.ext.enqueueWith
import org.koitharu.kotatsu.utils.ext.newImageRequest import org.koitharu.kotatsu.utils.ext.newImageRequest
import org.koitharu.kotatsu.utils.ext.source import org.koitharu.kotatsu.utils.ext.source
import org.koitharu.kotatsu.utils.image.CoverSizeResolver
fun bookmarksGroupAD( fun bookmarksGroupAD(
coil: ImageLoader, coil: ImageLoader,
@ -54,6 +55,7 @@ fun bookmarksGroupAD(
fallback(R.drawable.ic_placeholder) fallback(R.drawable.ic_placeholder)
error(R.drawable.ic_error_placeholder) error(R.drawable.ic_error_placeholder)
allowRgb565(true) allowRgb565(true)
size(CoverSizeResolver(binding.imageViewCover))
source(item.manga.source) source(item.manga.source)
enqueueWith(coil) enqueueWith(coil)
} }

@ -57,7 +57,7 @@ fun pageThumbnailAD(
ImageRequest.Builder(context) ImageRequest.Builder(context)
.data(file) .data(file)
.size(thumbSize) .size(thumbSize)
.decodeRegion() .decodeRegion(0)
.allowRgb565(isLowRamDevice(context)) .allowRgb565(isLowRamDevice(context))
.build(), .build(),
).drawable ).drawable

@ -54,9 +54,10 @@ fun ImageRequest.Builder.indicator(indicator: BaseProgressIndicator<*>): ImageRe
return listener(ImageRequestIndicatorListener(indicator)) return listener(ImageRequestIndicatorListener(indicator))
} }
fun ImageRequest.Builder.decodeRegion(): ImageRequest.Builder { fun ImageRequest.Builder.decodeRegion(
return decoderFactory(RegionBitmapDecoder.Factory()) scroll: Int = RegionBitmapDecoder.SCROLL_UNDEFINED,
} ): ImageRequest.Builder = decoderFactory(RegionBitmapDecoder.Factory())
.setParameter(RegionBitmapDecoder.PARAM_SCROLL, scroll)
@Suppress("SpellCheckingInspection") @Suppress("SpellCheckingInspection")
fun ImageRequest.Builder.crossfade(context: Context): ImageRequest.Builder { fun ImageRequest.Builder.crossfade(context: Context): ImageRequest.Builder {

@ -14,10 +14,10 @@ import coil.decode.ImageSource
import coil.fetch.SourceResult import coil.fetch.SourceResult
import coil.request.Options import coil.request.Options
import coil.size.* import coil.size.*
import kotlin.math.roundToInt
import kotlinx.coroutines.runInterruptible import kotlinx.coroutines.runInterruptible
import kotlinx.coroutines.sync.Semaphore import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit import kotlinx.coroutines.sync.withPermit
import kotlin.math.roundToInt
class RegionBitmapDecoder( class RegionBitmapDecoder(
private val source: ImageSource, private val source: ImageSource,
@ -83,14 +83,22 @@ class RegionBitmapDecoder(
val dstRatio = dstWidth / dstHeight.toDouble() val dstRatio = dstWidth / dstHeight.toDouble()
val rect = if (srcRatio < dstRatio) { val rect = if (srcRatio < dstRatio) {
// probably manga // probably manga
Rect(0, 0, srcWidth, (srcWidth / dstRatio).toInt()) Rect(0, 0, srcWidth, (srcWidth / dstRatio).toInt().coerceAtLeast(1))
} else { } else {
Rect(0, 0, (srcHeight / dstRatio).toInt(), srcHeight) Rect(0, 0, (srcHeight / dstRatio).toInt().coerceAtLeast(1), srcHeight)
} }
val scroll = options.parameters.value(PARAM_SCROLL) ?: SCROLL_UNDEFINED
if (scroll == SCROLL_UNDEFINED) {
rect.offsetTo( rect.offsetTo(
(srcWidth - rect.width()) / 2, (srcWidth - rect.width()) / 2,
(srcHeight - rect.height()) / 2, (srcHeight - rect.height()) / 2,
) )
} else {
rect.offsetTo(
(srcWidth - rect.width()) / 2,
(scroll * dstRatio).toInt().coerceAtMost(srcHeight - rect.height()),
)
}
// Calculate the image's sample size. // Calculate the image's sample size.
inSampleSize = DecodeUtils.calculateInSampleSize( inSampleSize = DecodeUtils.calculateInSampleSize(
@ -148,21 +156,26 @@ class RegionBitmapDecoder(
override fun hashCode() = javaClass.hashCode() override fun hashCode() = javaClass.hashCode()
} }
}
private const val DEFAULT_MAX_PARALLELISM = 4 companion object {
private inline fun Size.widthPx(scale: Scale, original: () -> Int): Int { const val PARAM_SCROLL = "scroll"
const val SCROLL_UNDEFINED = -1
private const val DEFAULT_MAX_PARALLELISM = 4
private inline fun Size.widthPx(scale: Scale, original: () -> Int): Int {
return if (isOriginal) original() else width.toPx(scale) return if (isOriginal) original() else width.toPx(scale)
} }
private inline fun Size.heightPx(scale: Scale, original: () -> Int): Int { private inline fun Size.heightPx(scale: Scale, original: () -> Int): Int {
return if (isOriginal) original() else height.toPx(scale) return if (isOriginal) original() else height.toPx(scale)
} }
private fun Dimension.toPx(scale: Scale) = pxOrElse { private fun Dimension.toPx(scale: Scale) = pxOrElse {
when (scale) { when (scale) {
Scale.FILL -> Int.MIN_VALUE Scale.FILL -> Int.MIN_VALUE
Scale.FIT -> Int.MAX_VALUE Scale.FIT -> Int.MAX_VALUE
} }
}
}
} }

Loading…
Cancel
Save