aboutsummaryrefslogtreecommitdiff
path: root/kpref-activity
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-05-02 21:22:55 -0400
committerGitHub <noreply@github.com>2018-05-02 21:22:55 -0400
commit5ad60050c91791638d90b5469bcb22e7b340af0c (patch)
treeffe92d0206ac9753f88a44076c070571409961ce /kpref-activity
parentb85fc4f939a443368cea296bfa8439745419d887 (diff)
downloadkau-5ad60050c91791638d90b5469bcb22e7b340af0c.tar.gz
kau-5ad60050c91791638d90b5469bcb22e7b340af0c.tar.bz2
kau-5ad60050c91791638d90b5469bcb22e7b340af0c.zip
Fix seekbar increments, resolves #145 (#147)
* Fix seekbar increments, resolves #145 * Update changelog
Diffstat (limited to 'kpref-activity')
-rw-r--r--kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSeekbar.kt23
1 files changed, 19 insertions, 4 deletions
diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSeekbar.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSeekbar.kt
index a580e83..78f4f80 100644
--- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSeekbar.kt
+++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSeekbar.kt
@@ -15,6 +15,15 @@ import ca.allanwang.kau.utils.tint
*/
open class KPrefSeekbar(val builder: KPrefSeekbarContract) : KPrefItemBase<Int>(builder) {
+ protected val min = builder.min
+ protected val max = builder.max
+ protected val increment = builder.increments
+
+ init {
+ if (increment <= 0)
+ throw IllegalArgumentException("Seekbar must increment by at least 1")
+ }
+
override fun KClick<Int>.defaultOnClick() = Unit
override fun bindView(holder: ViewHolder, payloads: List<Any>) {
@@ -26,8 +35,7 @@ open class KPrefSeekbar(val builder: KPrefSeekbarContract) : KPrefItemBase<Int>(
text.tvc()
val seekbar = holder.bindLowerView<SeekBar>(R.layout.kau_pref_seekbar) {
- it.max = builder.max - builder.min
- it.incrementProgressBy(builder.increments)
+ it.max = (max - min) / increment
it.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(sb: SeekBar, progress: Int, fromUser: Boolean) {
text.text = builder.toText(progress.fromProgress)
@@ -81,11 +89,18 @@ open class KPrefSeekbar(val builder: KPrefSeekbarContract) : KPrefItemBase<Int>(
override var textViewConfigs: TextView.() -> Unit = {}
}
+ /**
+ * Helper to convert true value to progress value
+ * Progress values start at 0 and increment by 1
+ */
protected inline val Int.toProgress: Int
- get() = this - builder.min
+ get() = (this - min) / increment
+ /**
+ * Inverse of [Int.toProgress] to find the true value from the seekbar
+ */
protected inline val Int.fromProgress: Int
- get() = this + builder.min
+ get() = this * increment + min
override fun getType(): Int = R.id.kau_item_pref_seekbar