mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2024-12-23 19:31:49 +01:00
Refactoting from dev
This commit is contained in:
parent
3d3d9b3a12
commit
8638e5452d
@ -70,14 +70,7 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
|
|||||||
pager.adapter = adapter
|
pager.adapter = adapter
|
||||||
pager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
|
pager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
|
||||||
override fun onPageSelected(position: Int) {
|
override fun onPageSelected(position: Int) {
|
||||||
val page = adapter.items.getOrNull(position)
|
onPageChange(position)
|
||||||
if (page != null && currentPage != page) {
|
|
||||||
currentPage = page
|
|
||||||
when (page) {
|
|
||||||
is ReaderPage -> onPageSelected(page, position)
|
|
||||||
is ChapterTransition -> onTransitionSelected(page)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPageScrollStateChanged(state: Int) {
|
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
|
* Called when a new page (either a [ReaderPage] or [ChapterTransition]) is marked as active
|
||||||
* 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 onPageChange(position: Int) {
|
||||||
val pages = page.chapter.pages!! // Won't be null because it's the loaded chapter
|
val page = adapter.items.getOrNull(position)
|
||||||
Timber.d("onPageSelected: ${page.number}/${pages.size}")
|
if (page != null && currentPage != page) {
|
||||||
activity.onPageSelected(page)
|
currentPage = page
|
||||||
|
when (page) {
|
||||||
if (page === pages.last()) {
|
is ReaderPage -> onReaderPageSelected(page)
|
||||||
Timber.d("Request preload next chapter because we're at the last page")
|
is ChapterTransition -> onTransitionSelected(page)
|
||||||
val transition = adapter.items.getOrNull(position + 1) as? ChapterTransition.Next
|
|
||||||
if (transition?.to != null) {
|
|
||||||
activity.requestPreloadChapter(transition.to)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
* preload of the destination chapter of the transition.
|
||||||
*/
|
*/
|
||||||
private fun onTransitionSelected(transition: ChapterTransition) {
|
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].
|
* Tells this viewer to move to the given [page].
|
||||||
*/
|
*/
|
||||||
override fun moveToPage(page: ReaderPage) {
|
override fun moveToPage(page: ReaderPage) {
|
||||||
Timber.d("moveToPage")
|
Timber.d("moveToPage ${page.number}")
|
||||||
val position = adapter.items.indexOf(page)
|
val position = adapter.items.indexOf(page)
|
||||||
if (position != -1) {
|
if (position != -1) {
|
||||||
|
val currentPosition = pager.currentItem
|
||||||
pager.setCurrentItem(position, true)
|
pager.setCurrentItem(position, true)
|
||||||
|
// manually call onPageChange since ViewPager listener is not triggered in this case
|
||||||
|
if (currentPosition == position) {
|
||||||
|
onPageChange(position)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Timber.d("Page $page not found in adapter")
|
Timber.d("Page $page not found in adapter")
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package eu.kanade.tachiyomi.ui.reader.viewer.pager
|
package eu.kanade.tachiyomi.ui.reader.viewer.pager
|
||||||
|
|
||||||
import androidx.viewpager.widget.PagerAdapter
|
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import eu.kanade.tachiyomi.ui.reader.model.ChapterTransition
|
import eu.kanade.tachiyomi.ui.reader.model.ChapterTransition
|
||||||
@ -20,6 +19,9 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
|
|||||||
var items: List<Any> = emptyList()
|
var items: List<Any> = emptyList()
|
||||||
private set
|
private set
|
||||||
|
|
||||||
|
var nextTransition: ChapterTransition.Next? = null
|
||||||
|
private set
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
@ -46,7 +48,8 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add next chapter transition and pages.
|
// 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) {
|
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
|
||||||
// swap more pages.
|
// swap more pages.
|
||||||
@ -95,7 +98,7 @@ class PagerViewerAdapter(private val viewer: PagerViewer) : ViewPagerAdapter() {
|
|||||||
Timber.d("Position for ${view.item} not found")
|
Timber.d("Position for ${view.item} not found")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return androidx.viewpager.widget.PagerAdapter.POSITION_NONE
|
return POSITION_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user