Faulty preload fix (#2731)

* Condition for preload

* Added preload fix for webtoon viewer. Replaced incorrect last-page logic

* Requested refactoring

* Requested changes

* Requested changes
This commit is contained in:
morcefaster 2020-03-18 15:45:22 +02:00 committed by GitHub
parent aa51968603
commit 31e2fe6a4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 9 deletions

View file

@ -129,26 +129,46 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
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 == null ?: return true
// Initial opening - preload allowed
currentPage == null ?: 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) {
val pages = page.chapter.pages!! // Won't be null because it's the loaded chapter val pages = page.chapter.pages!! // Won't be null because it's the loaded chapter
Timber.d("onReaderPageSelected: ${page.number}/${pages.size}") Timber.d("onReaderPageSelected: ${page.number}/${pages.size}")
activity.onPageSelected(page) activity.onPageSelected(page)
// Preload next chapter once we're within the last 3 pages of the current chapter // Preload next chapter once we're within the last 3 pages of the current chapter
val inPreloadRange = pages.size - page.number < 3 val inPreloadRange = pages.size - page.number < 3
if (inPreloadRange) { if (inPreloadRange && allowPreload) {
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)

View file

@ -74,10 +74,11 @@ class WebtoonViewer(val activity: ReaderActivity) : BaseViewer {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
val position = layoutManager.findLastEndVisibleItemPosition() val position = layoutManager.findLastEndVisibleItemPosition()
val item = adapter.items.getOrNull(position) val item = adapter.items.getOrNull(position)
val allowPreload = checkAllowPreload(item as? ReaderPage)
if (item != null && currentPage != item) { if (item != null && currentPage != item) {
currentPage = item currentPage = item
when (item) { when (item) {
is ReaderPage -> onPageSelected(item, position) is ReaderPage -> onPageSelected(item, allowPreload)
is ChapterTransition -> onTransitionSelected(item) is ChapterTransition -> onTransitionSelected(item)
} }
} }
@ -122,6 +123,26 @@ class WebtoonViewer(val activity: ReaderActivity) : BaseViewer {
frame.addView(recycler) frame.addView(recycler)
} }
private fun checkAllowPreload(page: ReaderPage?): Boolean {
// Page is transition page - preload allowed
page == null ?: return true
// Initial opening - preload allowed
currentPage == null ?: return true
val nextItem = adapter.items.getOrNull(adapter.items.count() - 1)
val nextChapter = (nextItem as? ChapterTransition.Next)?.to ?: (nextItem as? ReaderPage)?.chapter
// Allow preload for
// 1. Going between pages of same chapter
// 2. Next chapter page
return when (page!!.chapter) {
(currentPage as? ReaderPage)?.chapter -> true
nextChapter -> true
else -> false
}
}
/** /**
* Returns the view this viewer uses. * Returns the view this viewer uses.
*/ */
@ -142,18 +163,20 @@ class WebtoonViewer(val activity: ReaderActivity) : BaseViewer {
* Called from the RecyclerView listener when a [page] is marked as active. It notifies the * Called from the RecyclerView 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. * activity of the change and requests the preload of the next chapter if this is the last page.
*/ */
private fun onPageSelected(page: ReaderPage, position: Int) { private fun onPageSelected(page: ReaderPage, allowPreload: Boolean) {
val pages = page.chapter.pages!! // Won't be null because it's the loaded chapter val pages = page.chapter.pages!! // Won't be null because it's the loaded chapter
Timber.d("onPageSelected: ${page.number}/${pages.size}") Timber.d("onPageSelected: ${page.number}/${pages.size}")
activity.onPageSelected(page) activity.onPageSelected(page)
// Preload next chapter once we're within the last 3 pages of the current chapter // Preload next chapter once we're within the last 3 pages of the current chapter
val inPreloadRange = pages.size - page.number < 3 val inPreloadRange = pages.size - page.number < 3
if (inPreloadRange) { if (inPreloadRange && allowPreload) {
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}")
val transition = adapter.items.getOrNull(pages.size + 1) as? ChapterTransition.Next val nextItem = adapter.items.getOrNull(adapter.items.size - 1)
if (transition?.to != null) { val transitionChapter = (nextItem as? ChapterTransition.Next)?.to ?: (nextItem as?ReaderPage)?.chapter
activity.requestPreloadChapter(transition.to) if (transitionChapter != null) {
Timber.d("Requesting to preload chapter ${transitionChapter.chapter.chapter_number}")
activity.requestPreloadChapter(transitionChapter)
} }
} }
} }