diff --git a/ratings/src/main/java/com/decathlon/vitamin/compose/ratings/Icon.kt b/ratings/src/main/java/com/decathlon/vitamin/compose/ratings/Icon.kt index 86212742..668e0d8e 100644 --- a/ratings/src/main/java/com/decathlon/vitamin/compose/ratings/Icon.kt +++ b/ratings/src/main/java/com/decathlon/vitamin/compose/ratings/Icon.kt @@ -15,16 +15,22 @@ internal sealed class Icon(val imageVector: ImageVector) { object Fill : Icon(imageVector = VitaminIcons.Fill.Star) companion object { - private const val HALF = 0.5f + private const val EMPTY_LOWER_BOUND = 0f + private const val EMPTY_UPPER_BOUND = 0.24f + private const val HALF_LOWER_BOUND = 0.25f + private const val HALF_UPPER_BOUND = 0.75f + private const val FILL_LOWER_BOUND = 0.76f + private const val FILL_UPPER_BOUND = 1f + fun get(index: Int, number: Float): Icon { val floor = floor(number).toInt() val decimal = number - index - return if (floor == index && decimal != 0f) { - if (decimal < HALF) Half else Fill - } else if (index < number) { - Fill - } else { - Empty + return when { + index < floor -> Fill + decimal in EMPTY_LOWER_BOUND..EMPTY_UPPER_BOUND -> Empty + decimal in HALF_LOWER_BOUND..HALF_UPPER_BOUND -> Half + decimal in FILL_LOWER_BOUND..FILL_UPPER_BOUND -> Fill + else -> Empty } } } diff --git a/ratings/src/test/kotlin/com/decathlon/vitamin/compose/ratings/IconTest.kt b/ratings/src/test/kotlin/com/decathlon/vitamin/compose/ratings/IconTest.kt new file mode 100644 index 00000000..67323049 --- /dev/null +++ b/ratings/src/test/kotlin/com/decathlon/vitamin/compose/ratings/IconTest.kt @@ -0,0 +1,72 @@ +package com.decathlon.vitamin.compose.ratings + +import org.junit.Assert.* +import org.junit.Test + +class IconTest { + @Test + fun integer() { + val number = 3.0f + + assertEquals(Icon.Fill, Icon.get(0, number)) + assertEquals(Icon.Fill, Icon.get(1, number)) + assertEquals(Icon.Fill, Icon.get(2, number)) + assertEquals(Icon.Empty, Icon.get(3, number)) + assertEquals(Icon.Empty, Icon.get(4, number)) + } + + @Test + fun close_above_int() { + val number = 3.2f + + assertEquals(Icon.Fill, Icon.get(0, number)) + assertEquals(Icon.Fill, Icon.get(1, number)) + assertEquals(Icon.Fill, Icon.get(2, number)) + assertEquals(Icon.Empty, Icon.get(3, number)) + assertEquals(Icon.Empty, Icon.get(4, number)) + } + + @Test + fun close_below_half() { + val number = 3.4f + + assertEquals(Icon.Fill, Icon.get(0, number)) + assertEquals(Icon.Fill, Icon.get(1, number)) + assertEquals(Icon.Fill, Icon.get(2, number)) + assertEquals(Icon.Half, Icon.get(3, number)) + assertEquals(Icon.Empty, Icon.get(4, number)) + } + + @Test + fun strictly_half() { + val number = 3.5f + + assertEquals(Icon.Fill, Icon.get(0, number)) + assertEquals(Icon.Fill, Icon.get(1, number)) + assertEquals(Icon.Fill, Icon.get(2, number)) + assertEquals(Icon.Half, Icon.get(3, number)) + assertEquals(Icon.Empty, Icon.get(4, number)) + } + + @Test + fun close_above_half() { + val number = 3.6f + + assertEquals(Icon.Fill, Icon.get(0, number)) + assertEquals(Icon.Fill, Icon.get(1, number)) + assertEquals(Icon.Fill, Icon.get(2, number)) + assertEquals(Icon.Half, Icon.get(3, number)) + assertEquals(Icon.Empty, Icon.get(4, number)) + } + + @Test + fun close_below_int() { + val number = 3.8f + + assertEquals(Icon.Fill, Icon.get(0, number)) + assertEquals(Icon.Fill, Icon.get(1, number)) + assertEquals(Icon.Fill, Icon.get(2, number)) + assertEquals(Icon.Fill, Icon.get(3, number)) + assertEquals(Icon.Empty, Icon.get(4, number)) + } +}