Skip to content

Commit

Permalink
onRatingChanged fun changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowtham committed May 2, 2021
1 parent f4d6c3f commit 0defd8a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
3 changes: 1 addition & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.gowtham.compose_ratingbar.MainActivity.Companion.initialRating
import com.gowtham.compose_ratingbar.ui.theme.ComposeRatingBarTheme
import com.gowtham.ratingbar.LogMessage
import com.gowtham.ratingbar.RatingBar
import com.gowtham.ratingbar.RatingBarStyle
import com.gowtham.ratingbar.StepSize

class MainActivity : ComponentActivity() {
Expand Down Expand Up @@ -60,12 +61,9 @@ fun MainScreen() {
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
RatingBar(value = rating,onRatingDone = {
Log.d("TAG", "onRatingDone: $it")
},onRatingChanged = {
RatingBar(value = rating,onRatingChanged = {
rating=it
})
Spacer(modifier = Modifier.requiredSize(4.dp))
}
}

Expand Down
25 changes: 9 additions & 16 deletions ratingbar/src/main/java/com/gowtham/ratingbar/RatingBar.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.gowtham.ratingbar

import android.util.Log
import android.view.MotionEvent
import androidx.compose.foundation.layout.*
import androidx.compose.material.Surface
Expand Down Expand Up @@ -45,8 +44,7 @@ var SemanticsPropertyReceiver.starRating by StarRatingKey
* @param inactiveColor A [Color] representing a inactive star (or part of it)
* @param stepSize Minimum value to trigger a change
* @param ratingBarStyle Can be [RatingBarStyle.Normal] or [RatingBarStyle.HighLighted]
* @param onRatingDone triggers when the single click or dragging is done and lastRating value is passed in the param
* @param onRatingChanged A function to be called when the value changes
* @param onRatingChanged A function to be called when the click or drag is released and rating value is passed
*/
@Composable
fun RatingBar(
Expand All @@ -60,16 +58,17 @@ fun RatingBar(
inactiveColor: Color = Color(0xffffecb3),
stepSize: StepSize = StepSize.ONE,
ratingBarStyle: RatingBarStyle = RatingBarStyle.Normal,
onRatingDone: (Float) -> Unit,
onRatingChanged: (Float) -> Unit
) {
var rowSize by remember { mutableStateOf(Size.Zero) }
var lastKnownValue=0f
var rating by remember { mutableStateOf(value) }

Surface {
Row(modifier = modifier
.onSizeChanged { rowSize = it.toSize() }
.pointerInteropFilter {
if(isIndicator)
return@pointerInteropFilter false
when (it.action) {
MotionEvent.ACTION_DOWN -> {
//handling when single click happens
Expand All @@ -78,9 +77,7 @@ fun RatingBar(
it.x, rowSize.width,
numStars, padding.value.toInt()
)
val rating=calculatedStars.stepSized(stepSize)
onRatingChanged(rating)
lastKnownValue=rating
rating=calculatedStars.stepSized(stepSize)
}
MotionEvent.ACTION_MOVE -> {
//handling while dragging event
Expand All @@ -90,19 +87,17 @@ fun RatingBar(
x1, rowSize.width,
numStars, padding.value.toInt()
)
val rating=calculatedStars.stepSized(stepSize)
onRatingChanged(rating)
lastKnownValue=rating
rating=calculatedStars.stepSized(stepSize)
}
MotionEvent.ACTION_UP -> {
//when the click or drag is released
onRatingDone(lastKnownValue)
onRatingChanged(rating)
}
}
true
}) {
ComposeStars(
value, numStars, size, padding, activeColor,
rating, numStars, size, padding, activeColor,
inactiveColor, ratingBarStyle
)
}
Expand Down Expand Up @@ -162,9 +157,7 @@ fun ComposeStars(
fun RatingBarPreview() {
var rating by remember { mutableStateOf(3.3f) }

RatingBar(value = rating,onRatingDone = {
Log.d("TAG", "onRatingDone: $it")
},onRatingChanged = {
RatingBar(value = rating,onRatingChanged = {
rating=it
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ object RatingBarUtils {
else {
var value = this.toInt().toFloat()
if (this < value.plus(0.5)) {
if(this==0f)
return 0f
value = value.plus(0.5).toFloat()
value
} else
Expand Down

0 comments on commit 0defd8a

Please sign in to comment.