From dfda38f585f609a4a0df4b5f21948d6222965b56 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Fri, 7 Jul 2017 13:48:15 -0700 Subject: Create seekbar prefs --- .../kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt | 8 ++ .../ca/allanwang/kau/kpref/items/KPrefCheckbox.kt | 2 +- .../allanwang/kau/kpref/items/KPrefColorPicker.kt | 2 +- .../ca/allanwang/kau/kpref/items/KPrefHeader.kt | 2 +- .../ca/allanwang/kau/kpref/items/KPrefItemCore.kt | 23 ++++- .../ca/allanwang/kau/kpref/items/KPrefPlainText.kt | 2 +- .../ca/allanwang/kau/kpref/items/KPrefSeekbar.kt | 110 +++++++++++++++++++++ .../ca/allanwang/kau/kpref/items/KPrefSubItems.kt | 2 +- .../ca/allanwang/kau/kpref/items/KPrefText.kt | 2 +- 9 files changed, 144 insertions(+), 9 deletions(-) create mode 100644 core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSeekbar.kt (limited to 'core/src/main/kotlin') diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt index 7f42d2a..974e6cf 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt @@ -115,6 +115,14 @@ class KPrefAdapterBuilder(internal val globalOptions: GlobalOptions) { = list.add(KPrefPlainText(KPrefPlainText.KPrefPlainTextBuilder(globalOptions, title) .apply { builder() })) + @KPrefMarker + fun seekbar(@StringRes title: Int, + getter: (() -> Int), + setter: ((value: Int) -> Unit), + builder: KPrefSeekbar.KPrefSeekbarContract.() -> Unit = {}) + = list.add(KPrefSeekbar(KPrefSeekbar.KPrefSeekbarBuilder(globalOptions, title, getter, setter) + .apply { builder() })) + @KPrefMarker internal val list: MutableList = mutableListOf() } \ No newline at end of file diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt index 22cc927..999a718 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt @@ -12,7 +12,7 @@ import ca.allanwang.kau.utils.tint * Checkbox preference * When clicked, will toggle the preference and the apply the result to the checkbox */ -class KPrefCheckbox(builder: BaseContract) : KPrefItemBase(builder) { +open class KPrefCheckbox(builder: BaseContract) : KPrefItemBase(builder) { override fun defaultOnClick(itemView: View, innerContent: View?): Boolean { pref = !pref diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt index c573939..762bcf4 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt @@ -16,7 +16,7 @@ import ca.allanwang.kau.kpref.KPrefMarker * ColorPicker preference * When a color is successfully selected in the dialog, it will be saved as an int */ -class KPrefColorPicker(val builder: KPrefColorContract) : KPrefItemBase(builder) { +open class KPrefColorPicker(val builder: KPrefColorContract) : KPrefItemBase(builder) { override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { super.onPostBindView(viewHolder, textColor, accentColor) diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt index fa8efff..4068496 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt @@ -10,7 +10,7 @@ import ca.allanwang.kau.kpref.KPrefMarker * Header preference * This view just holds a title and is not clickable. It is styled using the accent color */ -class KPrefHeader(builder: CoreContract) : KPrefItemCore(builder) { +open class KPrefHeader(builder: CoreContract) : KPrefItemCore(builder) { override fun getLayoutRes(): Int = R.layout.kau_preference_header diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt index 5f684ba..18d1bae 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt @@ -67,7 +67,8 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem bindInnerView(@LayoutRes id: Int) = bindInnerView(id) { _: T -> } - inline fun bindInnerView(@LayoutRes id: Int): T { + inline fun bindInnerView(@LayoutRes id: Int, onFirstBind: (T) -> Unit): T { if (innerFrame == null) throw IllegalStateException("Cannot bind inner view when innerFrame does not exist") if (innerContent !is T) { innerFrame!!.removeAllViews() LayoutInflater.from(innerFrame!!.context).inflate(id, innerFrame) + onFirstBind(innerContent as T) } return innerContent as T } - inline fun getInnerView() = innerContent as T + inline fun bindLowerView(@LayoutRes id: Int) = bindLowerView(id) { _: T -> } + + inline fun bindLowerView(@LayoutRes id: Int, onFirstBind: (T) -> Unit): T { + if (lowerFrame == null) throw IllegalStateException("Cannot bind inner view when lowerContent does not exist") + if (lowerContent !is T) { + lowerFrame!!.removeAllViews() + LayoutInflater.from(lowerFrame!!.context).inflate(id, lowerFrame) + onFirstBind(lowerContent as T) + } + return lowerContent as T + } operator fun get(@IdRes id: Int): View = itemView.findViewById(id) } diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefPlainText.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefPlainText.kt index a782430..6f7adf8 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefPlainText.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefPlainText.kt @@ -12,7 +12,7 @@ import ca.allanwang.kau.kpref.GlobalOptions * and when the preference is completely handled by the click * */ -class KPrefPlainText(val builder: KPrefPlainTextBuilder) : KPrefItemBase(builder) { +open class KPrefPlainText(val builder: KPrefPlainTextBuilder) : KPrefItemBase(builder) { override fun defaultOnClick(itemView: View, innerContent: View?): Boolean { //nothing diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSeekbar.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSeekbar.kt new file mode 100644 index 0000000..a6897a3 --- /dev/null +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSeekbar.kt @@ -0,0 +1,110 @@ +package ca.allanwang.kau.kpref.items + +import android.view.View +import android.widget.SeekBar +import android.widget.TextView +import ca.allanwang.kau.R +import ca.allanwang.kau.kpref.GlobalOptions +import ca.allanwang.kau.kpref.KPrefException +import ca.allanwang.kau.logging.KL +import ca.allanwang.kau.utils.tint + +/** + * Created by Allan Wang on 2017-06-07. + * + * Checkbox preference + * When clicked, will toggle the preference and the apply the result to the checkbox + */ +open class KPrefSeekbar(val builder: KPrefSeekbarContract) : KPrefItemBase(builder) { + + + override fun defaultOnClick(itemView: View, innerContent: View?): Boolean = false + + override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { + super.onPostBindView(viewHolder, textColor, accentColor) + val text = viewHolder.bindInnerView(R.layout.kau_preference_seekbar_text) + if (textColor != null) text.setTextColor(textColor) + val tvc = builder.textViewConfigs + + text.tvc() + val seekbar = viewHolder.bindLowerView(R.layout.kau_preference_seekbar) { + it.max = builder.range + it.incrementProgressBy(builder.increments) + it.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { + override fun onProgressChanged(sb: SeekBar, progress: Int, fromUser: Boolean) { + text.text = builder.toText(progress.fromProgress) + } + + override fun onStartTrackingTouch(sb: SeekBar) { + + } + + override fun onStopTrackingTouch(sb: SeekBar) { + val trueProgress = sb.progress.fromProgress + pref = trueProgress + KL.d("New pref $trueProgress") + } + }) + } + if (accentColor != null) seekbar.tint(accentColor) + seekbar.progress = pref.toProgress + } + + /** + * Extension of the base contract + */ + interface KPrefSeekbarContract : BaseContract { + var min: Int + var max: Int + var increments: Int + var range: Int + /** + * Once a seekbar is let go, calculates what text to show in the text view + */ + var toText: (Int) -> String + var textViewConfigs: TextView.() -> Unit + } + + /** + * Default implementation of [KPrefSeekbarContract] + */ + class KPrefSeekbarBuilder( + globalOptions: GlobalOptions, + titleRes: Int, + getter: () -> Int, + setter: (value: Int) -> Unit + ) : KPrefSeekbarContract, BaseContract by BaseBuilder(globalOptions, titleRes, getter, setter) { + + override var min: Int = 0 + set(value) { + field = value + range = -1 + } + override var max: Int = 100 + set(value) { + field = value + range = -1 + } + override var increments: Int = 1 + + override var range: Int = max - min + //value doesn't matter; setting will prompt the check + set(value) { + if (max <= min) throw KPrefException("Range min ($min) must be smaller than max ($max)") + field = max - min + } + + override var toText: (Int) -> String = { it.toString() } + + override var textViewConfigs: TextView.() -> Unit = {} + } + + val Int.toProgress: Int + get() = this - builder.min + + val Int.fromProgress: Int + get() = this + builder.min + + override fun getType(): Int = R.id.kau_item_pref_seekbar + +} \ No newline at end of file diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSubItems.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSubItems.kt index 51625ab..f373ec4 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSubItems.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSubItems.kt @@ -12,7 +12,7 @@ import ca.allanwang.kau.kpref.KPrefAdapterBuilder * When clicked, will navigate to a new set of preferences and add the old list to a stack * */ -class KPrefSubItems(val builder: KPrefSubItemsContract) : KPrefItemCore(builder) { +open class KPrefSubItems(val builder: KPrefSubItemsContract) : KPrefItemCore(builder) { override fun onClick(itemView: View, innerContent: View?): Boolean { builder.globalOptions.showNextPrefs(builder.titleRes, builder.itemBuilder) diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt index 8662b6a..9c44cd4 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt @@ -14,7 +14,7 @@ import ca.allanwang.kau.utils.toast * This is still a generic preference * */ -class KPrefText(val builder: KPrefTextContract) : KPrefItemBase(builder) { +open class KPrefText(val builder: KPrefTextContract) : KPrefItemBase(builder) { /** * Automatically reload on set -- cgit v1.2.3