Skip to content

Commit

Permalink
Remove use of reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
diareuse committed Nov 27, 2023
1 parent ae509e4 commit 69d54f5
Showing 1 changed file with 5 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ class MultiChildPagerState(
super.scroll(scrollPriority) {
val scope = ScrollScope { pixels ->
scrollBy(pixels).also { _ ->
for (child in children)
child.snapToItem(currentPage, currentPageOffsetFraction)
val currentPage = currentPage
val currentPageOffsetFraction = currentPageOffsetFraction
for (child in children) with(child) {
updateCurrentPage(currentPage, currentPageOffsetFraction)
}
}
}
scope.block()
Expand All @@ -46,30 +49,9 @@ class MultiChildPagerState(
}
}

private fun PagerState.snapToItem(page: Int, offset: Float) {
val actualOffset = offset * pageAvailableSpace.invoke(this) as Int
snapToItem.invoke(this, page, actualOffset.toInt())
}

operator fun component1() = this
operator fun component2() = children[0]

companion object {

private val pagerState = PagerState::class.java
private val pageAvailableSpace = pagerState.getDeclaredMethod("getPageAvailableSpace")
private val snapToItem = pagerState.getDeclaredMethod(
"snapToItem\$foundation_release",
Int::class.java,
Int::class.java
)

init {
pageAvailableSpace.isAccessible = true
snapToItem.isAccessible = true
}
}

}

@Composable
Expand Down

0 comments on commit 69d54f5

Please sign in to comment.