/sspi-calculateSegments.kt Secret
Created
December 7, 2022 22:17
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun calculateSegments( | |
height: Float, | |
width: Float, | |
trackSegments: List<ProgressIndicatorSegment>, | |
trackColor: Color, | |
cornerRadiusDp: Dp, | |
strokeWidth: Dp, | |
paddingDp: Dp, | |
localDensity: Density | |
): List<SegmentGroups> { | |
val cornerRadius = with(localDensity) { cornerRadiusDp.toPx() } | |
val stroke = with(localDensity) { Stroke(width = strokeWidth.toPx()) } | |
val padding = with(localDensity) { paddingDp.toPx() } | |
val naturalWeight = trackSegments.sumOf { it.weight.toDouble() }.toFloat() | |
val edgeLength = | |
(2 * (height - 2 * cornerRadius)) + (2 * (width - 2 * cornerRadius)) + (2 * Math.PI.toFloat() * cornerRadius) | |
val paddingWeight = padding / edgeLength * naturalWeight | |
val paddedWeight = (paddingWeight * trackSegments.size) + naturalWeight | |
val measures = Measures(width, height, cornerRadius, stroke) | |
var startWeight = 0f | |
var startPaddingWeight = 0f | |
return buildList { | |
trackSegments.forEachIndexed { segmentIndex, trackSegment -> | |
val localTrackColor = trackSegment.trackColor ?: trackColor | |
val indicatorColor = trackSegment.indicatorColor | |
val paddedRange = | |
((startWeight + startPaddingWeight) / paddedWeight)..((startWeight + startPaddingWeight + trackSegment.weight) / paddedWeight) | |
val splitSegments = measures.splitSegments( | |
indicatorColor = indicatorColor, | |
trackColor = localTrackColor, | |
range = paddedRange | |
) | |
add( | |
SegmentGroups( | |
groupNumber = segmentIndex, | |
indicatorColor = indicatorColor, | |
trackColor = localTrackColor, | |
calculatedSegments = splitSegments | |
) | |
) | |
startWeight += trackSegment.weight | |
startPaddingWeight += paddingWeight | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment