diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/RecyclerView.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/RecyclerView.kt index 0dd4d0cf2..e2170048f 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/RecyclerView.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/RecyclerView.kt @@ -37,13 +37,6 @@ val RecyclerView.visibleItemCount: Int findLastVisibleItemPosition() - findFirstVisibleItemPosition() } ?: 0 -fun RecyclerView.findCenterViewPosition(): Int { - val centerX = width / 2f - val centerY = height / 2f - val view = findChildViewUnder(centerX, centerY) ?: return RecyclerView.NO_POSITION - return getChildAdapterPosition(view) -} - fun RecyclerView.ViewHolder.getItem(clazz: Class): T? { val rawItem = when (this) { is AdapterDelegateViewBindingViewHolder<*, *> -> item diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonReaderFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonReaderFragment.kt index d532aae06..460cc4856 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonReaderFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/webtoon/WebtoonReaderFragment.kt @@ -4,6 +4,7 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.ViewGroup import android.view.animation.DecelerateInterpolator +import androidx.recyclerview.widget.RecyclerView import com.google.android.material.snackbar.Snackbar import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.coroutineScope @@ -13,7 +14,6 @@ import kotlinx.coroutines.yield import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.os.NetworkState import org.koitharu.kotatsu.core.ui.list.lifecycle.RecyclerViewLifecycleDispatcher -import org.koitharu.kotatsu.core.util.ext.findCenterViewPosition import org.koitharu.kotatsu.core.util.ext.firstVisibleItemPosition import org.koitharu.kotatsu.core.util.ext.observe import org.koitharu.kotatsu.core.util.ext.removeItemDecoration @@ -127,14 +127,13 @@ class WebtoonReaderFragment : BaseReaderFragment() } override fun getCurrentState(): ReaderState? = viewBinding?.run { - val currentItem = recyclerView.findCenterViewPosition() + val currentItem = recyclerView.findCurrentPagePosition() val adapter = recyclerView.adapter as? BaseReaderAdapter<*> val page = adapter?.getItemOrNull(currentItem) ?: return@run null ReaderState( chapterId = page.chapterId, page = page.index, - scroll = (recyclerView.findViewHolderForAdapterPosition(currentItem) as? WebtoonHolder) - ?.getScrollY() ?: 0, + scroll = (recyclerView.findViewHolderForAdapterPosition(currentItem) as? WebtoonHolder)?.getScrollY() ?: 0, ) } @@ -168,4 +167,14 @@ class WebtoonReaderFragment : BaseReaderFragment() } return true } + + private fun RecyclerView.findCurrentPagePosition(): Int { + val centerX = width / 2f + val centerY = height - resources.getDimension(R.dimen.webtoon_pages_gap) + if (centerY <= 0) { + return RecyclerView.NO_POSITION + } + val view = findChildViewUnder(centerX, centerY) ?: return RecyclerView.NO_POSITION + return getChildAdapterPosition(view) + } }