From 6b91f6545704ba6facace7b1d0cdd129ab132f71 Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Thu, 8 Sep 2022 03:11:44 +0600 Subject: [PATCH] Cleanup logic of `onStripSplit` in WebtoonPageHolder (#7955) * Cleanup logic of `onStripSplit` in WebtoonPageHolder * Update app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt Co-authored-by: arkon --- .../reader/viewer/webtoon/WebtoonPageHolder.kt | 17 ++++++++--------- .../kanade/tachiyomi/util/system/ImageUtil.kt | 17 +++++++++-------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt index c415a4aeec..3556b5d274 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt @@ -289,25 +289,24 @@ class WebtoonPageHolder( } val isStripSplitNeeded = ImageUtil.isStripSplitNeeded(imageStream) if (isStripSplitNeeded) { - onStripSplit(imageStream)?.let { return it } + return onStripSplit(imageStream) } } return imageStream } - private fun onStripSplit(imageStream: BufferedInputStream): InputStream? { - val page = page ?: return null - val streamFn = page.stream ?: return null + private fun onStripSplit(imageStream: BufferedInputStream): InputStream { + // If we have reached this point [page] and its stream shouldn't be null + val page = page!! + val stream = page.stream!! val splitData = ImageUtil.getSplitDataForStream(imageStream) - if (splitData.size == 1) return imageStream val newPages = splitData.map { - StencilPage(page) { ImageUtil.splitStrip(it, streamFn) } + StencilPage(page) { ImageUtil.splitStrip(it, stream) } }.toMutableList() - return newPages.removeFirst().stream?.invoke() + return newPages.removeFirst().stream!!() .also { - // Doing this first and then returning InputStream - // results in various issues with splitting + // Running [onLongStripSplit] first results in issues with splitting viewer.onLongStripSplit(page, newPages) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt index e4a6b43ebb..0c7b7fd620 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt @@ -261,7 +261,7 @@ object ImageUtil { val options = extractImageOptions(imageStream) val imageHeightIsBiggerThanWidth = options.outHeight > options.outWidth - val imageHeightBiggerThanScreenHeight = options.outHeight > getDisplayMaxHeightInPx + val imageHeightBiggerThanScreenHeight = options.outHeight > optimalImageHeight return imageHeightIsBiggerThanWidth && imageHeightBiggerThanScreenHeight } @@ -300,9 +300,8 @@ object ImageUtil { val imageHeight = outHeight val imageWidth = outWidth - val splitHeight = getDisplayMaxHeightInPx * 2 - // -1 so it doesn't try to split when imageHeight = splitHeight - val partCount = (imageHeight - 1) / splitHeight + 1 + // -1 so it doesn't try to split when imageHeight = optimalImageHeight + val partCount = (imageHeight - 1) / optimalImageHeight + 1 val optimalSplitHeight = imageHeight / partCount logcat { @@ -316,15 +315,15 @@ object ImageUtil { if (isNotEmpty() && imageHeight <= last().bottomOffset) break val topOffset = index * optimalSplitHeight - var outputImageHeight = min(optimalSplitHeight, imageHeight - topOffset) + var splitHeight = min(optimalSplitHeight, imageHeight - topOffset) - val remainingHeight = imageHeight - (topOffset + outputImageHeight) + val remainingHeight = imageHeight - (topOffset + splitHeight) // If remaining height is smaller or equal to 1/10th of // optimal split height then include it in current page if (remainingHeight <= (optimalSplitHeight / 10)) { - outputImageHeight += remainingHeight + splitHeight += remainingHeight } - add(SplitData(index, topOffset, outputImageHeight, imageWidth)) + add(SplitData(index, topOffset, splitHeight, imageWidth)) } } } @@ -578,6 +577,8 @@ object ImageUtil { } } + private val optimalImageHeight = getDisplayMaxHeightInPx * 2 + // Android doesn't include some mappings private val SUPPLEMENTARY_MIMETYPE_MAPPING = mapOf( // https://issuetracker.google.com/issues/182703810