|
|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
package org.koitharu.kotatsu.core.ui.list.lifecycle
|
|
|
|
|
|
|
|
|
|
import android.view.View
|
|
|
|
|
import androidx.core.view.children
|
|
|
|
|
import androidx.viewpager2.widget.ViewPager2
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.recyclerView
|
|
|
|
|
@ -8,16 +9,59 @@ class PagerLifecycleDispatcher(
|
|
|
|
|
private val pager: ViewPager2,
|
|
|
|
|
) : ViewPager2.OnPageChangeCallback() {
|
|
|
|
|
|
|
|
|
|
private var pendingUpdate: OneShotLayoutListener? = null
|
|
|
|
|
|
|
|
|
|
override fun onPageSelected(position: Int) {
|
|
|
|
|
super.onPageSelected(position)
|
|
|
|
|
setResumedPage(position)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun invalidate() {
|
|
|
|
|
setResumedPage(pager.currentItem)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun setResumedPage(position: Int) {
|
|
|
|
|
pendingUpdate?.cancel()
|
|
|
|
|
pendingUpdate = null
|
|
|
|
|
var hasResumedItem = false
|
|
|
|
|
val rv = pager.recyclerView ?: return
|
|
|
|
|
if (rv.childCount == 0) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for (child in rv.children) {
|
|
|
|
|
val wh = rv.getChildViewHolder(child) ?: continue
|
|
|
|
|
(wh as? LifecycleAwareViewHolder)?.setIsCurrent(wh.absoluteAdapterPosition == position)
|
|
|
|
|
val isCurrent = wh.absoluteAdapterPosition == position
|
|
|
|
|
(wh as? LifecycleAwareViewHolder)?.setIsCurrent(isCurrent)
|
|
|
|
|
if (isCurrent) {
|
|
|
|
|
hasResumedItem = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!hasResumedItem) {
|
|
|
|
|
rv.addOnLayoutChangeListener(OneShotLayoutListener(rv, position).also { pendingUpdate = it })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun invalidate() {
|
|
|
|
|
onPageSelected(pager.currentItem)
|
|
|
|
|
private inner class OneShotLayoutListener(
|
|
|
|
|
private val view: View,
|
|
|
|
|
private val targetPosition: Int,
|
|
|
|
|
) : View.OnLayoutChangeListener {
|
|
|
|
|
|
|
|
|
|
override fun onLayoutChange(
|
|
|
|
|
v: View?,
|
|
|
|
|
left: Int,
|
|
|
|
|
top: Int,
|
|
|
|
|
right: Int,
|
|
|
|
|
bottom: Int,
|
|
|
|
|
oldLeft: Int,
|
|
|
|
|
oldTop: Int,
|
|
|
|
|
oldRight: Int,
|
|
|
|
|
oldBottom: Int
|
|
|
|
|
) {
|
|
|
|
|
view.removeOnLayoutChangeListener(this)
|
|
|
|
|
setResumedPage(targetPosition)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun cancel() {
|
|
|
|
|
view.removeOnLayoutChangeListener(this)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|