Skip to content

Commit

Permalink
Add ability to wrap around rating selection 0->5->0 (#300)
Browse files Browse the repository at this point in the history
Closes #283 


Pressing left on a zero rating will wrap around to 5 stars (or 10.0
decimal). And pressing right on a five star or 10.0 decimal rating to
wrap around to zero.

This makes it easier/faster to pick a higher value.
  • Loading branch information
damontecres authored Jun 18, 2024
1 parent 85ee660 commit 11b4059
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.damontecres.stashapp.views

import android.content.Context
import android.util.AttributeSet
import android.view.KeyEvent
import androidx.appcompat.widget.AppCompatSeekBar

class DecimalRatingBar(context: Context, attrs: AttributeSet?) : AppCompatSeekBar(context, attrs) {
override fun onKeyDown(
keyCode: Int,
event: KeyEvent?,
): Boolean {
if (super.isEnabled()) {
if ((keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_MINUS) && progress <= 0) {
progress = 100
return true
} else if ((keyCode == KeyEvent.KEYCODE_DPAD_RIGHT || keyCode == KeyEvent.KEYCODE_PLUS) && progress >= 100) {
progress = 0
return true
}
}
return super.onKeyDown(keyCode, event)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.damontecres.stashapp.views

import android.content.Context
import android.util.AttributeSet
import android.view.KeyEvent
import androidx.appcompat.widget.AppCompatRatingBar

class StarRatingBar(context: Context, attrs: AttributeSet?) : AppCompatRatingBar(context, attrs) {
override fun onKeyDown(
keyCode: Int,
event: KeyEvent?,
): Boolean {
if (super.isEnabled()) {
if ((keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_MINUS) && rating < .05f) {
rating = 5f
return true
} else if ((keyCode == KeyEvent.KEYCODE_DPAD_RIGHT || keyCode == KeyEvent.KEYCODE_PLUS) && rating > 4.95f) {
rating = 0f
return true
}
}
return super.onKeyDown(keyCode, event)
}
}
5 changes: 3 additions & 2 deletions app/src/main/res/layout/stash_rating_bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<RatingBar

<com.github.damontecres.stashapp.views.StarRatingBar
android:id="@+id/rating_star"
android:layout_width="wrap_content"
android:layout_height="match_parent"
Expand All @@ -27,7 +28,7 @@
android:textAlignment="center"
tools:text="@string/stashapp_rating" />

<SeekBar
<com.github.damontecres.stashapp.views.DecimalRatingBar
android:id="@+id/rating_decimal"
android:layout_width="match_parent"
android:minWidth="160dp"
Expand Down

0 comments on commit 11b4059

Please sign in to comment.