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 +- core/src/main/res/layout/kau_preference.xml | 25 +++-- .../main/res/layout/kau_preference_checkbox.xml | 2 +- .../res/layout/kau_preference_color_preview.xml | 2 +- .../src/main/res/layout/kau_preference_seekbar.xml | 8 ++ .../res/layout/kau_preference_seekbar_text.xml | 12 +++ core/src/main/res/layout/kau_preference_text.xml | 2 +- core/src/main/res/values/ids.xml | 3 + docs/Changelog.md | 44 +++++---- .../kotlin/ca/allanwang/kau/sample/KPrefSample.kt | 1 + .../kotlin/ca/allanwang/kau/sample/MainActivity.kt | 9 +- sample/src/main/res/values/strings.xml | 1 + sample/src/main/res/xml/changelog.xml | 9 +- 21 files changed, 230 insertions(+), 41 deletions(-) create mode 100644 core/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefSeekbar.kt create mode 100644 core/src/main/res/layout/kau_preference_seekbar.xml create mode 100644 core/src/main/res/layout/kau_preference_seekbar_text.xml 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 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" /> + + @@ -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 @@ \ 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 @@ + + + \ 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 @@ + @@ -13,4 +14,6 @@ + + \ No newline at end of file diff --git a/docs/Changelog.md b/docs/Changelog.md index 9161280..7e99a63 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -1,26 +1,30 @@ # Changelog +## v1.6 + ## v1.5 * Change snackbar builder * Change addBundle to withArguments to match ANKO -* Created KauIItem to replace AbstractItem -* Created permission manager and permission constants +* Create KauIItem to replace AbstractItem +* Create permission manager and permission constants +* Create swipe, a very simple helper to allow for activities to be dismissed with gestures +* Create network utils ## v1.4 -* Added about activities -* Added themed fast item imageAdapter -* Added chained imageAdapter -* Added item animators -* Ported some views over from Plaid +* Add about activities +* Add themed fast item imageAdapter +* Add chained imageAdapter +* Add item animators +* Port some views over from Plaid * Add string arg option for sendEmail * Add many iitems ## v1.3 -* Added kpref subitems -* Added DSL markers -* Added transition utils and other utils -* Added custom searchview with binders -* Added KauBoundedCardView +* Add kpref subitems +* Add DSL markers +* Add transition utils and other utils +* Add custom searchview with binders +* Add KauBoundedCardView ## v1.2 * Fix title attribute in changelog @@ -29,14 +33,14 @@ * Add email builder ## v1.1 -* Created kpref items -* Attached source files -* Created color dialog -* Added more utilities -* Fixed indexStack clearing when starting activity +* Create kpref items +* Attach source files +* Create color dialog +* Add more utilities +* Fix indexStack clearing when starting activity ## v1.0 * Initial Changelog -* Created many extension functions -* Ported changelog builer -* Ported ripple canvas +* Create many extension functions +* Port changelog builer +* Port ripple canvas diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt index a0fbc59..1bbcc47 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt @@ -15,4 +15,5 @@ object KPrefSample : KPref() { var check2: Boolean by kpref("check2", false) var check3: Boolean by kpref("check3", false) var text: String by kpref("text", "empty") + var seekbar: Int by kpref("seekbar", 20) } \ No newline at end of file diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt index 899a50e..e63f107 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt @@ -143,6 +143,13 @@ class MainActivity : KPrefActivity() { } } + seekbar(R.string.seekbar, { KPrefSample.seekbar }, { KPrefSample.seekbar = it }) { + descRes = R.string.kau_lorem_ipsum + textViewConfigs = { + minEms = 2 + } + } + subItems(R.string.sub_item, subPrefs()) { descRes = R.string.sub_item_desc } @@ -158,7 +165,7 @@ class MainActivity : KPrefActivity() { } fun subPrefs(): KPrefAdapterBuilder.() -> Unit = { - text(R.string.text, { KPrefSample.text }, { KPrefSample.text = it }) { + text(R.string.text, { KPrefSample.text }, { KPrefSample.text = it }) { descRes = R.string.text_desc onClick = { itemView, _, item -> diff --git a/sample/src/main/res/values/strings.xml b/sample/src/main/res/values/strings.xml index a2f1b58..0be46ec 100644 --- a/sample/src/main/res/values/strings.xml +++ b/sample/src/main/res/values/strings.xml @@ -15,6 +15,7 @@ This selector allows for custom colors with alpha values Text Pref Saves the text + Seekbar Sub Item Pref Press this to view the next subset of preferences your.email@here.com diff --git a/sample/src/main/res/xml/changelog.xml b/sample/src/main/res/xml/changelog.xml index 7801511..7d79ad5 100644 --- a/sample/src/main/res/xml/changelog.xml +++ b/sample/src/main/res/xml/changelog.xml @@ -6,6 +6,13 @@ --> + + + + + + + @@ -13,8 +20,6 @@ - - -- cgit v1.2.3