Skip to content

Commit

Permalink
better variable names in sparkbutton compose
Browse files Browse the repository at this point in the history
  • Loading branch information
connyduck committed Mar 12, 2024
1 parent 116fe62 commit 6867d47
Showing 1 changed file with 50 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ public fun SparkButton(
Animatable(0.0f)
}

val dotsSizeProgress = remember {
val largeDotSizeProgress = remember {
Animatable(0.0f)
}

val dotsSizeProgress2 = remember {
val smallDotSizeProgress = remember {
Animatable(0.0f)
}

Expand Down Expand Up @@ -129,7 +129,7 @@ public fun SparkButton(
}

launch {
dotsSizeProgress.animateTo(
largeDotSizeProgress.animateTo(
targetValue = 0f,
animationSpec =
keyframes {
Expand All @@ -143,7 +143,7 @@ public fun SparkButton(
}

launch {
dotsSizeProgress2.animateTo(
smallDotSizeProgress.animateTo(
targetValue = 0f,
animationSpec =
keyframes {
Expand Down Expand Up @@ -178,8 +178,7 @@ public fun SparkButton(
}

Box(
modifier =
modifier
modifier = modifier
.toggleable(
value = checked,
interactionSource = interactionSource,
Expand All @@ -196,15 +195,15 @@ public fun SparkButton(
if (enabled) {
1f
} else {
disabledAlpha
DISABLED_ALPHA
}
)
.drawBehind {
val maxDotSize = this.size.maxDimension / 24

val maxOuterDotsRadius: Float = this.size.maxDimension * 1.5f

val currentRadius1: Float =
val outerDotsRadius: Float =
mapValueFromRangeToRange(
dotsRadiusProgress.value,
0.0f,
Expand All @@ -213,7 +212,7 @@ public fun SparkButton(
maxOuterDotsRadius
)

val currentRadius2: Float =
val middleDotsRadius: Float =
mapValueFromRangeToRange(
dotsRadiusProgress.value,
0.0f,
Expand All @@ -222,7 +221,7 @@ public fun SparkButton(
maxOuterDotsRadius / 8 * 7
)

val currentRadius3: Float =
val innerDotsRadius: Float =
mapValueFromRangeToRange(
dotsRadiusProgress.value,
0.0f,
Expand All @@ -231,74 +230,71 @@ public fun SparkButton(
maxOuterDotsRadius / 4 * 3
)

val dotColors =
if (dotsRadiusProgress.value < 0.5f) {
val progress =
mapValueFromRangeToRange(
dotsRadiusProgress.value,
0.0f,
0.5f,
0.0f,
1.0f
)
listOf(
primaryColor.interpolate(primaryColorDark, progress),
primaryColorDark.interpolate(secondaryColor, progress),
secondaryColor.interpolate(secondaryColorDark, progress),
secondaryColorDark.interpolate(primaryColor, progress)
)
} else {
val progress =
mapValueFromRangeToRange(
dotsRadiusProgress.value,
0.5f,
1.0f,
0.0f,
1.0f
)
listOf(
primaryColorDark.interpolate(primaryColor, progress),
secondaryColor.interpolate(primaryColorDark, progress),
secondaryColorDark.interpolate(secondaryColor, progress),
primaryColor.interpolate(secondaryColorDark, progress)
)
}
val dotColors = if (dotsRadiusProgress.value < 0.5f) {
val progress = mapValueFromRangeToRange(
dotsRadiusProgress.value,
0.0f,
0.5f,
0.0f,
1.0f
)
listOf(
primaryColor.interpolate(primaryColorDark, progress),
primaryColorDark.interpolate(secondaryColor, progress),
secondaryColor.interpolate(secondaryColorDark, progress),
secondaryColorDark.interpolate(primaryColor, progress)
)
} else {
val progress = mapValueFromRangeToRange(
dotsRadiusProgress.value,
0.5f,
1.0f,
0.0f,
1.0f
)
listOf(
primaryColorDark.interpolate(primaryColor, progress),
secondaryColor.interpolate(primaryColorDark, progress),
secondaryColorDark.interpolate(secondaryColor, progress),
primaryColor.interpolate(secondaryColorDark, progress)
)
}

// outer Dots
for (i in 0 until DOT_COUNT) {
val cX: Float =
(center.x + currentRadius1 * cos(i * DOT_POSITION_ANGLE * PI / 180f)).toFloat()
(center.x + outerDotsRadius * cos(i * DOT_POSITION_ANGLE * PI / 180f)).toFloat()
val cY: Float =
(center.y + currentRadius1 * sin(i * DOT_POSITION_ANGLE * PI / 180f)).toFloat()
(center.y + outerDotsRadius * sin(i * DOT_POSITION_ANGLE * PI / 180f)).toFloat()
drawCircle(
color = dotColors[i % dotColors.size],
radius = dotsSizeProgress2.value * maxDotSize,
radius = smallDotSizeProgress.value * maxDotSize,
center = Offset(cX, cY)
)
}

// innerDots
// middle Dots (larger)
for (i in 0 until DOT_COUNT) {
val cX: Float =
(center.x + currentRadius2 * cos((i * DOT_POSITION_ANGLE - DOT_POSITION_ANGLE / 2f) * PI / 180f)).toFloat()
(center.x + middleDotsRadius * cos((i * DOT_POSITION_ANGLE - DOT_POSITION_ANGLE / 2f) * PI / 180f)).toFloat()
val cY: Float =
(center.y + currentRadius2 * sin((i * DOT_POSITION_ANGLE - DOT_POSITION_ANGLE / 2f) * PI / 180f)).toFloat()
(center.y + middleDotsRadius * sin((i * DOT_POSITION_ANGLE - DOT_POSITION_ANGLE / 2f) * PI / 180f)).toFloat()
drawCircle(
color = dotColors[(i + 1) % dotColors.size],
radius = dotsSizeProgress.value * 2 * maxDotSize,
radius = largeDotSizeProgress.value * 2 * maxDotSize,
center = Offset(cX, cY)
)
}

// innerDots
// inner Dots
for (i in 0 until DOT_COUNT) {
val cX: Float =
(center.x + currentRadius3 * cos((i * DOT_POSITION_ANGLE) * PI / 180f)).toFloat()
(center.x + innerDotsRadius * cos((i * DOT_POSITION_ANGLE) * PI / 180f)).toFloat()
val cY: Float =
(center.y + currentRadius3 * sin((i * DOT_POSITION_ANGLE) * PI / 180f)).toFloat()
(center.y + innerDotsRadius * sin((i * DOT_POSITION_ANGLE) * PI / 180f)).toFloat()
drawCircle(
color = dotColors[(i + 1) % dotColors.size],
radius = dotsSizeProgress2.value * maxDotSize,
radius = smallDotSizeProgress.value * maxDotSize,
center = Offset(cX, cY)
)
}
Expand All @@ -314,4 +310,4 @@ private const val DOT_POSITION_ANGLE: Float = 360f / DOT_COUNT

private val SlowOutFastInEasing = CubicBezierEasing(0.8f, 0.0f, 0.4f, 1.0f)

private val disabledAlpha = 0.38f
private const val DISABLED_ALPHA = 0.38f

0 comments on commit 6867d47

Please sign in to comment.