mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-30 04:15:00 -05:00
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:
parent
aa51968603
commit
31e2fe6a4c
2 changed files with 52 additions and 9 deletions
|
@ -129,26 +129,46 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
|
|||
private fun onPageChange(position: Int) {
|
||||
val page = adapter.items.getOrNull(position)
|
||||
if (page != null && currentPage != page) {
|
||||
val allowPreload = checkAllowPreload(page as? ReaderPage)
|
||||
currentPage = page
|
||||
when (page) {
|
||||
is ReaderPage -> onReaderPageSelected(page)
|
||||
is ReaderPage -> onReaderPageSelected(page, allowPreload)
|
||||
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
|
||||
* 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
|
||||
Timber.d("onReaderPageSelected: ${page.number}/${pages.size}")
|
||||
activity.onPageSelected(page)
|
||||
|
||||
// Preload next chapter once we're within the last 3 pages of the current chapter
|
||||
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}")
|
||||
adapter.nextTransition?.to?.let {
|
||||
activity.requestPreloadChapter(it)
|
||||
|
|
|
@ -74,10 +74,11 @@ class WebtoonViewer(val activity: ReaderActivity) : BaseViewer {
|
|||
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
||||
val position = layoutManager.findLastEndVisibleItemPosition()
|
||||
val item = adapter.items.getOrNull(position)
|
||||
val allowPreload = checkAllowPreload(item as? ReaderPage)
|
||||
if (item != null && currentPage != item) {
|
||||
currentPage = item
|
||||
when (item) {
|
||||
is ReaderPage -> onPageSelected(item, position)
|
||||
is ReaderPage -> onPageSelected(item, allowPreload)
|
||||
is ChapterTransition -> onTransitionSelected(item)
|
||||
}
|
||||
}
|
||||
|
@ -122,6 +123,26 @@ class WebtoonViewer(val activity: ReaderActivity) : BaseViewer {
|
|||
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.
|
||||
*/
|
||||
|
@ -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
|
||||
* 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
|
||||
Timber.d("onPageSelected: ${page.number}/${pages.size}")
|
||||
activity.onPageSelected(page)
|
||||
|
||||
// Preload next chapter once we're within the last 3 pages of the current chapter
|
||||
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}")
|
||||
val transition = adapter.items.getOrNull(pages.size + 1) as? ChapterTransition.Next
|
||||
if (transition?.to != null) {
|
||||
activity.requestPreloadChapter(transition.to)
|
||||
val nextItem = adapter.items.getOrNull(adapter.items.size - 1)
|
||||
val transitionChapter = (nextItem as? ChapterTransition.Next)?.to ?: (nextItem as?ReaderPage)?.chapter
|
||||
if (transitionChapter != null) {
|
||||
Timber.d("Requesting to preload chapter ${transitionChapter.chapter.chapter_number}")
|
||||
activity.requestPreloadChapter(transitionChapter)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue