Skip to content

Commit

Permalink
Merge pull request #34 from a914-gowtham/refactor/ratingbar_usage
Browse files Browse the repository at this point in the history
Refactor/ratingbar usage
  • Loading branch information
a914-gowtham authored May 27, 2023
2 parents 8741e11 + f873706 commit 490ed44
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 295 deletions.
43 changes: 21 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


<img src="https://github.com/a914-gowtham/compose-ratingbar/blob/main/demo_1.gif" width="340" height="260"/>
<img src="https://github.com/a914-gowtham/compose-ratingbar/blob/refactor/ratingbar_usage/demo_1.gif" width="340" height="260"/>

Download
--------
Expand All @@ -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'
Expand All @@ -30,46 +30,45 @@ 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")
}
)
```

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

<img src="https://github.com/a914-gowtham/compose-ratingbar/blob/main/customization_demo.gif" width="340" height="260"/>

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 = {
Expand Down
30 changes: 21 additions & 9 deletions app/src/main/java/com/gowtham/compose_ratingbar/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -51,22 +53,32 @@ 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(),
horizontalAlignment = Alignment.CenterHorizontally,
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")
Expand Down
Binary file modified demo_1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion ratingbar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ afterEvaluate {
from components.release
groupId = 'com.gowtham.composeratingbar'
artifactId = 'compose-ratingbar'
version = '1.3.1'
version = '1.3.3'
}
}
}
Expand Down
Loading

0 comments on commit 490ed44

Please sign in to comment.