diff options
author | Allan Wang <me@allanwang.ca> | 2017-07-07 13:48:15 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-07-07 13:48:15 -0700 |
commit | dfda38f585f609a4a0df4b5f21948d6222965b56 (patch) | |
tree | a42982ee38c8d6fad83bc927b3055aef789bd987 /core/src | |
parent | 0a635fd19a1673d534d57e76f270981766cf6a2b (diff) | |
download | kau-dfda38f585f609a4a0df4b5f21948d6222965b56.tar.gz kau-dfda38f585f609a4a0df4b5f21948d6222965b56.tar.bz2 kau-dfda38f585f609a4a0df4b5f21948d6222965b56.zip |
Create seekbar prefs
Diffstat (limited to 'core/src')
16 files changed, 189 insertions, 18 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 diff --git a/core/src/main/res/layout/kau_preference.xml b/core/src/main/res/layout/kau_preference.xml index c49951b..7ee4f01 100644 --- a/core/src/main/res/layout/kau_preference.xml +++ b/core/src/main/res/layout/kau_preference.xml @@ -40,7 +40,7 @@ android:id="@+id/kau_pref_title" android:layout_width="0dp" android:layout_height="wrap_content" - android:layout_marginTop="16dp" + android:layout_marginTop="@dimen/kau_padding_normal" android:ellipsize="marquee" android:textAppearance="?android:attr/textAppearanceListItem" android:textColor="?android:attr/textColorPrimary" @@ -49,23 +49,36 @@ app:layout_constraintHorizontal_bias="0" app:layout_constraintStart_toEndOf="@id/kau_pref_icon" app:layout_constraintTop_toTopOf="parent" - app:layout_goneMarginBottom="16dp" + app:layout_goneMarginBottom="@dimen/kau_padding_normal" tools:layout_editor_absoluteX="-175dp" /> <TextView android:id="@id/kau_pref_desc" android:layout_width="0dp" android:layout_height="wrap_content" - android:layout_marginBottom="16dp" android:ellipsize="end" android:maxLines="10" android:textAppearance="?android:attr/textAppearanceListItemSecondary" android:textColor="?android:attr/textColorSecondary" - app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintBottom_toTopOf="@+id/kau_pref_lower_frame" app:layout_constraintEnd_toStartOf="@id/kau_pref_inner_frame" app:layout_constraintHorizontal_bias="0" app:layout_constraintStart_toEndOf="@id/kau_pref_icon" app:layout_constraintTop_toBottomOf="@id/kau_pref_title" + app:layout_goneMarginBottom="@dimen/kau_padding_normal" + tools:layout_editor_absoluteX="-175dp" /> + + <LinearLayout + android:id="@id/kau_pref_lower_frame" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_marginBottom="@dimen/kau_padding_normal" + android:orientation="vertical" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toStartOf="@id/kau_pref_inner_frame" + app:layout_constraintHorizontal_bias="0" + app:layout_constraintStart_toEndOf="@id/kau_pref_icon" + app:layout_constraintTop_toBottomOf="@id/kau_pref_desc" tools:layout_editor_absoluteX="-175dp" /> <android.support.constraint.Barrier @@ -73,7 +86,7 @@ android:layout_width="1dp" android:layout_height="wrap_content" app:barrierDirection="end" - app:constraint_referenced_ids="kau_pref_title,kau_pref_desc" + app:constraint_referenced_ids="kau_pref_title,kau_pref_desc,kau_pref_lower_frame" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" /> @@ -83,7 +96,7 @@ android:layout_height="0dp" android:gravity="center_vertical|end" android:orientation="horizontal" - android:paddingStart="16dp" + android:paddingStart="@dimen/kau_padding_normal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" diff --git a/core/src/main/res/layout/kau_preference_checkbox.xml b/core/src/main/res/layout/kau_preference_checkbox.xml index 5ab368b..016394f 100644 --- a/core/src/main/res/layout/kau_preference_checkbox.xml +++ b/core/src/main/res/layout/kau_preference_checkbox.xml @@ -1,5 +1,5 @@ <CheckBox xmlns:android="http://schemas.android.com/apk/res/android" - android:id="@+id/kau_pref_inner_content" + android:id="@id/kau_pref_inner_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" diff --git a/core/src/main/res/layout/kau_preference_color_preview.xml b/core/src/main/res/layout/kau_preference_color_preview.xml index 2374971..18ca35a 100644 --- a/core/src/main/res/layout/kau_preference_color_preview.xml +++ b/core/src/main/res/layout/kau_preference_color_preview.xml @@ -1,5 +1,5 @@ <ca.allanwang.kau.dialogs.color.CircleView xmlns:android="http://schemas.android.com/apk/res/android" - android:id="@+id/kau_pref_inner_content" + android:id="@id/kau_pref_inner_content" android:layout_width="40dp" android:layout_height="40dp" android:focusable="false" diff --git a/core/src/main/res/layout/kau_preference_seekbar.xml b/core/src/main/res/layout/kau_preference_seekbar.xml new file mode 100644 index 0000000..8da4d5d --- /dev/null +++ b/core/src/main/res/layout/kau_preference_seekbar.xml @@ -0,0 +1,8 @@ +<SeekBar xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@id/kau_pref_lower_content" + android:layout_width="match_parent" + android:paddingTop="@dimen/kau_padding_normal" + android:layout_height="wrap_content" + android:focusable="false" + android:clickable="false" + android:background="@null" />
\ No newline at end of file diff --git a/core/src/main/res/layout/kau_preference_seekbar_text.xml b/core/src/main/res/layout/kau_preference_seekbar_text.xml new file mode 100644 index 0000000..6ba2543 --- /dev/null +++ b/core/src/main/res/layout/kau_preference_seekbar_text.xml @@ -0,0 +1,12 @@ +<!--TextView that aligns to the bottom--> + +<TextView xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@id/kau_pref_inner_content" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="bottom" + android:background="@null" + android:clickable="false" + android:focusable="false" + android:gravity="bottom|end" + android:paddingBottom="@dimen/kau_padding_normal" />
\ No newline at end of file diff --git a/core/src/main/res/layout/kau_preference_text.xml b/core/src/main/res/layout/kau_preference_text.xml index bbabdfb..a4d901e 100644 --- a/core/src/main/res/layout/kau_preference_text.xml +++ b/core/src/main/res/layout/kau_preference_text.xml @@ -1,5 +1,5 @@ <TextView xmlns:android="http://schemas.android.com/apk/res/android" - android:id="@+id/kau_pref_inner_content" + android:id="@id/kau_pref_inner_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" diff --git a/core/src/main/res/values/ids.xml b/core/src/main/res/values/ids.xml index dd42392..0b4322c 100644 --- a/core/src/main/res/values/ids.xml +++ b/core/src/main/res/values/ids.xml @@ -4,6 +4,7 @@ <item name="kau_item_pref_header" type="id" /> <item name="kau_item_pref_text" type="id" /> <item name="kau_item_pref_checkbox" type="id" /> + <item name="kau_item_pref_seekbar" type="id" /> <item name="kau_item_pref_color_picker" type="id" /> <item name="kau_item_pref_sub_item" type="id" /> <item name="kau_item_pref_plain_text" type="id" /> @@ -13,4 +14,6 @@ <item name="kau_item_card" type="id" /> <item name="kau_item_library" type="id" /> <item name="kau_item_about_main" type="id" /> + <item name="kau_pref_inner_content" type="id" /> + <item name="kau_pref_lower_content" type="id" /> </resources>
\ No newline at end of file |