Skip to content

Commit

Permalink
Make worm indicator internal
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-megh-l committed Jan 3, 2024
1 parent e95cbdf commit e889844
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@ import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.RoundRect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.canopas.lib.bottombar.R
import com.example.bottombar.utils.customWormTransition

@Composable
internal fun LineIndicator(
Expand Down Expand Up @@ -114,30 +110,4 @@ internal fun FilledIndicator(
.clip(indicatorShape)
.background(indicatorColor, shape = indicatorShape)
)
}

fun Modifier.customWormTransition(
offset: Dp,
indicatorColor: Color,
itemWidth: Dp
) = composed {
drawWithContent {
val distance = itemWidth.roundToPx()
val scrollPosition = (offset.toPx().div(distance))
val wormOffset = (scrollPosition % 1) * 2

val xPos = scrollPosition.toInt() * distance
val head = xPos + distance * 0f.coerceAtLeast(wormOffset - 1)
val tail = xPos + size.width + distance * 1f.coerceAtMost(wormOffset)

val worm = RoundRect(
head, 0f, tail, size.height,
CornerRadius(50f)
)

val path = Path().apply { addRoundRect(worm) }

// Draw the worm shape on top of the content
drawPath(path = path, color = indicatorColor)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package com.example.bottombar.utils
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.RoundRect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.unit.Dp

internal fun Modifier.conditional(
condition: Boolean,
Expand All @@ -14,4 +20,30 @@ internal fun Modifier.conditional(
} else {
orElse?.invoke(this) ?: this
}
}

internal fun Modifier.customWormTransition(
offset: Dp,
indicatorColor: Color,
itemWidth: Dp
) = composed {
drawWithContent {
val distance = itemWidth.roundToPx()
val scrollPosition = (offset.toPx().div(distance))
val wormOffset = (scrollPosition % 1) * 2

val xPos = scrollPosition.toInt() * distance
val head = xPos + distance * 0f.coerceAtLeast(wormOffset - 1)
val tail = xPos + size.width + distance * 1f.coerceAtMost(wormOffset)

val worm = RoundRect(
head, 0f, tail, size.height,
CornerRadius(50f)
)

val path = Path().apply { addRoundRect(worm) }

// Draw the worm shape on top of the content
drawPath(path = path, color = indicatorColor)
}
}

0 comments on commit e889844

Please sign in to comment.