Codestral(ChatGPT) cleanup of some double pages code
This commit is contained in:
parent
7b7a594ddb
commit
1a4a2506f4
@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.ui.reader.viewer.pager
|
|||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.graphics.Bitmap
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import eu.kanade.tachiyomi.databinding.ReaderErrorBinding
|
import eu.kanade.tachiyomi.databinding.ReaderErrorBinding
|
||||||
@ -244,16 +245,7 @@ class PagerPageHolder(
|
|||||||
private fun mergePages(imageSource: BufferedSource, imageSource2: BufferedSource?): BufferedSource {
|
private fun mergePages(imageSource: BufferedSource, imageSource2: BufferedSource?): BufferedSource {
|
||||||
// Handle adding a center margin to wide images if requested
|
// Handle adding a center margin to wide images if requested
|
||||||
if (imageSource2 == null) {
|
if (imageSource2 == null) {
|
||||||
return if (
|
return handleWideImage(imageSource)
|
||||||
!ImageUtil.isAnimatedAndSupported(imageSource) &&
|
|
||||||
ImageUtil.isWideImage(imageSource) &&
|
|
||||||
viewer.config.centerMarginType and PagerConfig.CenterMarginType.WIDE_PAGE_CENTER_MARGIN > 0 &&
|
|
||||||
!viewer.config.imageCropBorders
|
|
||||||
) {
|
|
||||||
ImageUtil.addHorizontalCenterMargin(imageSource, height, context)
|
|
||||||
} else {
|
|
||||||
imageSource
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (page.fullPage) return imageSource
|
if (page.fullPage) return imageSource
|
||||||
@ -268,12 +260,7 @@ class PagerPageHolder(
|
|||||||
return imageSource
|
return imageSource
|
||||||
}
|
}
|
||||||
|
|
||||||
val imageBitmap = try {
|
val imageBitmap = decodeImage(imageSource)
|
||||||
ImageDecoder.newInstance(imageSource.inputStream())?.decode()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
logcat(LogPriority.ERROR, e) { "Cannot combine pages" }
|
|
||||||
null
|
|
||||||
}
|
|
||||||
if (imageBitmap == null) {
|
if (imageBitmap == null) {
|
||||||
imageSource2.close()
|
imageSource2.close()
|
||||||
page.fullPage = true
|
page.fullPage = true
|
||||||
@ -281,23 +268,16 @@ class PagerPageHolder(
|
|||||||
logcat(LogPriority.ERROR) { "Cannot combine pages" }
|
logcat(LogPriority.ERROR) { "Cannot combine pages" }
|
||||||
return imageSource
|
return imageSource
|
||||||
}
|
}
|
||||||
scope.launch { progressIndicator.setProgress(96) }
|
|
||||||
val height = imageBitmap.height
|
|
||||||
val width = imageBitmap.width
|
|
||||||
|
|
||||||
if (height < width) {
|
scope.launch { progressIndicator.setProgress(96) }
|
||||||
|
if (imageBitmap.height < imageBitmap.width) {
|
||||||
imageSource2.close()
|
imageSource2.close()
|
||||||
page.fullPage = true
|
page.fullPage = true
|
||||||
splitDoublePages()
|
splitDoublePages()
|
||||||
return imageSource
|
return imageSource
|
||||||
}
|
}
|
||||||
|
|
||||||
val imageBitmap2 = try {
|
val imageBitmap2 = decodeImage(imageSource2)
|
||||||
ImageDecoder.newInstance(imageSource2.inputStream())?.decode()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
logcat(LogPriority.ERROR, e) { "Cannot combine pages" }
|
|
||||||
null
|
|
||||||
}
|
|
||||||
if (imageBitmap2 == null) {
|
if (imageBitmap2 == null) {
|
||||||
imageSource2.close()
|
imageSource2.close()
|
||||||
extraPage?.fullPage = true
|
extraPage?.fullPage = true
|
||||||
@ -306,35 +286,63 @@ class PagerPageHolder(
|
|||||||
logcat(LogPriority.ERROR) { "Cannot combine pages" }
|
logcat(LogPriority.ERROR) { "Cannot combine pages" }
|
||||||
return imageSource
|
return imageSource
|
||||||
}
|
}
|
||||||
scope.launch { progressIndicator.setProgress(97) }
|
|
||||||
val height2 = imageBitmap2.height
|
|
||||||
val width2 = imageBitmap2.width
|
|
||||||
|
|
||||||
if (height2 < width2) {
|
scope.launch { progressIndicator.setProgress(97) }
|
||||||
|
if (imageBitmap2.height < imageBitmap2.width) {
|
||||||
imageSource2.close()
|
imageSource2.close()
|
||||||
extraPage?.fullPage = true
|
extraPage?.fullPage = true
|
||||||
page.isolatedPage = true
|
page.isolatedPage = true
|
||||||
splitDoublePages()
|
splitDoublePages()
|
||||||
return imageSource
|
return imageSource
|
||||||
}
|
}
|
||||||
|
|
||||||
val isLTR = (viewer !is R2LPagerViewer) xor viewer.config.invertDoublePages
|
val isLTR = (viewer !is R2LPagerViewer) xor viewer.config.invertDoublePages
|
||||||
|
val centerMargin = calculateCenterMargin(imageBitmap.height, imageBitmap2.height)
|
||||||
|
|
||||||
imageSource.close()
|
imageSource.close()
|
||||||
imageSource2.close()
|
imageSource2.close()
|
||||||
|
|
||||||
val centerMargin = if (viewer.config.centerMarginType and PagerConfig.CenterMarginType.DOUBLE_PAGE_CENTER_MARGIN > 0 && !viewer.config.imageCropBorders) {
|
return ImageUtil.mergeBitmaps(imageBitmap, imageBitmap2, isLTR, centerMargin, viewer.config.pageCanvasColor) {
|
||||||
|
updateProgress(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleWideImage(imageSource: BufferedSource): BufferedSource {
|
||||||
|
return if (
|
||||||
|
!ImageUtil.isAnimatedAndSupported(imageSource) &&
|
||||||
|
ImageUtil.isWideImage(imageSource) &&
|
||||||
|
viewer.config.centerMarginType and PagerConfig.CenterMarginType.WIDE_PAGE_CENTER_MARGIN > 0 &&
|
||||||
|
!viewer.config.imageCropBorders
|
||||||
|
) {
|
||||||
|
ImageUtil.addHorizontalCenterMargin(imageSource, height, context)
|
||||||
|
} else {
|
||||||
|
imageSource
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun decodeImage(imageSource: BufferedSource): Bitmap? {
|
||||||
|
return try {
|
||||||
|
ImageDecoder.newInstance(imageSource.inputStream())?.decode()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
logcat(LogPriority.ERROR, e) { "Cannot decode image" }
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun calculateCenterMargin(height: Int, height2: Int): Int {
|
||||||
|
return if (viewer.config.centerMarginType and PagerConfig.CenterMarginType.DOUBLE_PAGE_CENTER_MARGIN > 0 && !viewer.config.imageCropBorders) {
|
||||||
96 / (this.height.coerceAtLeast(1) / max(height, height2).coerceAtLeast(1)).coerceAtLeast(1)
|
96 / (this.height.coerceAtLeast(1) / max(height, height2).coerceAtLeast(1)).coerceAtLeast(1)
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return ImageUtil.mergeBitmaps(imageBitmap, imageBitmap2, isLTR, centerMargin, viewer.config.pageCanvasColor) {
|
private fun updateProgress(progress: Int) {
|
||||||
scope.launch {
|
scope.launch {
|
||||||
if (it == 100) {
|
if (progress == 100) {
|
||||||
progressIndicator.hide()
|
progressIndicator.hide()
|
||||||
} else {
|
} else {
|
||||||
progressIndicator.setProgress(it)
|
progressIndicator.setProgress(progress)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,6 @@ import eu.kanade.tachiyomi.widget.ViewPagerAdapter
|
|||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import tachiyomi.core.common.util.lang.launchUI
|
import tachiyomi.core.common.util.lang.launchUI
|
||||||
import tachiyomi.core.common.util.system.logcat
|
import tachiyomi.core.common.util.system.logcat
|
||||||
import kotlin.math.max
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pager adapter used by this [viewer] to where [ViewerChapters] updates are posted.
|
* Pager adapter used by this [viewer] to where [ViewerChapters] updates are posted.
|
||||||
@ -231,10 +230,12 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
|
|||||||
val oldCurrent = joinedItems.getOrNull(viewer.pager.currentItem)
|
val oldCurrent = joinedItems.getOrNull(viewer.pager.currentItem)
|
||||||
if (!viewer.config.doublePages) {
|
if (!viewer.config.doublePages) {
|
||||||
// If not in double mode, set up items like before
|
// If not in double mode, set up items like before
|
||||||
subItems.forEach {
|
subItems.forEach { readerItem ->
|
||||||
(it as? ReaderPage)?.shiftedPage = false
|
if (readerItem is ReaderPage) {
|
||||||
|
readerItem.shiftedPage = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.joinedItems = subItems.map { Pair<ReaderItem, ReaderItem?>(it, null) }.toMutableList()
|
this.joinedItems = subItems.map { Pair(it, null) }.toMutableList()
|
||||||
if (viewer is R2LPagerViewer) {
|
if (viewer is R2LPagerViewer) {
|
||||||
joinedItems.reverse()
|
joinedItems.reverse()
|
||||||
}
|
}
|
||||||
@ -242,54 +243,43 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
|
|||||||
val pagedItems = mutableListOf<MutableList<ReaderPage?>>()
|
val pagedItems = mutableListOf<MutableList<ReaderPage?>>()
|
||||||
val otherItems = mutableListOf<ReaderItem>()
|
val otherItems = mutableListOf<ReaderItem>()
|
||||||
pagedItems.add(mutableListOf())
|
pagedItems.add(mutableListOf())
|
||||||
|
|
||||||
// Step 1: segment the pages and transition pages
|
// Step 1: segment the pages and transition pages
|
||||||
subItems.forEach {
|
subItems.forEach { readerItem ->
|
||||||
when (it) {
|
when (readerItem) {
|
||||||
is ReaderPage -> {
|
is ReaderPage -> {
|
||||||
if (pagedItems.last().lastOrNull() != null &&
|
if (pagedItems.last().isNotEmpty() && pagedItems.last().last()?.chapter?.chapter?.id != readerItem.chapter.chapter.id) {
|
||||||
pagedItems.last().last()?.chapter?.chapter?.id != it.chapter.chapter.id
|
|
||||||
) {
|
|
||||||
pagedItems.add(mutableListOf())
|
pagedItems.add(mutableListOf())
|
||||||
}
|
}
|
||||||
pagedItems.last().add(it)
|
pagedItems.last().add(readerItem)
|
||||||
}
|
}
|
||||||
is ChapterTransition -> {
|
is ChapterTransition -> {
|
||||||
otherItems.add(it)
|
otherItems.add(readerItem)
|
||||||
pagedItems.add(mutableListOf())
|
pagedItems.add(mutableListOf())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var pagedIndex = 0
|
|
||||||
val subJoinedItems = mutableListOf<Pair<ReaderItem, ReaderItem?>>()
|
val subJoinedItems = mutableListOf<Pair<ReaderItem, ReaderItem?>>()
|
||||||
|
|
||||||
// Step 2: run through each set of pages
|
// Step 2: run through each set of pages
|
||||||
pagedItems.forEach { items ->
|
pagedItems.forEach { items ->
|
||||||
|
items.forEach { it?.shiftedPage = false }
|
||||||
|
|
||||||
items.forEach {
|
|
||||||
it?.shiftedPage = false
|
|
||||||
}
|
|
||||||
// Step 3: If pages have been shifted,
|
// Step 3: If pages have been shifted,
|
||||||
if (viewer.config.shiftDoublePage) {
|
if (viewer.config.shiftDoublePage) {
|
||||||
|
val index = items.indexOf(pageToShift)
|
||||||
|
// Go from the current page and work your way back to the first page,
|
||||||
|
// or the first page that's a full page.
|
||||||
|
// This is done in case user tries to shift a page after a full page
|
||||||
|
val fullPageBeforeIndex = if (index > -1) {
|
||||||
|
items.take(index).indexOfLast { it?.fullPage == true }
|
||||||
|
} else {
|
||||||
|
-1
|
||||||
|
}.coerceAtLeast(0)
|
||||||
|
|
||||||
|
// Add a shifted page to the first place there isnt a full page
|
||||||
run loop@{
|
run loop@{
|
||||||
var index = items.indexOf(pageToShift)
|
|
||||||
if (pageToShift?.fullPage == true) {
|
|
||||||
index = max(0, index - 1)
|
|
||||||
}
|
|
||||||
// Go from the current page and work your way back to the first page,
|
|
||||||
// or the first page that's a full page.
|
|
||||||
// This is done in case user tries to shift a page after a full page
|
|
||||||
val fullPageBeforeIndex = max(
|
|
||||||
0,
|
|
||||||
(
|
|
||||||
if (index > -1) {
|
|
||||||
(
|
|
||||||
items.take(index).indexOfLast { it?.fullPage == true }
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
-1
|
|
||||||
}
|
|
||||||
),
|
|
||||||
)
|
|
||||||
// Add a shifted page to the first place there isnt a full page
|
|
||||||
(fullPageBeforeIndex until items.size).forEach {
|
(fullPageBeforeIndex until items.size).forEach {
|
||||||
if (items[it]?.fullPage == false) {
|
if (items[it]?.fullPage == false) {
|
||||||
items[it]?.shiftedPage = true
|
items[it]?.shiftedPage = true
|
||||||
@ -302,12 +292,15 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
|
|||||||
// Step 4: Add blanks for chunking
|
// Step 4: Add blanks for chunking
|
||||||
var itemIndex = 0
|
var itemIndex = 0
|
||||||
while (itemIndex < items.size) {
|
while (itemIndex < items.size) {
|
||||||
items[itemIndex]?.isolatedPage = false
|
val currentItem = items[itemIndex]
|
||||||
if (items[itemIndex]?.fullPage == true || items[itemIndex]?.shiftedPage == true) {
|
currentItem?.isolatedPage = false
|
||||||
|
if (currentItem?.fullPage == true || currentItem?.shiftedPage == true) {
|
||||||
// Add a 'blank' page after each full page. It will be used when chunked to solo a page
|
// Add a 'blank' page after each full page. It will be used when chunked to solo a page
|
||||||
items.add(itemIndex + 1, null)
|
items.add(itemIndex + 1, null)
|
||||||
if (items[itemIndex]?.fullPage == true && itemIndex > 0 &&
|
if (
|
||||||
items[itemIndex - 1] != null && (itemIndex - 1) % 2 == 0
|
currentItem.fullPage && itemIndex > 0 &&
|
||||||
|
items[itemIndex - 1] != null &&
|
||||||
|
(itemIndex - 1) % 2 == 0
|
||||||
) {
|
) {
|
||||||
// If a page is a full page, check if the previous page needs to be isolated
|
// If a page is a full page, check if the previous page needs to be isolated
|
||||||
// we should check if it's an even or odd page, since even pages need shifting
|
// we should check if it's an even or odd page, since even pages need shifting
|
||||||
@ -325,15 +318,14 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
|
|||||||
|
|
||||||
// Step 5: chunk em
|
// Step 5: chunk em
|
||||||
if (items.isNotEmpty()) {
|
if (items.isNotEmpty()) {
|
||||||
subJoinedItems.addAll(
|
subJoinedItems.addAll(items.chunked(2).map { Pair(it.first()!!, it.getOrNull(1)) })
|
||||||
items.chunked(2).map { Pair(it.first()!!, it.getOrNull(1)) },
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
otherItems.getOrNull(pagedIndex)?.let {
|
|
||||||
|
otherItems.getOrNull(pagedItems.indexOf(items))?.let {
|
||||||
subJoinedItems.add(Pair(it, null))
|
subJoinedItems.add(Pair(it, null))
|
||||||
pagedIndex++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (viewer is R2LPagerViewer) {
|
if (viewer is R2LPagerViewer) {
|
||||||
subJoinedItems.reverse()
|
subJoinedItems.reverse()
|
||||||
}
|
}
|
||||||
@ -347,42 +339,37 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
|
|||||||
// we need to set the page back correctly
|
// we need to set the page back correctly
|
||||||
// We will however shift to the first page of the new chapter if the last page we were are
|
// We will however shift to the first page of the new chapter if the last page we were are
|
||||||
// on is not in the new chapter that has loaded
|
// on is not in the new chapter that has loaded
|
||||||
val newPage =
|
val newPage = when {
|
||||||
when {
|
oldCurrent?.first is ReaderPage && (oldCurrent.first as ReaderPage).chapter != currentChapter &&
|
||||||
(oldCurrent?.first as? ReaderPage)?.chapter != currentChapter &&
|
(oldCurrent.second as? ChapterTransition)?.from != currentChapter ->
|
||||||
(oldCurrent?.first as? ChapterTransition)?.from != currentChapter -> subItems.find {
|
subItems.find { it is ReaderPage && it.chapter == currentChapter }
|
||||||
(it as? ReaderPage)?.chapter == currentChapter
|
useSecondPage -> oldCurrent?.second ?: oldCurrent?.first
|
||||||
}
|
else -> oldCurrent?.first ?: return
|
||||||
useSecondPage -> (oldCurrent?.second ?: oldCurrent?.first)
|
|
||||||
else -> oldCurrent?.first ?: return
|
|
||||||
}
|
|
||||||
var index = joinedItems.indexOfFirst { it.first == newPage || it.second == newPage }
|
|
||||||
if (newPage is ChapterTransition && index == -1) {
|
|
||||||
val newerPage = if (newPage is ChapterTransition.Next) {
|
|
||||||
joinedItems.filter {
|
|
||||||
(it.first as? ReaderPage)?.chapter == newPage.to
|
|
||||||
}.minByOrNull { (it.first as? ReaderPage)?.index ?: Int.MAX_VALUE }?.first
|
|
||||||
} else {
|
|
||||||
joinedItems.filter {
|
|
||||||
(it.first as? ReaderPage)?.chapter == newPage.to
|
|
||||||
}.maxByOrNull { (it.first as? ReaderPage)?.index ?: Int.MIN_VALUE }?.first
|
|
||||||
}
|
|
||||||
index = joinedItems.indexOfFirst { it.first == newerPage || it.second == newerPage }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val index = when (newPage) {
|
||||||
|
is ChapterTransition -> {
|
||||||
|
val filteredPages = joinedItems.filter { it.first is ReaderPage && (it.first as ReaderPage).chapter == newPage.to }
|
||||||
|
val page = if (newPage is ChapterTransition.Next) {
|
||||||
|
filteredPages.minByOrNull { (it.first as ReaderPage).index }?.first
|
||||||
|
} else {
|
||||||
|
filteredPages.maxByOrNull { (it.first as ReaderPage).index }?.first
|
||||||
|
}
|
||||||
|
joinedItems.indexOfFirst { it.first == page || it.second == page }
|
||||||
|
}
|
||||||
|
else -> joinedItems.indexOfFirst { it.first == newPage || it.second == newPage }
|
||||||
|
}
|
||||||
|
|
||||||
viewer.pager.setCurrentItem(index, false)
|
viewer.pager.setCurrentItem(index, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun splitDoublePages(current: ReaderPage) {
|
fun splitDoublePages(current: ReaderPage) {
|
||||||
val oldCurrent = joinedItems.getOrNull(viewer.pager.currentItem)
|
val oldCurrent = joinedItems.getOrNull(viewer.pager.currentItem)
|
||||||
setJoinedItems(
|
val oldSecondPage = oldCurrent?.second as? ReaderPage
|
||||||
oldCurrent?.second == current ||
|
val oldFirstPage = oldCurrent?.first as? ReaderPage
|
||||||
(current.index + 1) < (
|
val oldPage = oldSecondPage ?: oldFirstPage
|
||||||
(
|
|
||||||
oldCurrent?.second
|
setJoinedItems(oldSecondPage == current || (current.index + 1) < (oldPage?.index ?: 0))
|
||||||
?: oldCurrent?.first
|
|
||||||
) as? ReaderPage
|
|
||||||
)?.index ?: 0,
|
|
||||||
)
|
|
||||||
|
|
||||||
// The listener may be removed when we split a page, so the ui may not have updated properly
|
// The listener may be removed when we split a page, so the ui may not have updated properly
|
||||||
// This case usually happens when we load a new chapter and the first 2 pages need to split og
|
// This case usually happens when we load a new chapter and the first 2 pages need to split og
|
||||||
|
Loading…
x
Reference in New Issue
Block a user