Skip to content

Commit

Permalink
fix: rtl support added
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowtham committed Nov 11, 2021
1 parent 305cafb commit 46f17c3
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

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

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

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

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
implementation project(path: ':ratingbar')
implementation "com.github.instacart.truetime-android:library:3.4"

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.gowtham.compose_ratingbar

import android.app.Application
import com.instacart.library.truetime.TrueTime
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.logging.Handler

class MApplication : Application() {

Expand Down
19 changes: 11 additions & 8 deletions app/src/main/java/com/gowtham/compose_ratingbar/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.LayoutDirection
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.RatingBarStyle
import kotlin.math.absoluteValue
import kotlin.math.log
import com.gowtham.ratingbar.StepSize
import com.instacart.library.truetime.TrueTime
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*

class MainActivity : ComponentActivity() {

Expand Down Expand Up @@ -47,7 +53,7 @@ fun MyApp() {

@Composable
fun MainScreen() {
var rating: Float by rememberSaveable { mutableStateOf(initialRating) }
var rating: Float by rememberSaveable { mutableStateOf(2.8f) }

Column(
modifier =
Expand All @@ -56,15 +62,12 @@ fun MainScreen() {
verticalArrangement = Arrangement.Center,
) {
RatingBar(value = rating,
size = 20.dp,
padding = 30.dp,
numStars = 3,
ratingBarStyle = RatingBarStyle.HighLighted, onValueChange = {
ratingBarStyle = RatingBarStyle.HighLighted,
onValueChange = {
rating = it
}) {
Log.d("TAG", "onRatingChanged: $it")
}

}
}

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.1'
classpath 'com.android.tools.build:gradle:7.0.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}"
// classpath 'com.vanniktech:gradle-maven-publish-plugin:0.15.1'
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.15.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
11 changes: 6 additions & 5 deletions ratingbar/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
// id 'com.vanniktech.maven.publish'
id 'com.vanniktech.maven.publish'
id 'maven-publish'
}

Expand All @@ -19,21 +19,21 @@ afterEvaluate {
}
}

/*allprojects {
allprojects {
plugins.withId("com.vanniktech.maven.publish") {
mavenPublish {
sonatypeHost = "S01"
}
}
}*/
}

android {
compileSdkVersion 30
compileSdkVersion 31
buildToolsVersion "31.0.0"

defaultConfig {
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 31
versionCode 1
versionName "1.0"

Expand Down Expand Up @@ -71,5 +71,6 @@ dependencies {
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"

}

21 changes: 19 additions & 2 deletions ratingbar/src/main/java/com/gowtham/ratingbar/RatingBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ package com.gowtham.ratingbar
import android.util.Log
import android.view.MotionEvent
import androidx.compose.foundation.layout.*
import androidx.compose.material.Text
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.input.pointer.pointerInteropFilter
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.platform.testTag
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
import com.gowtham.ratingbar.RatingBarUtils.stepSized
Expand Down Expand Up @@ -66,6 +71,7 @@ fun RatingBar(
) {
var rowSize by remember { mutableStateOf(Size.Zero) }
var changedValue by remember { mutableStateOf(0f) }
val direction = LocalLayoutDirection.current

Row(modifier = modifier
.onSizeChanged { rowSize = it.toSize() }
Expand All @@ -80,7 +86,12 @@ fun RatingBar(
it.x, rowSize.width,
numStars, padding.value.toInt()
)
val newValue = calculatedStars.stepSized(stepSize)
var newValue =
calculatedStars
.stepSized(stepSize)
.coerceIn(0f, numStars.toFloat())
if (direction == LayoutDirection.Rtl)
newValue = numStars - newValue
onValueChange(newValue)
onRatingChanged(changedValue)
}
Expand All @@ -92,7 +103,13 @@ fun RatingBar(
x1, rowSize.width,
numStars, padding.value.toInt()
)
val newValue = calculatedStars.stepSized(stepSize)
var newValue =
calculatedStars
.stepSized(stepSize)
.coerceIn(0f, numStars.toFloat())

if (direction == LayoutDirection.Rtl)
newValue = numStars - newValue
onValueChange(newValue)
changedValue = newValue
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.gowtham.ratingbar

import android.util.Log
import kotlin.math.roundToInt

object RatingBarUtils {
Expand Down
85 changes: 61 additions & 24 deletions ratingbar/src/main/java/com/gowtham/ratingbar/RatingStar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.*
import androidx.compose.ui.graphics.drawscope.Fill
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import com.gowtham.ratingbar.FractionalRectangleShape
import com.gowtham.ratingbar.addStar


private const val strokeWidth = 1f
Expand All @@ -25,18 +25,26 @@ fun RatingStar(
modifier: Modifier = Modifier,
activeColor: Color,
inactiveColor: Color,
ratingBarStyle: RatingBarStyle) {
ratingBarStyle: RatingBarStyle
) {
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl

Box(modifier = modifier) {
FilledStar(fraction,activeColor)
EmptyStar(fraction,ratingBarStyle,inactiveColor)
FilledStar(fraction, activeColor, isRtl)
EmptyStar(fraction, ratingBarStyle, inactiveColor, isRtl)
}
}

@Composable
private fun FilledStar(fraction: Float,activeColor: Color) = Canvas(
private fun FilledStar(fraction: Float, activeColor: Color, isRtl: Boolean) = Canvas(
modifier = Modifier
.fillMaxSize()
.clip(FractionalRectangleShape(0f, fraction))
.clip(
if (isRtl)
rtlFilledStarFractionalShape(fraction = fraction)
else
FractionalRectangleShape(0f, fraction)
)
) {
val path = Path().addStar(size)

Expand All @@ -45,41 +53,70 @@ private fun FilledStar(fraction: Float,activeColor: Color) = Canvas(
}

@Composable
private fun EmptyStar(fraction: Float,ratingBarStyle: RatingBarStyle,inactiveColor: Color) = Canvas(
modifier = Modifier
.fillMaxSize()
.clip(FractionalRectangleShape(fraction, 1f))
) {
val path = Path().addStar(size)
if(ratingBarStyle is RatingBarStyle.Normal)
drawPath(path, color = inactiveColor, style = Fill) // Border
else
drawPath(path, color = Color.Gray, style = Stroke(width = strokeWidth)) // Border
}
private fun EmptyStar(
fraction: Float,
ratingBarStyle: RatingBarStyle,
inactiveColor: Color,
isRtl: Boolean
) =
Canvas(
modifier = Modifier
.fillMaxSize()
.clip(
if (isRtl)
rtlEmptyStarFractionalShape(fraction = fraction)
else
FractionalRectangleShape(fraction, 1f)
)
) {
val path = Path().addStar(size)
if (ratingBarStyle is RatingBarStyle.Normal)
drawPath(path, color = inactiveColor, style = Fill) // Border
else
drawPath(path, color = Color.Gray, style = Stroke(width = strokeWidth)) // Border
}

@Preview(showBackground = true)
@Composable
fun EmptyRatingStarPreview() {
RatingStar(fraction = 0f, modifier = Modifier.size(20.dp),
RatingStar(
fraction = 0f, modifier = Modifier.size(20.dp),
Color(0xffffd740),
Color(0xffffecb3),
RatingBarStyle.Normal)
RatingBarStyle.Normal
)
}

@Preview(showBackground = true)
@Composable
fun PartialRatingStarPreview() {
RatingStar(fraction = 0.7f, modifier = Modifier.size(20.dp),
RatingStar(
fraction = 0.7f, modifier = Modifier.size(20.dp),
Color(0xffffd740),
Color(0xffffecb3),
RatingBarStyle.Normal)
RatingBarStyle.Normal
)
}

@Preview(showBackground = true)
@Composable
fun FullRatingStarPreview() {
RatingStar(fraction = 1f, modifier = Modifier.size(20.dp),
RatingStar(
fraction = 1f, modifier = Modifier.size(20.dp),
Color(0xffffd740),
Color(0xffffecb3),
RatingBarStyle.Normal)
RatingBarStyle.Normal
)
}

fun rtlEmptyStarFractionalShape(fraction: Float): FractionalRectangleShape {
return if (fraction == 1f || fraction == 0f)
FractionalRectangleShape(fraction, 1f)
else FractionalRectangleShape(0f, 1f - fraction)
}

fun rtlFilledStarFractionalShape(fraction: Float): FractionalRectangleShape {
return if (fraction == 0f || fraction == 1f)
FractionalRectangleShape(0f, fraction)
else FractionalRectangleShape(1f - fraction, 1f)
}

0 comments on commit 46f17c3

Please sign in to comment.