From 7a4763ee68b404d51f93533d0b09c3e5c642a0be Mon Sep 17 00:00:00 2001 From: MCAxiaz Date: Thu, 16 Jan 2020 18:45:06 -0800 Subject: [PATCH] minor reader bugs: (#2491) - fix preload on last page for R2L reader - page 3 bug (cherry picked from commit 8b0458cdf6f9ff733074945d5e8f4209f73474f5) --- .../ui/reader/viewer/pager/PagerViewer.kt | 55 +++++++++++-------- .../reader/viewer/pager/PagerViewerAdapter.kt | 6 +- 2 files changed, 38 insertions(+), 23 deletions(-) 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 8f37257a5..895f99d71 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 @@ -70,14 +70,7 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer { pager.adapter = adapter pager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() { override fun onPageSelected(position: Int) { - val page = adapter.items.getOrNull(position) - if (page != null && currentPage != page) { - currentPage = page - when (page) { - is ReaderPage -> onPageSelected(page, position) - is ChapterTransition -> onTransitionSelected(page) - } - } + onPageChange(position) } override fun onPageScrollStateChanged(state: Int) { @@ -129,25 +122,38 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer { } /** - * Called from the ViewPager listener when a [page] is marked as active. It notifies the - * activity of the change and requests the preload of the next chapter if this is the last page. + * Called when a new page (either a [ReaderPage] or [ChapterTransition]) is marked as active */ - private fun onPageSelected(page: ReaderPage, position: Int) { - val pages = page.chapter.pages!! // Won't be null because it's the loaded chapter - Timber.d("onPageSelected: ${page.number}/${pages.size}") - activity.onPageSelected(page) - - if (page === pages.last()) { - Timber.d("Request preload next chapter because we're at the last page") - val transition = adapter.items.getOrNull(position + 1) as? ChapterTransition.Next - if (transition?.to != null) { - activity.requestPreloadChapter(transition.to) + private fun onPageChange(position: Int) { + val page = adapter.items.getOrNull(position) + if (page != null && currentPage != page) { + currentPage = page + when (page) { + is ReaderPage -> onReaderPageSelected(page) + is ChapterTransition -> onTransitionSelected(page) } } } /** - * Called from the ViewPager listener when a [transition] is marked as active. It request the + * Called when a [ReaderPage] is marked as active. It notifies the + * activity of the change and requests the preload of the next chapter if this is the last page. + */ + private fun onReaderPageSelected(page: ReaderPage) { + val pages = page.chapter.pages!! // Won't be null because it's the loaded chapter + Timber.d("onReaderPageSelected: ${page.number}/${pages.size}") + activity.onPageSelected(page) + + if (page === pages.last()) { + Timber.d("Request preload next chapter because we're at the last page") + adapter.nextTransition?.to?.let { + activity.requestPreloadChapter(it) + } + } + } + + /** + * Called when a [ChapterTransition] is marked as active. It request the * preload of the destination chapter of the transition. */ private fun onTransitionSelected(transition: ChapterTransition) { @@ -194,10 +200,15 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer { * Tells this viewer to move to the given [page]. */ override fun moveToPage(page: ReaderPage) { - Timber.d("moveToPage") + Timber.d("moveToPage ${page.number}") val position = adapter.items.indexOf(page) if (position != -1) { + val currentPosition = pager.currentItem pager.setCurrentItem(position, true) + // manually call onPageChange since ViewPager listener is not triggered in this case + if (currentPosition == position) { + onPageChange(position) + } } else { Timber.d("Page $page not found in adapter") } 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 d03e822b0..930eb2a0f 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 @@ -20,6 +20,9 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() { var items: List = emptyList() private set + var nextTransition: ChapterTransition.Next? = null + private set + /** * Updates this adapter with the given [chapters]. It handles setting a few pages of the * next/previous chapter to allow seamless transitions and inverting the pages if the viewer @@ -46,7 +49,8 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() { } // Add next chapter transition and pages. - newItems.add(ChapterTransition.Next(chapters.currChapter, chapters.nextChapter)) + nextTransition = ChapterTransition.Next(chapters.currChapter, chapters.nextChapter) + .also { newItems.add(it) } if (chapters.nextChapter != null) { // Add at most two pages, because this chapter will be selected before the user can // swap more pages.