Skip to content

Commit

Permalink
Merge pull request #8 from saran2020/drag_rating
Browse files Browse the repository at this point in the history
Improve the round off logic of the drag rating
  • Loading branch information
saran2020 authored Jul 13, 2019
2 parents 23819a4 + 61fa719 commit 82dc138
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 35 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/github/saran2020/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ class MainActivity : AppCompatActivity() {

override fun onPause() {
super.onPause()
val rating = ratingView?.getRating() ?: 0
val rating = ratingView?.rating ?: 0
Log.d("buggy_bug", "current rating is $rating")
}

override fun onResume() {
super.onResume()
ratingView?.setRating(4f)
ratingView?.rating = 4f
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.android.tools.build:gradle:3.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4"
classpath "com.github.dcendents:android-maven-gradle-plugin:2.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.widget.ImageView
import java.util.SortedMap
import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.min

open class DragRatingView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
Expand All @@ -24,7 +25,7 @@ open class DragRatingView @JvmOverloads constructor(
private var mTouchDownX = 0f
private var ratingSpace = 0.0f
private var maxRating = 5
private var currentRating = 0f
private var _currentRating = 0f
set(value) {
if (value > maxRating) {
throw IllegalArgumentException("Rating cannot be more than max rating")
Expand All @@ -42,10 +43,16 @@ open class DragRatingView @JvmOverloads constructor(
field = newRating
callback?.onRatingChange(previousRating, field)

Log.d("buggy_bug", "current rating $currentRating")
Log.d("buggy_bug", "current rating $_currentRating")
refreshRatingView()
}

var rating: Float
get() = _currentRating
set(value) {
_currentRating = value
}

private fun refreshRatingView() {
for (i in 0..childCount) {
val childAt = getChildAt(i)
Expand All @@ -60,31 +67,46 @@ open class DragRatingView @JvmOverloads constructor(
// We multiply the decimal value by 100 so that we can convert the
// decimal to ints It gives us more control on rounding off to the
// nearest available.
private fun roundOffRating(value: Float): Float {
val decimalMultiplied = (value - floor(value)) * 100
private fun roundOffRating(rating: Float): Float {

// If the rating is 3.2, decimalRating will hold 0.2
val decimalRating = (rating - floor(rating))
val decimalMultiplied = decimalRating * 100

val keys = assetMap.keys.toList()
var previous = 0
for (current in 1 until assetMap.size) {
val currentMultiple = keys[current] * 100
val previousMultiple = keys[previous] * 100

if (decimalMultiplied > previousMultiple && decimalMultiplied < currentMultiple) {
val differPreviousMultiple = decimalMultiplied - previousMultiple
val differCurrentMultiple = currentMultiple - decimalMultiplied

val minDifference = Math.min(differCurrentMultiple, differPreviousMultiple)
return when {
differPreviousMultiple == differCurrentMultiple -> floor(value) + keys[current]
minDifference == differPreviousMultiple -> floor(value) + keys[previous]
else -> floor(value) + keys[current]
for (step in 1 until assetMap.size) {

val previousStep = step - 1

val currentMultiple = keys[step] * 100
val previousMultiple = keys[previousStep] * 100

if (previousStep == 0) {

// If it is first step return the closest key from the user supplied
// key value map. This is being to handle the zero rating case.
if (decimalMultiplied > previousMultiple && decimalMultiplied < currentMultiple) {
val differPreviousMultiple = decimalMultiplied - previousMultiple
val differCurrentMultiple = currentMultiple - decimalMultiplied

val minDifference = min(differCurrentMultiple, differPreviousMultiple)
return when {
differPreviousMultiple == differCurrentMultiple -> floor(rating) + keys[step]
minDifference == differPreviousMultiple -> floor(rating) + keys[previousStep]
else -> floor(rating) + keys[step]
}
}
}
} else {

previous = current
// If it's not first step just return the next bigger key
// from the user supplied key value map.
if (currentMultiple > decimalMultiplied) {
return floor(rating) + keys[step]
}
}
}

return value
return rating
}

private var assetMap: SortedMap<Float, Drawable> = convertToDrawableMap(
Expand All @@ -108,7 +130,7 @@ open class DragRatingView @JvmOverloads constructor(
maxRating = typedArray.getInt(
R.styleable.DragRatingView_max_rating, 5
)
currentRating = typedArray.getFloat(
_currentRating = typedArray.getFloat(
R.styleable.DragRatingView_initial_rating, 0f
)

Expand Down Expand Up @@ -200,7 +222,7 @@ open class DragRatingView @JvmOverloads constructor(
val dragOnView = x - child.left
val ratioCross = dragOnView / child.width.toFloat()

currentRating = i + ratioCross
_currentRating = i + ratioCross
}
}
}
Expand Down Expand Up @@ -241,9 +263,9 @@ open class DragRatingView @JvmOverloads constructor(

imageView.setImageDrawable(
when {
pos <= floor(currentRating) -> assetMap[1f]!!
pos == ceil(currentRating).toInt() -> {
val decimal = currentRating - floor(currentRating)
pos <= floor(_currentRating) -> assetMap[1f]!!
pos == ceil(_currentRating).toInt() -> {
val decimal = _currentRating - floor(_currentRating)
assetMap[decimal]!!
}
else -> assetMap[0f]!!
Expand All @@ -262,12 +284,6 @@ open class DragRatingView @JvmOverloads constructor(
return sortedMap
}

fun getRating(): Float = currentRating

fun setRating(rating: Float) {
currentRating = rating
}

fun setDrawableAssetMap(map: Map<Float, Drawable>) {
assetMap = map.toSortedMap()

Expand Down

0 comments on commit 82dc138

Please sign in to comment.