From 5ad60050c91791638d90b5469bcb22e7b340af0c Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Wed, 2 May 2018 21:22:55 -0400 Subject: Fix seekbar increments, resolves #145 (#147) * Fix seekbar increments, resolves #145 * Update changelog --- .../kau/kpref/activity/items/KPrefSeekbar.kt | 23 ++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'kpref-activity/src/main/kotlin') 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(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.defaultOnClick() = Unit override fun bindView(holder: ViewHolder, payloads: List) { @@ -26,8 +35,7 @@ open class KPrefSeekbar(val builder: KPrefSeekbarContract) : KPrefItemBase( text.tvc() val seekbar = holder.bindLowerView(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( 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 -- cgit v1.2.3