diff --git a/README.md b/README.md
index 6efa597..f42bff7 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@ A ratingbar for jetpack compose
[![](https://jitpack.io/v/a914-gowtham/compose-ratingbar/month.svg)](https://jitpack.io/#a914-gowtham/compose-ratingbar)
-
+
Download
--------
@@ -19,7 +19,7 @@ repositories {
// App build.gradle
dependencies {
- implementation 'com.github.a914-gowtham:compose-ratingbar:1.3.1'
+ implementation 'com.github.a914-gowtham:compose-ratingbar:1.3.3'
//mavenCentral
// implementation 'io.github.a914-gowtham:compose-ratingbar:1.2.3'
@@ -30,14 +30,13 @@ dependencies {
```kotlin
import androidx.compose.runtime.*
- var rating: Float by remember { mutableStateOf(initialRating) }
+ var rating: Float by remember { mutableStateOf(3.2f) }
RatingBar(
value = rating,
- config = RatingBarConfig()
- .style(RatingBarStyle.HighLighted),
+ style = RatingBarStyle.Fill(),
onValueChange = {
- rating = it
+ ratingOne = it
},
onRatingChanged = {
Log.d("TAG", "onRatingChanged: $it")
@@ -45,31 +44,31 @@ dependencies {
)
```
-Ratingbar composable can be customized using [RatingBarConfig](https://github.com/a914-gowtham/compose-ratingbar/blob/main/ratingbar/src/main/java/com/gowtham/ratingbar/RatingBarConfig.kt) class as shown below:
+#### Other optional params:
```kotlin
- RatingBarConfig()
- .activeColor(Color.Yellow)
- .hideInactiveStars(true)
- .inactiveColor(Color.LightGray)
- .inactiveBorderColor(Color.Blue)
- .stepSize(StepSize.HALF)
- .numStars(10)
- .isIndicator(true)
- .size(24.dp)
- .horizontalPadding(6.dp)
- .style(RatingBarStyle.HighLighted)
+ fun RatingBar(
+ value: Float,
+ modifier: Modifier = Modifier,
+ numOfStars: Int = 5,
+ size: Dp = 32.dp,
+ horizontalPadding: Dp = 6.dp,
+ isIndicator: Boolean = false,
+ stepSize: StepSize = StepSize.ONE,
+ hideInactiveStars: Boolean = false,
+ style: RatingBarStyle,
+ onValueChange: (Float) -> Unit,
+ onRatingChanged: (Float) -> Unit
+)
```
-## Customization
+## More Customization✨
+Icon can be changed using ```painterEmpty``` ```painterFilled``` params.
```kotlin
RatingBar(
value = rating,
- config = RatingBarConfig()
- .horizontalPadding(2.dp)
- .size(32.dp),
painterEmpty = painterResource(id = R.drawable.icon_empty),
painterFilled = painterResource(id = R.drawable.icon_filled),
onValueChange = {
diff --git a/app/src/main/java/com/gowtham/compose_ratingbar/MainActivity.kt b/app/src/main/java/com/gowtham/compose_ratingbar/MainActivity.kt
index 9f88025..cf6d7c2 100644
--- a/app/src/main/java/com/gowtham/compose_ratingbar/MainActivity.kt
+++ b/app/src/main/java/com/gowtham/compose_ratingbar/MainActivity.kt
@@ -6,7 +6,9 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.height
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
@@ -16,12 +18,12 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
+import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.gowtham.compose_ratingbar.MainActivity.Companion.initialRating
import com.gowtham.compose_ratingbar.ui.theme.JetpackComposeTheme
import com.gowtham.ratingbar.RatingBar
-import com.gowtham.ratingbar.RatingBarConfig
import com.gowtham.ratingbar.RatingBarStyle
import com.gowtham.ratingbar.StepSize
@@ -51,7 +53,8 @@ fun MyApp() {
@Composable
fun MainScreen() {
- var rating: Float by rememberSaveable { mutableStateOf(initialRating) }
+ var ratingOne: Float by rememberSaveable { mutableStateOf(1.4f) }
+ var ratingTwo: Float by rememberSaveable { mutableStateOf(initialRating) }
Column(
modifier = Modifier.fillMaxSize(),
@@ -59,14 +62,23 @@ fun MainScreen() {
verticalArrangement = Arrangement.Center,
) {
RatingBar(
- value = rating,
- config = RatingBarConfig()
- .horizontalPadding(6.dp)
- .size(28.dp)
- .stepSize(StepSize.HALF)
- .style(RatingBarStyle.HighLighted),
+ value = ratingOne,
+ stepSize = StepSize.HALF,
+ style = RatingBarStyle.Fill(),
onValueChange = {
- rating = it
+ ratingOne = it
+ },
+ onRatingChanged = {
+ Log.d("TAG", "onRatingChanged: $it")
+ }
+ )
+ Spacer(modifier = Modifier.height(30.dp))
+ RatingBar(
+ value = ratingTwo,
+ painterEmpty = painterResource(id = R.drawable.icon_empty),
+ painterFilled = painterResource(id = R.drawable.icon_filled),
+ onValueChange = {
+ ratingTwo = it
},
onRatingChanged = {
Log.d("TAG", "onRatingChanged: $it")
diff --git a/demo_1.gif b/demo_1.gif
index 0c52b7d..7473db2 100644
Binary files a/demo_1.gif and b/demo_1.gif differ
diff --git a/ratingbar/build.gradle b/ratingbar/build.gradle
index 98981b2..8a31816 100644
--- a/ratingbar/build.gradle
+++ b/ratingbar/build.gradle
@@ -13,7 +13,7 @@ afterEvaluate {
from components.release
groupId = 'com.gowtham.composeratingbar'
artifactId = 'compose-ratingbar'
- version = '1.3.1'
+ version = '1.3.3'
}
}
}
diff --git a/ratingbar/src/main/java/com/gowtham/ratingbar/RatingBar.kt b/ratingbar/src/main/java/com/gowtham/ratingbar/RatingBar.kt
index cc01a79..2edb392 100644
--- a/ratingbar/src/main/java/com/gowtham/ratingbar/RatingBar.kt
+++ b/ratingbar/src/main/java/com/gowtham/ratingbar/RatingBar.kt
@@ -10,6 +10,7 @@ import androidx.compose.runtime.*
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Size
+import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.pointerInteropFilter
@@ -21,6 +22,7 @@ import androidx.compose.ui.semantics.SemanticsPropertyKey
import androidx.compose.ui.semantics.SemanticsPropertyReceiver
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.toSize
@@ -30,9 +32,25 @@ sealed interface StepSize {
object HALF : StepSize
}
-sealed interface RatingBarStyle {
- object Normal : RatingBarStyle
- object HighLighted : RatingBarStyle
+sealed class RatingBarStyle(open val activeColor: Color) {
+ companion object {
+ val Default = Stroke()
+ }
+
+ open class Fill(
+ override val activeColor: Color = Color(0xFFFFCA00),
+ val inActiveColor: Color = Color(0x66FFCA00),
+ ) : RatingBarStyle(activeColor)
+
+ /**
+ * @param width width for each star
+ * @param color A border [Color] shown on inactive star.
+ */
+ class Stroke(
+ val width: Float = 1f,
+ override val activeColor: Color = Color(0xFFFFCA00),
+ val strokeColor: Color = Color(0xFF888888)
+ ) : RatingBarStyle(activeColor)
}
//For ui testing
@@ -41,21 +59,30 @@ var SemanticsPropertyReceiver.starRating by StarRatingKey
/**
- * Draws a Rating Bar on the screen according to the [RatingBarConfig] instance passed to the composable
- *
* @param value is current selected rating count
- * @param config the different configurations applied to the Rating Bar.
+ * @param numOfStars count of stars to be shown.
+ * @param size size for each star
+ * @param horizontalPadding padding between each star.
+ * @param isIndicator isIndicator Whether this rating bar is only an indicator or the value is changeable on user interaction.
+ * @param stepSize Can be [StepSize.ONE] or [StepSize.HALF]
+ * @param hideInactiveStars Whether the inactive stars should be hidden.
+ * @param style the different style applied to the Rating Bar.
* @param onRatingChanged A function to be called when the click or drag is released and rating value is passed
- * @see [RatingBarConfig]
*/
@OptIn(ExperimentalComposeUiApi::class)
@Composable
-fun RatingBar(
+internal fun RatingBar(
value: Float,
modifier: Modifier = Modifier,
- config: RatingBarConfig = RatingBarConfig(),
- painterEmpty: Painter?= null,
- painterFilled: Painter?= null,
+ numOfStars: Int = 5,
+ size: Dp = 32.dp,
+ horizontalPadding: Dp = 6.dp,
+ isIndicator: Boolean = false,
+ stepSize: StepSize = StepSize.ONE,
+ hideInactiveStars: Boolean = false,
+ style: RatingBarStyle = RatingBarStyle.Default,
+ painterEmpty: Painter? = null,
+ painterFilled: Painter? = null,
onValueChange: (Float) -> Unit,
onRatingChanged: (Float) -> Unit
) {
@@ -65,11 +92,11 @@ fun RatingBar(
val density = LocalDensity.current
- val paddingInPx = remember(config.horizontalPadding) {
- with(density) { config.horizontalPadding.toPx() }
+ val paddingInPx = remember {
+ with(density) { horizontalPadding.toPx() }
}
- val starSizeInPx = remember(config.size) {
- with(density) { config.size.toPx() }
+ val starSizeInPx = remember() {
+ with(density) { size.toPx() }
}
Row(modifier = modifier
@@ -80,7 +107,7 @@ fun RatingBar(
//handling dragging events
detectHorizontalDragGestures(
onDragEnd = {
- if (config.isIndicator || config.hideInactiveStars)
+ if (isIndicator || hideInactiveStars)
return@detectHorizontalDragGestures
onRatingChanged(lastDraggedValue)
},
@@ -91,7 +118,7 @@ fun RatingBar(
},
onHorizontalDrag = { change, _ ->
- if (config.isIndicator || config.hideInactiveStars)
+ if (isIndicator || hideInactiveStars)
return@detectHorizontalDragGestures
change.consume()
val dragX = change.position.x.coerceIn(-1f, rowSize.width)
@@ -99,19 +126,18 @@ fun RatingBar(
RatingBarUtils.calculateStars(
dragX,
paddingInPx,
- starSizeInPx,
- config
+ numOfStars, stepSize, starSizeInPx
)
if (direction == LayoutDirection.Rtl)
- calculatedStars = config.numStars - calculatedStars
+ calculatedStars = numOfStars - calculatedStars
onValueChange(calculatedStars)
lastDraggedValue = calculatedStars
}
)
}
.pointerInteropFilter {
- if (config.isIndicator || config.hideInactiveStars)
+ if (isIndicator || hideInactiveStars)
return@pointerInteropFilter false
//handling when click events
when (it.action) {
@@ -121,25 +147,100 @@ fun RatingBar(
RatingBarUtils.calculateStars(
dragX,
paddingInPx,
- starSizeInPx,
- config
+ numOfStars, stepSize, starSizeInPx
)
if (direction == LayoutDirection.Rtl)
- calculatedStars = config.numStars - calculatedStars
+ calculatedStars = numOfStars - calculatedStars
onValueChange(calculatedStars)
onRatingChanged(calculatedStars)
}
}
true
}) {
- ComposeStars(value, config,painterEmpty,painterFilled)
+ ComposeStars(
+ value,
+ numOfStars,
+ size,
+ horizontalPadding,
+ hideInactiveStars,
+ style = style,
+ painterEmpty,
+ painterFilled
+ )
}
}
+@Composable
+fun RatingBar(
+ value: Float,
+ modifier: Modifier = Modifier,
+ numOfStars: Int = 5,
+ size: Dp = 32.dp,
+ horizontalPadding: Dp = 6.dp,
+ isIndicator: Boolean = false,
+ stepSize: StepSize = StepSize.ONE,
+ hideInactiveStars: Boolean = false,
+ style: RatingBarStyle,
+ onValueChange: (Float) -> Unit,
+ onRatingChanged: (Float) -> Unit
+) {
+ RatingBar(
+ value = value,
+ modifier = modifier,
+ numOfStars = numOfStars,
+ size = size,
+ horizontalPadding = horizontalPadding,
+ isIndicator = isIndicator,
+ stepSize = stepSize,
+ hideInactiveStars = hideInactiveStars,
+ style = style,
+ painterEmpty = null,
+ painterFilled = null,
+ onValueChange = onValueChange,
+ onRatingChanged = onRatingChanged
+ )
+}
+
+@Composable
+fun RatingBar(
+ value: Float,
+ modifier: Modifier = Modifier,
+ numOfStars: Int = 5,
+ size: Dp = 32.dp,
+ horizontalPadding: Dp = 6.dp,
+ isIndicator: Boolean = false,
+ stepSize: StepSize = StepSize.ONE,
+ hideInactiveStars: Boolean = false,
+ painterEmpty: Painter,
+ painterFilled: Painter,
+ onValueChange: (Float) -> Unit,
+ onRatingChanged: (Float) -> Unit
+) {
+ RatingBar(
+ value = value,
+ modifier = modifier,
+ numOfStars = numOfStars,
+ size = size,
+ horizontalPadding = horizontalPadding,
+ isIndicator = isIndicator,
+ stepSize = stepSize,
+ hideInactiveStars = hideInactiveStars,
+ style = RatingBarStyle.Default,
+ painterEmpty = painterEmpty,
+ painterFilled = painterFilled,
+ onValueChange = onValueChange,
+ onRatingChanged = onRatingChanged
+ )
+}
+
@Composable
fun ComposeStars(
value: Float,
- config: RatingBarConfig,
+ numOfStars: Int,
+ size: Dp,
+ horizontalPadding: Dp,
+ hideInactiveStars: Boolean,
+ style: RatingBarStyle,
painterEmpty: Painter?,
painterFilled: Painter?
) {
@@ -149,35 +250,37 @@ fun ComposeStars(
Row(modifier = Modifier
.semantics { starRating = value }) {
- for (i in 1..config.numStars) {
+ for (i in 1..numOfStars) {
val starRating = when {
remainingRating == 0f -> {
0f
}
+
remainingRating >= ratingPerStar -> {
remainingRating -= ratingPerStar
1f
}
+
else -> {
val fraction = remainingRating / ratingPerStar
remainingRating = 0f
fraction
}
}
- if (config.hideInactiveStars && starRating == 0.0f)
+ if (hideInactiveStars && starRating == 0.0f)
break
RatingStar(
fraction = starRating,
- config = config,
+ style = style,
modifier = Modifier
.padding(
- start = if (i > 1) config.horizontalPadding else 0.dp,
- end = if (i < config.numStars) config.horizontalPadding else 0.dp
+ start = if (i > 1) horizontalPadding else 0.dp,
+ end = if (i < numOfStars) horizontalPadding else 0.dp
)
- .size(size = config.size)
+ .size(size = size)
.testTag("RatingStar"),
- painterEmpty, painterFilled
+ painterEmpty = painterEmpty, painterFilled = painterFilled
)
}
}
@@ -189,7 +292,7 @@ fun RatingBarPreview() {
var rating by remember { mutableStateOf(3.3f) }
RatingBar(
value = rating,
- config = RatingBarConfig(),
+ style = RatingBarStyle.Fill(),
onValueChange = {
rating = it
}
diff --git a/ratingbar/src/main/java/com/gowtham/ratingbar/RatingBarConfig.kt b/ratingbar/src/main/java/com/gowtham/ratingbar/RatingBarConfig.kt
deleted file mode 100644
index 2c05da4..0000000
--- a/ratingbar/src/main/java/com/gowtham/ratingbar/RatingBarConfig.kt
+++ /dev/null
@@ -1,195 +0,0 @@
-package com.gowtham.ratingbar
-
-import androidx.compose.ui.graphics.Color
-import androidx.compose.ui.unit.Dp
-import androidx.compose.ui.unit.dp
-
-/**
- * This is a helper class with the different configurations
- * required to be passed to the RatingBar composable.
- * ```kotlin
- * RatingBar(
- * config = RatingBarConfig()
- * .size(26.dp)
- * .padding(2.dp)
- * .style(RatingBarStyle.Normal)
- * .numStars(5)
- * .isIndicator(false)
- * .activeColor(Color(0xffffd740))
- * .inactiveColor(Color(0xffffecb3))
- * .stepSize(StepSize.ONE)
- * .hideInactiveStars(false),
- * onValueChanged = {/* code here */},
- * onRatingChanged = {/* code here */}
- * )
- * ```
- * @see [RatingBarStyle]
- */
-class RatingBarConfig {
-
- /**
- * size for each star.
- */
- var size: Dp = 26.dp
- private set
-
- /**
- * stroke width for each star
- */
- var strokeWidth: Float = 1f
- private set
-
- /**
- * padding between each star.
- */
- var horizontalPadding: Dp = 2.dp
- private set
-
- /**
- * Can be [RatingBarStyle.Normal] or [RatingBarStyle.HighLighted]
- */
- var style: RatingBarStyle = RatingBarStyle.Normal
- private set
-
- /**
- * numStars Sets the number of stars to show.
- */
- var numStars: Int = 5
- private set
-
- /**
- * isIndicator Whether this rating bar is only an indicator.
- */
- var isIndicator: Boolean = false
- private set
-
- /**
- * A [Color] representing an active star (or part of it).
- */
- var activeColor: Color = Color(0xffffd740)
- private set
-
- /**
- * A [Color] representing a inactive star (or part of it).
- */
- var inactiveColor: Color = Color(0xffffecb3)
- private set
-
- /**
- * A border [Color] shown on inactive star.
- */
- var inactiveBorderColor: Color = Color(0xFF888888)
- private set
-
- /**
- * Minimum value to trigger a change
- */
- var stepSize: StepSize = StepSize.ONE
- private set
-
- /**
- * Whether the inactive stars should be hidden.
- */
- var hideInactiveStars: Boolean = false
- private set
-
- /**
- * Sets the size of the star.
- * @param value the value in Dp
- */
- fun size(value: Dp): RatingBarConfig =
- apply {
- size = value
- }
-
- /**
- * Sets the stroke width of stars
- * @param value the value in Float
- */
- fun strokeWidth(value: Float): RatingBarConfig =
- apply {
- strokeWidth = value
- }
-
- /**
- * Sets the padding between star.
- * @param value the value in Dp.
- */
- fun horizontalPadding(value: Dp): RatingBarConfig =
- apply {
- horizontalPadding = value
- }
-
- /**
- * Sets the style of the composable.
- * @param value the value in Float.
- * @see [RatingBarStyle]
- */
- fun style(value: RatingBarStyle): RatingBarConfig =
- apply {
- style = value
- }
-
- /**
- * Sets whether this rating bar is only an indicator.
- * @param value the value in Boolean.
- */
- fun isIndicator(value: Boolean): RatingBarConfig =
- apply {
- isIndicator = value
- }
-
- /**
- * Sets the number of stars to show.
- * @param value the value in Int.
- */
- fun numStars(value: Int): RatingBarConfig =
- apply {
- numStars = value
- }
-
- /**
- * Sets the [Color] representing an active star (or part of it).
- * @param value the value in [Color]
- */
- fun activeColor(value: Color): RatingBarConfig =
- apply {
- activeColor = value
- }
-
- /**
- * Sets the [Color] representing a inactive star (or part of it).
- * @param value the value in [Color]
- */
- fun inactiveColor(value: Color): RatingBarConfig =
- apply {
- inactiveColor = value
- }
-
- /**
- * Sets the [Color] representing a inactive star (or part of it).
- * @param value the value in [Color]
- */
- fun inactiveBorderColor(value: Color): RatingBarConfig =
- apply {
- inactiveBorderColor = value
- }
-
- /**
- * Sets the minimum value to trigger a change.
- * @param value the value in [StepSize]
- */
- fun stepSize(value: StepSize): RatingBarConfig =
- apply {
- stepSize = value
- }
-
- /**
- * Whether the inactive stars should be hidden.
- * @param value the value in Boolean
- */
- fun hideInactiveStars(value: Boolean): RatingBarConfig =
- apply {
- hideInactiveStars = value
- }
-}
\ No newline at end of file
diff --git a/ratingbar/src/main/java/com/gowtham/ratingbar/RatingBarUtils.kt b/ratingbar/src/main/java/com/gowtham/ratingbar/RatingBarUtils.kt
index 33f3ee1..efe1362 100644
--- a/ratingbar/src/main/java/com/gowtham/ratingbar/RatingBarUtils.kt
+++ b/ratingbar/src/main/java/com/gowtham/ratingbar/RatingBarUtils.kt
@@ -5,8 +5,9 @@ object RatingBarUtils {
fun calculateStars(
draggedX: Float,
horizontalPaddingInPx: Float,
+ numOfStars: Int,
+ stepSize: StepSize,
starSizeInPx: Float,
- config: RatingBarConfig
): Float {
if(draggedX<=0){
@@ -15,9 +16,9 @@ object RatingBarUtils {
val starWidthWithRightPadding = starSizeInPx + (2 * horizontalPaddingInPx)
val halfStarWidth = starSizeInPx / 2
- for (i in 1..config.numStars) {
+ for (i in 1..numOfStars) {
if (draggedX < (i * starWidthWithRightPadding)) {
- return if (config.stepSize is StepSize.ONE) {
+ return if (stepSize is StepSize.ONE) {
i.toFloat()
} else {
val crossedStarsWidth = (i - 1) * starWidthWithRightPadding
diff --git a/ratingbar/src/main/java/com/gowtham/ratingbar/RatingStar.kt b/ratingbar/src/main/java/com/gowtham/ratingbar/RatingStar.kt
index b59ff38..9a580a0 100644
--- a/ratingbar/src/main/java/com/gowtham/ratingbar/RatingStar.kt
+++ b/ratingbar/src/main/java/com/gowtham/ratingbar/RatingStar.kt
@@ -22,22 +22,26 @@ import androidx.compose.ui.unit.dp
@Composable
fun RatingStar(
@FloatRange(from = 0.0, to = 1.0) fraction: Float,
- config: RatingBarConfig,
modifier: Modifier = Modifier,
+ style: RatingBarStyle,
painterEmpty: Painter?,
painterFilled: Painter?
) {
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl
-
Box(modifier = modifier) {
- FilledStar(fraction, config.activeColor, isRtl, config.strokeWidth, painterFilled)
- EmptyStar(fraction, config, isRtl, config.strokeWidth, painterEmpty)
+ FilledStar(
+ fraction,
+ style,
+ isRtl,
+ painterFilled
+ )
+ EmptyStar(fraction, style, isRtl, painterEmpty)
}
}
@Composable
private fun FilledStar(
- fraction: Float, activeColor: Color, isRtl: Boolean, strokeWidth: Float, painterFilled: Painter?
+ fraction: Float, style: RatingBarStyle, isRtl: Boolean, painterFilled: Painter?
) = Canvas(
modifier = Modifier
.fillMaxSize()
@@ -56,8 +60,12 @@ private fun FilledStar(
} else {
val path = Path().addStar(size)
- drawPath(path, color = activeColor, style = Fill) // Filled Star
- drawPath(path, color = activeColor, style = Stroke(width = strokeWidth)) // Border
+ drawPath(path, color = style.activeColor, style = Fill) // Filled Star
+ drawPath(
+ path,
+ color = style.activeColor,
+ style = Stroke(width = if (style is RatingBarStyle.Stroke) style.width else 1f)
+ ) // Border
}
@@ -66,9 +74,8 @@ private fun FilledStar(
@Composable
private fun EmptyStar(
fraction: Float,
- config: RatingBarConfig,
+ style: RatingBarStyle,
isRtl: Boolean,
- strokeWidth: Float,
painterEmpty: Painter?
) =
Canvas(
@@ -88,13 +95,13 @@ private fun EmptyStar(
}
} else {
val path = Path().addStar(size)
- if (config.style is RatingBarStyle.Normal) drawPath(
+ if (style is RatingBarStyle.Fill) drawPath(
path,
- color = config.inactiveColor,
+ color = style.inActiveColor,
style = Fill
) // Border
- else drawPath(
- path, color = config.inactiveBorderColor, style = Stroke(width = strokeWidth)
+ else if (style is RatingBarStyle.Stroke) drawPath(
+ path, color = style.strokeColor, style = Stroke(width = style.width)
) // Border
}
@@ -105,11 +112,13 @@ private fun EmptyStar(
fun EmptyRatingStarPreview() {
RatingStar(
fraction = 0f,
- config = RatingBarConfig().activeColor(Color(0xffffd740)).inactiveColor(Color.Gray)
- .style(RatingBarStyle.Normal),
+ style = RatingBarStyle.Fill(
+ activeColor = Color(0xffffd740),
+ inActiveColor = Color.Gray
+ ),
modifier = Modifier.size(20.dp),
- null,
- null
+ painterEmpty = null,
+ painterFilled = null
)
}
@@ -117,12 +126,14 @@ fun EmptyRatingStarPreview() {
@Composable
fun HighlightedWithBorderColorPreview() {
RatingStar(
- fraction = 0.5f,
- config = RatingBarConfig().activeColor(Color(0xffffd740)).inactiveBorderColor(Color.Blue)
- .style(RatingBarStyle.HighLighted),
+ fraction = 0.8f,
+ style = RatingBarStyle.Stroke(
+ activeColor = Color(0xffffd740),
+ strokeColor = Color.Blue
+ ),
modifier = Modifier.size(20.dp),
- null,
- null
+ painterEmpty = null,
+ painterFilled = null
)
}
@@ -130,12 +141,14 @@ fun HighlightedWithBorderColorPreview() {
@Composable
fun PartialRatingStarPreview() {
RatingStar(
- fraction = 0.7f,
- config = RatingBarConfig().activeColor(Color(0xffffd740)).inactiveColor(Color(0xffffd740))
- .style(RatingBarStyle.Normal),
+ fraction = 0.8f,
+ style = RatingBarStyle.Fill(
+ activeColor = Color(0xffffd740),
+ inActiveColor = Color(0x66FFD740)
+ ),
modifier = Modifier.size(20.dp),
- null,
- null
+ painterEmpty = null,
+ painterFilled = null
)
}
@@ -144,11 +157,13 @@ fun PartialRatingStarPreview() {
fun FullRatingStarPreview() {
RatingStar(
fraction = 1f,
- config = RatingBarConfig().activeColor(Color(0xffffd740)).inactiveColor(Color(0xffffd740))
- .style(RatingBarStyle.Normal),
+ style = RatingBarStyle.Fill(
+ activeColor = Color(0xffffd740),
+ inActiveColor = Color(0x66FFD740)
+ ),
modifier = Modifier.size(20.dp),
- null,
- null
+ painterEmpty = null,
+ painterFilled = null
)
}