Taking paging preload logic from upstream

This commit is contained in:
Jay 2020-05-07 22:51:32 -04:00
parent 283ff636f4
commit fe41b1515a
3 changed files with 45 additions and 30 deletions

View File

@ -371,12 +371,10 @@ class ReaderPresenter(
* Saves this [chapter] progress (last read page and whether it's read). * Saves this [chapter] progress (last read page and whether it's read).
*/ */
private fun saveChapterProgress(chapter: ReaderChapter) { private fun saveChapterProgress(chapter: ReaderChapter) {
val dbChapter = db.getChapter(chapter.chapter.id!!).executeAsBlocking() db.getChapter(chapter.chapter.id!!).executeAsBlocking()?.let { dbChapter ->
chapter.chapter.bookmark = dbChapter!!.bookmark chapter.chapter.bookmark = dbChapter.bookmark
db.updateChapterProgress(chapter.chapter).asRxCompletable() }
.onErrorComplete() db.updateChapterProgress(chapter.chapter).executeAsBlocking()
.subscribeOn(Schedulers.io())
.subscribe()
} }
/** /**

View File

@ -12,6 +12,8 @@ import eu.kanade.tachiyomi.ui.reader.model.ChapterTransition
import eu.kanade.tachiyomi.ui.reader.model.ReaderPage import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters
import eu.kanade.tachiyomi.ui.reader.viewer.BaseViewer import eu.kanade.tachiyomi.ui.reader.viewer.BaseViewer
import eu.kanade.tachiyomi.util.view.gone
import eu.kanade.tachiyomi.util.view.visible
import timber.log.Timber import timber.log.Timber
/** /**
@ -63,7 +65,7 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
} }
init { init {
pager.visibility = View.GONE // Don't layout the pager yet pager.gone() // Don't layout the pager yet
pager.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) pager.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
pager.offscreenPageLimit = 1 pager.offscreenPageLimit = 1
pager.id = R.id.reader_pager pager.id = R.id.reader_pager
@ -80,8 +82,8 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
pager.tapListener = { event -> pager.tapListener = { event ->
val positionX = event.x val positionX = event.x
when { when {
positionX < pager.width * 0.33f -> if (config.tappingEnabled) moveLeft() positionX < pager.width * 0.33f -> if (config.tappingEnabled) moveLeft() else activity.toggleMenu()
positionX > pager.width * 0.66f -> if (config.tappingEnabled) moveRight() positionX > pager.width * 0.66f -> if (config.tappingEnabled) moveRight() else activity.toggleMenu()
else -> activity.toggleMenu() else -> activity.toggleMenu()
} }
} }
@ -113,40 +115,52 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
return pager return pager
} }
/**
* Destroys this viewer. Called when leaving the reader or swapping viewers.
*/
override fun destroy() {
super.destroy()
config.unsubscribe()
}
/** /**
* Called when a new page (either a [ReaderPage] or [ChapterTransition]) is marked as active * Called when a new page (either a [ReaderPage] or [ChapterTransition]) is marked as active
*/ */
private fun onPageChange(position: Int) { private fun onPageChange(position: Int) {
val page = adapter.items.getOrNull(position) val page = adapter.items.getOrNull(position)
if (page != null && currentPage != page) { if (page != null && currentPage != page) {
val allowPreload = checkAllowPreload(page as? ReaderPage)
currentPage = page currentPage = page
when (page) { when (page) {
is ReaderPage -> onReaderPageSelected(page) is ReaderPage -> onReaderPageSelected(page, allowPreload)
is ChapterTransition -> onTransitionSelected(page) is ChapterTransition -> onTransitionSelected(page)
} }
} }
} }
private fun checkAllowPreload(page: ReaderPage?): Boolean {
// Page is transition page - preload allowed
page ?: return true
// Initial opening - preload allowed
currentPage ?: return true
// Allow preload for
// 1. Going to next chapter from chapter transition
// 2. Going between pages of same chapter
// 3. Next chapter page
return when (page.chapter) {
(currentPage as? ChapterTransition.Next)?.to -> true
(currentPage as? ReaderPage)?.chapter -> true
adapter.nextTransition?.to -> true
else -> false
}
}
/** /**
* Called when a [ReaderPage] is marked as active. It notifies 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. * activity of the change and requests the preload of the next chapter if this is the last page.
*/ */
private fun onReaderPageSelected(page: ReaderPage) { private fun onReaderPageSelected(page: ReaderPage, allowPreload: Boolean) {
activity.onPageSelected(page) activity.onPageSelected(page)
val pages = page.chapter.pages ?: return val pages = page.chapter.pages ?: return
Timber.d("onReaderPageSelected: ${page.number}/${pages.size}") Timber.d("onReaderPageSelected: ${page.number}/${pages.size}")
// Preload next chapter once we're within the last 3 pages of the current chapter // Preload next chapter once we're within the last 5 pages of the current chapter
val inPreloadRange = pages.size - page.number < 3 val inPreloadRange = pages.size - page.number < 5
if (inPreloadRange) { if (inPreloadRange && allowPreload && page.chapter == adapter.currentChapter) {
Timber.d("Request preload next chapter because we're at page ${page.number} of ${pages.size}") Timber.d("Request preload next chapter because we're at page ${page.number} of ${pages.size}")
adapter.nextTransition?.to?.let { adapter.nextTransition?.to?.let {
activity.requestPreloadChapter(it) activity.requestPreloadChapter(it)
@ -196,7 +210,7 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
Timber.d("Pager first layout") Timber.d("Pager first layout")
val pages = chapters.currChapter.pages ?: return val pages = chapters.currChapter.pages ?: return
moveToPage(pages[chapters.currChapter.requestedPage]) moveToPage(pages[chapters.currChapter.requestedPage])
pager.visibility = View.VISIBLE pager.visible()
} }
} }

View File

@ -23,6 +23,7 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
var nextTransition: ChapterTransition.Next? = null var nextTransition: ChapterTransition.Next? = null
private set private set
var currentChapter: ReaderChapter? = null
/** /**
* Updates this adapter with the given [chapters]. It handles setting a few pages of the * 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 * next/previous chapter to allow seamless transitions and inverting the pages if the viewer
@ -52,14 +53,17 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
newItems.addAll(currPages) newItems.addAll(currPages)
} }
currentChapter = chapters.currChapter
// Add next chapter transition and pages. // Add next chapter transition and pages.
nextTransition = ChapterTransition.Next(chapters.currChapter, chapters.nextChapter) nextTransition = ChapterTransition.Next(chapters.currChapter, chapters.nextChapter)
.also { .also {
if (forceTransition || if (forceTransition ||
chapters.nextChapter?.state !is ReaderChapter.State.Loaded) { chapters.nextChapter?.state !is ReaderChapter.State.Loaded
newItems.add(it) ) {
} newItems.add(it)
} }
}
if (chapters.nextChapter != null) { if (chapters.nextChapter != null) {
// Add at most two pages, because this chapter will be selected before the user can // Add at most two pages, because this chapter will be selected before the user can
@ -89,8 +93,7 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
* Creates a new view for the item at the given [position]. * Creates a new view for the item at the given [position].
*/ */
override fun createView(container: ViewGroup, position: Int): View { override fun createView(container: ViewGroup, position: Int): View {
val item = items[position] return when (val item = items[position]) {
return when (item) {
is ReaderPage -> PagerPageHolder(viewer, item) is ReaderPage -> PagerPageHolder(viewer, item)
is ChapterTransition -> PagerTransitionHolder(viewer, item) is ChapterTransition -> PagerTransitionHolder(viewer, item)
else -> throw NotImplementedError("Holder for ${item.javaClass} not implemented") else -> throw NotImplementedError("Holder for ${item.javaClass} not implemented")