Fix bugs in dual-page split (#4983)

If more bugs appear probably better to go back to the main thread and process dual-pages every time a page is shown as it did before
This commit is contained in:
Andreas 2021-04-30 15:14:20 +02:00 committed by GitHub
parent 60f18f3b5a
commit fe373a95a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 14 deletions

View File

@ -17,6 +17,7 @@ import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
import eu.kanade.tachiyomi.ui.reader.loader.ChapterLoader
import eu.kanade.tachiyomi.ui.reader.model.InsertPage
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters
@ -367,6 +368,11 @@ class ReaderPresenter(
val selectedChapter = page.chapter
// Insert page doesn't change page progress
if (page is InsertPage) {
return
}
// Save last page read and mark as read if needed
selectedChapter.chapter.last_page_read = page.index
val shouldTrack = !preferences.incognitoMode().get() || hasTrackers

View File

@ -230,7 +230,7 @@ class PagerPageHolder(
readImageHeaderSubscription = Observable
.fromCallable {
val stream = streamFn().buffered(16)
openStream = process(stream)
openStream = process(item, stream)
ImageUtil.findImageType(stream) == ImageUtil.ImageType.GIF
}
@ -249,7 +249,7 @@ class PagerPageHolder(
.subscribe({}, {})
}
private fun process(imageStream: InputStream): InputStream {
private fun process(page: ReaderPage, imageStream: InputStream): InputStream {
if (!viewer.config.dualPageSplit) {
return imageStream
}
@ -263,7 +263,7 @@ class PagerPageHolder(
return imageStream
}
onPageSplit()
onPageSplit(page)
return splitInHalf(imageStream)
}
@ -287,7 +287,7 @@ class PagerPageHolder(
return ImageUtil.splitInHalf(imageStream, side)
}
private fun onPageSplit() {
private fun onPageSplit(page: ReaderPage) {
val newPage = InsertPage(page)
viewer.onPageSplit(page, newPage)
}

View File

@ -192,6 +192,11 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
Timber.d("onReaderPageSelected: ${page.number}/${pages.size}")
activity.onPageSelected(page)
// Skip preload on inserts it causes unwanted page jumping
if (page is InsertPage) {
return
}
// Preload next chapter once we're within the last 5 pages of the current chapter
val inPreloadRange = pages.size - page.number < 5
if (inPreloadRange && allowPreload && page.chapter == adapter.currentChapter) {
@ -387,7 +392,7 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
fun onPageSplit(currentPage: ReaderPage, newPage: InsertPage) {
activity.runOnUiThread {
// Need to insert on UI thread else images will go blank
adapter.onPageSplit(currentPage, newPage, this::class.java)
adapter.onPageSplit(currentPage, newPage)
}
}

View File

@ -22,6 +22,11 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
var items: MutableList<Any> = mutableListOf()
private set
/**
* Holds preprocessed items so they don't get removed when changing chapter
*/
private var preprocessed: MutableMap<Int, InsertPage> = mutableMapOf()
var nextTransition: ChapterTransition.Next? = null
private set
@ -54,10 +59,25 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
newItems.add(ChapterTransition.Prev(chapters.currChapter, chapters.prevChapter))
}
var insertPageLastPage: InsertPage? = null
// Add current chapter.
val currPages = chapters.currChapter.pages
if (currPages != null) {
newItems.addAll(currPages)
val pages = currPages.toMutableList()
val lastPage = pages.last()
// Insert preprocessed pages into current page list
preprocessed.keys.sortedDescending()
.forEach { key ->
if (lastPage.index == key) {
insertPageLastPage = preprocessed[key]
}
preprocessed[key]?.let { pages.add(key + 1, it) }
}
newItems.addAll(pages)
}
currentChapter = chapters.currChapter
@ -88,8 +108,14 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
newItems.reverse()
}
preprocessed = mutableMapOf()
items = newItems
notifyDataSetChanged()
// Will skip insert page otherwise
if (insertPageLastPage != null) {
viewer.moveToPage(insertPageLastPage!!)
}
}
/**
@ -125,20 +151,25 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
return POSITION_NONE
}
fun onPageSplit(current: Any?, newPage: InsertPage, clazz: Class<out PagerViewer>) {
if (current !is ReaderPage) return
fun onPageSplit(currentPage: Any?, newPage: InsertPage) {
if (currentPage !is ReaderPage) return
val currentIndex = items.indexOf(current)
val currentIndex = items.indexOf(currentPage)
val placeAtIndex = when {
clazz.isAssignableFrom(L2RPagerViewer::class.java) -> currentIndex + 1
clazz.isAssignableFrom(VerticalPagerViewer::class.java) -> currentIndex + 1
clazz.isAssignableFrom(R2LPagerViewer::class.java) -> currentIndex
// Put aside preprocessed pages for next chapter so they don't get removed when changing chapter
if (currentPage.chapter.chapter.id != currentChapter?.chapter?.id) {
preprocessed[newPage.index] = newPage
return
}
val placeAtIndex = when (viewer) {
is L2RPagerViewer,
is VerticalPagerViewer -> currentIndex + 1
else -> currentIndex
}
// It will enter a endless cycle of insert pages
if (clazz.isAssignableFrom(R2LPagerViewer::class.java) && items[placeAtIndex - 1] is InsertPage) {
if (viewer is R2LPagerViewer && placeAtIndex - 1 >= 0 && items[placeAtIndex - 1] is InsertPage) {
return
}