Skip to content

Commit

Permalink
refactor internal code
Browse files Browse the repository at this point in the history
  • Loading branch information
shpasha committed Nov 8, 2024
1 parent e2c70cd commit eb401d8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=0.0.6
version=0.0.7
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
android.useAndroidX=true
kotlin.code.style=official
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package mx.platacard.pagerindicator.internal

internal fun calculateDrawableDotIndices(
internal fun calculateVisibleDotIndices(
dotCount: Int,
currentPage: Int,
pageCount: Int,
): Pair<Int, Int> {

val onTheSidesDotCount = dotCount / 2

val firstVisible: Int
val lastVisible: Int

if (currentPage < pageCount / 2) {
firstVisible = (currentPage - onTheSidesDotCount).coerceAtLeast(0)
lastVisible = (firstVisible + onTheSidesDotCount * 2)
firstVisible = (currentPage - dotCount / 2).coerceAtLeast(0)
lastVisible = (firstVisible + (dotCount - 1))
} else {
lastVisible = (currentPage + onTheSidesDotCount).coerceAtMost(pageCount - 1)
firstVisible = (lastVisible - onTheSidesDotCount * 2)
lastVisible = (currentPage + dotCount / 2).coerceAtMost(pageCount - 1)
firstVisible = (lastVisible - (dotCount - 1))
}

return firstVisible to lastVisible
}

Expand All @@ -30,7 +29,7 @@ internal fun calculateTargetDotSizeForPage(

val onTheSidesDotCount = dotCount / 2

val (firstVisible, lastVisible) = calculateDrawableDotIndices(
val (firstVisible, lastVisible) = calculateVisibleDotIndices(
dotCount = dotCount,
currentPage = currentPage,
pageCount = pageCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ internal fun PagerIndicatorInternal(
onAfterDraw: DrawScope.() -> Unit = {},
) {

val adjustedDotCount = minOf(dotCount, pageCount)
val adjustedDotCount = when {
dotCount >= pageCount -> pageCount
else -> if (dotCount % 2 == 0) dotCount - 1 else dotCount
}

val density = LocalDensity.current

Expand Down Expand Up @@ -94,28 +97,18 @@ internal fun PagerIndicatorInternal(
.height(if (orientation == Horizontal) activeDotSize else mainAxisSize)
) {

val centerDot = pageCount / 2

val pagerFraction = currentPageFraction.value
val pagesFromCenter = pagerFraction - centerDot

val additionalOffset = if (pageCount % 2 == 0 && adjustedDotCount != pageCount) 1 else 0

val mainAxisCenterX = if (orientation == Horizontal) center.x else center.y

val firstItemStart = mainAxisCenterX - (pageCount / 2f + additionalOffset) * dotSizePx -
((pageCount - 1 - additionalOffset) / 2f) * spacePx
val itemsCount = (pagerFraction - adjustedDotCount / 2)
.coerceIn(
minimumValue = 0f,
maximumValue = pageCount - adjustedDotCount.toFloat(),
)

val scroll = when {
adjustedDotCount == pageCount -> 0f
else -> -pagesFromCenter.coerceIn(
minimumValue = (adjustedDotCount / 2 - centerDot).toFloat(),
maximumValue = (-adjustedDotCount / 2 + centerDot - additionalOffset).toFloat(),
) * (dotSizePx + spacePx)
} + firstItemStart
val scroll = -itemsCount * (dotSizePx + spacePx)

val (firstVisible, lastVisible) = calculateDrawableDotIndices(
dotCount = dotCount,
val (firstVisible, lastVisible) = calculateVisibleDotIndices(
dotCount = adjustedDotCount,
currentPage = pagerFraction.roundToInt(),
pageCount = pageCount,
).let { (first, second) ->
Expand Down Expand Up @@ -189,7 +182,7 @@ internal fun Modifier.onDotClick(
): Modifier {
return this.pointerInput(Unit) {
detectTapGestures { offset ->
val (start, stop) = calculateDrawableDotIndices(
val (start, stop) = calculateVisibleDotIndices(
dotCount = dotCount,
currentPage = currentPageFraction.value.roundToInt(),
pageCount = pageCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ class SampleActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Column(
modifier = Modifier
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.SpaceEvenly,
) {
HorizontalPagerSample()
VerticalPagerSample()
Box {
Column(
modifier = Modifier
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.SpaceEvenly,
) {
HorizontalPagerSample()
VerticalPagerSample()
}
}
}
}
Expand Down

0 comments on commit eb401d8

Please sign in to comment.