From fe373a95a2a6eaf9792a28b4f2b4f394ae40fba4 Mon Sep 17 00:00:00 2001 From: Andreas Date: Fri, 30 Apr 2021 15:14:20 +0200 Subject: [PATCH] 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 --- .../tachiyomi/ui/reader/ReaderPresenter.kt | 6 +++ .../ui/reader/viewer/pager/PagerPageHolder.kt | 8 +-- .../ui/reader/viewer/pager/PagerViewer.kt | 7 ++- .../reader/viewer/pager/PagerViewerAdapter.kt | 49 +++++++++++++++---- 4 files changed, 56 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt index c318c9596..a61c46afe 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt @@ -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 diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt index f5abc2728..791a8a025 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt @@ -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) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt index b5808f4bf..478d7bb42 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt @@ -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) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewerAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewerAdapter.kt index 59a35e5c0..888d83648 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewerAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewerAdapter.kt @@ -22,6 +22,11 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() { var items: MutableList = mutableListOf() private set + /** + * Holds preprocessed items so they don't get removed when changing chapter + */ + private var preprocessed: MutableMap = 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) { - 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 }