aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-07-07 13:48:15 -0700
committerAllan Wang <me@allanwang.ca>2017-07-07 13:48:15 -0700
commitdfda38f585f609a4a0df4b5f21948d6222965b56 (patch)
treea42982ee38c8d6fad83bc927b3055aef789bd987 /core/src/main/kotlin
parent0a635fd19a1673d534d57e76f270981766cf6a2b (diff)
downloadkau-dfda38f585f609a4a0df4b5f21948d6222965b56.tar.gz
kau-dfda38f585f609a4a0df4b5f21948d6222965b56.tar.bz2
kau-dfda38f585f609a4a0df4b5f21948d6222965b56.zip
Create seekbar prefs
Diffstat (limited to 'core/src/main/kotlin')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt8
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt2
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt2
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt2
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt23
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefPlainText.kt2
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSeekbar.kt110
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSubItems.kt2
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt2
9 files changed, 144 insertions, 9 deletions
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
@@ -116,5 +116,13 @@ class KPrefAdapterBuilder(internal val globalOptions: GlobalOptions) {
.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<KPrefItemCore> = 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<Boolean>) : KPrefItemBase<Boolean>(builder) {
+open class KPrefCheckbox(builder: BaseContract<Boolean>) : KPrefItemBase<Boolean>(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<Int>(builder) {
+open class KPrefColorPicker(val builder: KPrefColorContract) : KPrefItemBase<Int>(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<KPrefItemCor
title.text = null
desc?.text = null
icon?.setImageDrawable(null)
-// innerFrame?.removeAllViews()
+ innerFrame?.removeAllViews()
+ lowerFrame?.removeAllViews()
}
}
@@ -107,19 +108,35 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem<KPrefItemCor
val desc: TextView? by bindOptionalView(R.id.kau_pref_desc)
val icon: ImageView? by bindOptionalView(R.id.kau_pref_icon)
val innerFrame: LinearLayout? by bindOptionalView(R.id.kau_pref_inner_frame)
+ val lowerFrame: LinearLayout? by bindOptionalView(R.id.kau_pref_lower_frame)
val innerContent: View?
get() = itemView.findViewById(R.id.kau_pref_inner_content)
+ val lowerContent: View?
+ get() = itemView.findViewById(R.id.kau_pref_lower_content)
+
+ inline fun <reified T : View> bindInnerView(@LayoutRes id: Int) = bindInnerView(id) { _: T -> }
- inline fun <reified T : View> bindInnerView(@LayoutRes id: Int): T {
+ inline fun <reified T : View> 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 <reified T : View> getInnerView() = innerContent as T
+ inline fun <reified T : View> bindLowerView(@LayoutRes id: Int) = bindLowerView(id) { _: T -> }
+
+ inline fun <reified T : View> 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<Unit>(builder) {
+open class KPrefPlainText(val builder: KPrefPlainTextBuilder) : KPrefItemBase<Unit>(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<Int>(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<TextView>(R.layout.kau_preference_seekbar_text)
+ if (textColor != null) text.setTextColor(textColor)
+ val tvc = builder.textViewConfigs
+
+ text.tvc()
+ val seekbar = viewHolder.bindLowerView<SeekBar>(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<Int> {
+ 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<Int> 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<T>(val builder: KPrefTextContract<T>) : KPrefItemBase<T>(builder) {
+open class KPrefText<T>(val builder: KPrefTextContract<T>) : KPrefItemBase<T>(builder) {
/**
* Automatically reload on set