From 7a3165ac7404381eb85ea40525db1a7a7d980761 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 26 Dec 2017 04:01:23 -0500 Subject: Update/kpref activity (#113) * Revamp kpref * Clean up data * Fix script * Test emulator * Test google api * Test again * Test gpg * Update dependencies --- .../ca/allanwang/kau/kpref/activity/KClick.kt | 27 ++++++++++ .../allanwang/kau/kpref/activity/KPrefActivity.kt | 4 +- .../kau/kpref/activity/items/KPrefCheckbox.kt | 6 +-- .../kau/kpref/activity/items/KPrefColorPicker.kt | 25 ++++----- .../kau/kpref/activity/items/KPrefHeader.kt | 2 - .../kau/kpref/activity/items/KPrefItemBase.kt | 53 ++++++++++-------- .../kau/kpref/activity/items/KPrefItemCore.kt | 62 +++++++++++++--------- .../kau/kpref/activity/items/KPrefPlainText.kt | 11 ++-- .../kau/kpref/activity/items/KPrefSeekbar.kt | 9 ++-- .../kau/kpref/activity/items/KPrefSubItems.kt | 9 ++-- .../kau/kpref/activity/items/KPrefText.kt | 17 +++--- .../kau/kpref/activity/items/KPrefTimePicker.kt | 32 +++++------ 12 files changed, 148 insertions(+), 109 deletions(-) create mode 100644 kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KClick.kt (limited to 'kpref-activity/src/main/kotlin/ca/allanwang') diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KClick.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KClick.kt new file mode 100644 index 0000000..c02b024 --- /dev/null +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KClick.kt @@ -0,0 +1,27 @@ +package ca.allanwang.kau.kpref.activity + +import android.content.Context +import android.view.View +import ca.allanwang.kau.kpref.activity.items.KPrefItemBase + +/** + * Created by Allan Wang on 10/12/17. + */ +interface KClick { + + val context: Context + /** + * Base view container from ViewHolder + */ + val itemView: View + + /** + * Optional inner view which differs per element + */ + val innerView: View? + + /** + * The item holding the data + */ + val item: KPrefItemBase +} \ No newline at end of file diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefActivity.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefActivity.kt index 2b75328..0f88ea0 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefActivity.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefActivity.kt @@ -70,7 +70,7 @@ abstract class KPrefActivity : KauBaseActivity(), KPrefActivityContract { globalOptions = GlobalOptions(core, this) recycler.withLinearAdapter(adapter) adapter.withSelectable(false) - .withOnClickListener { v, _, item, _ -> item.onClick(v, v.findViewById(R.id.kau_pref_inner_content)) } + .withOnClickListener { v, _, item, _ -> item.onClick(v); true } showNextPrefs(R.string.kau_settings, onCreateKPrefs(savedInstanceState), true) } @@ -141,7 +141,7 @@ abstract class KPrefActivity : KauBaseActivity(), KPrefActivityContract { override fun reloadByTitle(@StringRes vararg title: Int) { if (title.isEmpty()) return adapter.adapterItems.forEachIndexed { index, item -> - if (title.any { item.core.titleRes == it }) + if (title.any { item.core.titleId == it }) adapter.notifyItemChanged(index) } } diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefCheckbox.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefCheckbox.kt index 5738022..88692c4 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefCheckbox.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefCheckbox.kt @@ -3,6 +3,7 @@ package ca.allanwang.kau.kpref.activity.items import android.support.v7.widget.AppCompatCheckBox import android.view.View import android.widget.CheckBox +import ca.allanwang.kau.kpref.activity.KClick import ca.allanwang.kau.kpref.activity.R import ca.allanwang.kau.utils.tint @@ -14,10 +15,9 @@ import ca.allanwang.kau.utils.tint */ open class KPrefCheckbox(builder: BaseContract) : KPrefItemBase(builder) { - override fun defaultOnClick(itemView: View, innerContent: View?): Boolean { + override fun KClick.defaultOnClick() { pref = !pref - (innerContent as AppCompatCheckBox).isChecked = pref - return true + (innerView as AppCompatCheckBox).isChecked = pref } override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefColorPicker.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefColorPicker.kt index 1950589..e0bea4f 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefColorPicker.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefColorPicker.kt @@ -1,12 +1,13 @@ package ca.allanwang.kau.kpref.activity.items -import android.view.View import ca.allanwang.kau.colorpicker.CircleView import ca.allanwang.kau.colorpicker.ColorBuilder import ca.allanwang.kau.colorpicker.ColorContract import ca.allanwang.kau.colorpicker.colorPickerDialog import ca.allanwang.kau.kpref.activity.GlobalOptions +import ca.allanwang.kau.kpref.activity.KClick import ca.allanwang.kau.kpref.activity.R +import ca.allanwang.kau.utils.string /** * Created by Allan Wang on 2017-06-07. @@ -19,10 +20,8 @@ open class KPrefColorPicker(open val builder: KPrefColorContract) : KPrefItemBas override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { super.onPostBindView(viewHolder, textColor, accentColor) builder.apply { - titleRes = core.titleRes - colorCallback = { - pref = it - } + titleRes = core.titleFun() + colorCallback = { pref = it } } if (builder.showPreview) { val preview = viewHolder.bindInnerView(R.layout.kau_pref_color) @@ -33,18 +32,16 @@ open class KPrefColorPicker(open val builder: KPrefColorContract) : KPrefItemBas pref = it if (builder.showPreview) preview.setBackgroundColor(it) + viewHolder.updateTitle() + viewHolder.updateDesc() } } } } - - override fun defaultOnClick(itemView: View, innerContent: View?): Boolean { - builder.apply { - defaultColor = pref //update color - } - itemView.context.colorPickerDialog(builder).show() - return true + override fun KClick.defaultOnClick() { + builder.defaultColor = pref + context.colorPickerDialog(builder).show() } /** @@ -58,10 +55,10 @@ open class KPrefColorPicker(open val builder: KPrefColorContract) : KPrefItemBas * Default implementation of [KPrefColorContract] */ class KPrefColorBuilder(globalOptions: GlobalOptions, - override var titleRes: Int, + titleId: Int, getter: () -> Int, setter: (value: Int) -> Unit - ) : KPrefColorContract, BaseContract by BaseBuilder(globalOptions, titleRes, getter, setter), + ) : KPrefColorContract, BaseContract by BaseBuilder(globalOptions, titleId, getter, setter), ColorContract by ColorBuilder() { override var showPreview: Boolean = true } diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefHeader.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefHeader.kt index 6f565ae..3135bb3 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefHeader.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefHeader.kt @@ -17,8 +17,6 @@ open class KPrefHeader(builder: CoreContract) : KPrefItemCore(builder) { if (accentColor != null) viewHolder.title.setTextColor(accentColor) } - override fun onClick(itemView: View, innerContent: View?): Boolean = true - override fun getType() = R.id.kau_item_pref_header } \ No newline at end of file diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemBase.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemBase.kt index c3f0290..344df5c 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemBase.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemBase.kt @@ -3,6 +3,7 @@ package ca.allanwang.kau.kpref.activity.items import android.support.annotation.CallSuper import android.view.View import ca.allanwang.kau.kpref.activity.GlobalOptions +import ca.allanwang.kau.kpref.activity.KClick import ca.allanwang.kau.kpref.activity.R import ca.allanwang.kau.utils.resolveDrawable @@ -14,42 +15,50 @@ import ca.allanwang.kau.utils.resolveDrawable abstract class KPrefItemBase(val base: BaseContract) : KPrefItemCore(base) { open var pref: T - get() = base.getter.invoke() + get() = base.getter() set(value) { - base.setter.invoke(value) + base.setter(value) } var enabled: Boolean = true init { - if (base.onClick == null) base.onClick = { - itemView, innerContent, _ -> - defaultOnClick(itemView, innerContent) - } + if (base.onClick == null) base.onClick = { defaultOnClick() } } - abstract fun defaultOnClick(itemView: View, innerContent: View?): Boolean - @CallSuper override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { - enabled = base.enabler.invoke() + enabled = base.enabler() with(viewHolder) { if (!enabled) container?.background = null container?.alpha = if (enabled) 1.0f else 0.3f } } - override final fun onClick(itemView: View, innerContent: View?): Boolean { - return if (enabled) base.onClick?.invoke(itemView, innerContent, this) ?: false - else base.onDisabledClick?.invoke(itemView, innerContent, this) ?: false + override final fun onClick(itemView: View) { + val kclick = object : KClick { + override val context = itemView.context + override val itemView = itemView + override val innerView: View? by lazy { itemView.findViewById(R.id.kau_pref_inner_content) } + override val item = this@KPrefItemBase + } + if (enabled) { + val onClick = base.onClick ?: return + kclick.onClick() + } else { + val onClick = base.onDisabledClick ?: return + kclick.onClick() + } } + abstract fun KClick.defaultOnClick() + override fun unbindView(holder: ViewHolder) { super.unbindView(holder) - with(holder) { - container?.isEnabled = true - container?.background = itemView.context.resolveDrawable(android.R.attr.selectableItemBackground) - container?.alpha = 1.0f + holder.container?.apply { + isEnabled = true + background = holder.itemView.context.resolveDrawable(android.R.attr.selectableItemBackground) + alpha = 1.0f } } @@ -62,8 +71,8 @@ abstract class KPrefItemBase(val base: BaseContract) : KPrefItemCore(base) */ interface BaseContract : CoreContract { var enabler: () -> Boolean - var onClick: ((itemView: View, innerContent: View?, item: KPrefItemBase) -> Boolean)? - var onDisabledClick: ((itemView: View, innerContent: View?, item: KPrefItemBase) -> Boolean)? + var onClick: (KClick.() -> Unit)? + var onDisabledClick: (KClick.() -> Unit)? val getter: () -> T val setter: (value: T) -> Unit } @@ -72,13 +81,13 @@ abstract class KPrefItemBase(val base: BaseContract) : KPrefItemCore(base) * Default implementation of [BaseContract] */ class BaseBuilder(globalOptions: GlobalOptions, - titleRes: Int, + titleId: Int, override val getter: () -> T, override val setter: (value: T) -> Unit - ) : CoreContract by CoreBuilder(globalOptions, titleRes), BaseContract { + ) : CoreContract by CoreBuilder(globalOptions, titleId), BaseContract { override var enabler: () -> Boolean = { true } - override var onClick: ((itemView: View, innerContent: View?, item: KPrefItemBase) -> Boolean)? = null - override var onDisabledClick: ((itemView: View, innerContent: View?, item: KPrefItemBase) -> Boolean)? = null + override var onClick: (KClick.() -> Unit)? = null + override var onDisabledClick: (KClick.() -> Unit)? = null } } \ No newline at end of file diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemCore.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemCore.kt index dc74c97..734824b 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemCore.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemCore.kt @@ -4,7 +4,6 @@ import android.annotation.SuppressLint import android.support.annotation.CallSuper import android.support.annotation.IdRes import android.support.annotation.LayoutRes -import android.support.annotation.StringRes import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View @@ -32,17 +31,25 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem 0) + desc?.visible()?.setText(descRes) + else + desc?.gone() + } + + protected fun ViewHolder.updateTitle() { + title.setText(core.titleFun()) + } + @SuppressLint("NewApi") @CallSuper override fun bindView(viewHolder: ViewHolder, payloads: List) { super.bindView(viewHolder, payloads) with(viewHolder) { - val context = itemView.context - title.text = context.string(core.titleRes) - if (core.descRes > 0) - desc?.visible()?.setText(core.descRes) - else - desc?.gone() + updateTitle() + updateDesc() if (core.iicon != null) icon?.visible()?.setIcon(core.iicon, 24) else icon?.gone() innerFrame?.removeAllViews() @@ -61,7 +68,7 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem Int var descRes: Int - @StringRes get + var descFun: () -> Int var iicon: IIcon? var visible: () -> Boolean /** - * Attempts to reload current item by identifying it with its [titleRes] + * Attempts to reload current item by identifying it with its [titleId] */ fun reloadSelf() } @@ -96,13 +104,19 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem Boolean = { true } + override var visible = { true } + override var titleFun = { titleId } override fun reloadSelf() { - globalOptions.reloadByTitle(titleRes) + globalOptions.reloadByTitle(titleId) } } @@ -113,7 +127,7 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem bindInnerView(@LayoutRes id: Int) = bindInnerView(id) { _: 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) + val innerFrame = this.innerFrame ?: throw IllegalStateException("Cannot bind inner view when innerFrame does not exist") + if (innerView !is T) { + innerFrame.removeAllViews() + LayoutInflater.from(innerFrame.context).inflate(id, innerFrame) + onFirstBind(innerView as T) } - return innerContent as T + return innerView 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") + val lowerFrame = this.lowerFrame ?: throw IllegalStateException("Cannot bind inner view when lowerContent does not exist") if (lowerContent !is T) { - lowerFrame!!.removeAllViews() - LayoutInflater.from(lowerFrame!!.context).inflate(id, lowerFrame) + lowerFrame.removeAllViews() + LayoutInflater.from(lowerFrame.context).inflate(id, lowerFrame) onFirstBind(lowerContent as T) } return lowerContent as T diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefPlainText.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefPlainText.kt index d2e49d8..40bf284 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefPlainText.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefPlainText.kt @@ -1,7 +1,7 @@ package ca.allanwang.kau.kpref.activity.items -import android.view.View import ca.allanwang.kau.kpref.activity.GlobalOptions +import ca.allanwang.kau.kpref.activity.KClick import ca.allanwang.kau.kpref.activity.R /** @@ -14,15 +14,12 @@ import ca.allanwang.kau.kpref.activity.R */ open class KPrefPlainText(open val builder: KPrefPlainTextBuilder) : KPrefItemBase(builder) { - override fun defaultOnClick(itemView: View, innerContent: View?): Boolean { - //nothing - return true - } + override fun KClick.defaultOnClick() = Unit class KPrefPlainTextBuilder( globalOptions: GlobalOptions, - titleRes: Int - ) : BaseContract by BaseBuilder(globalOptions, titleRes, {}, {}) + titleId: Int + ) : BaseContract by BaseBuilder(globalOptions, titleId, {}, {}) override fun getType(): Int = R.id.kau_item_pref_plain_text 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 3afa614..b27b2ae 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 @@ -1,9 +1,9 @@ package ca.allanwang.kau.kpref.activity.items -import android.view.View import android.widget.SeekBar import android.widget.TextView import ca.allanwang.kau.kpref.activity.GlobalOptions +import ca.allanwang.kau.kpref.activity.KClick import ca.allanwang.kau.kpref.activity.R import ca.allanwang.kau.utils.tint @@ -15,8 +15,7 @@ import ca.allanwang.kau.utils.tint */ open class KPrefSeekbar(val builder: KPrefSeekbarContract) : KPrefItemBase(builder) { - - override fun defaultOnClick(itemView: View, innerContent: View?): Boolean = false + override fun KClick.defaultOnClick() = Unit override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { super.onPostBindView(viewHolder, textColor, accentColor) @@ -66,10 +65,10 @@ open class KPrefSeekbar(val builder: KPrefSeekbarContract) : KPrefItemBase( */ class KPrefSeekbarBuilder( globalOptions: GlobalOptions, - titleRes: Int, + titleId: Int, getter: () -> Int, setter: (value: Int) -> Unit - ) : KPrefSeekbarContract, BaseContract by BaseBuilder(globalOptions, titleRes, getter, setter) { + ) : KPrefSeekbarContract, BaseContract by BaseBuilder(globalOptions, titleId, getter, setter) { override var min: Int = 0 diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSubItems.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSubItems.kt index 7c2979c..5d5db96 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSubItems.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSubItems.kt @@ -14,9 +14,8 @@ import ca.allanwang.kau.kpref.activity.R */ open class KPrefSubItems(open val builder: KPrefSubItemsContract) : KPrefItemCore(builder) { - override fun onClick(itemView: View, innerContent: View?): Boolean { - builder.globalOptions.showNextPrefs(builder.titleRes, builder.itemBuilder) - return true + override fun onClick(itemView: View) { + builder.globalOptions.showNextPrefs(builder.titleFun(), builder.itemBuilder) } override fun getLayoutRes(): Int = R.layout.kau_pref_core @@ -34,9 +33,9 @@ open class KPrefSubItems(open val builder: KPrefSubItemsContract) : KPrefItemCor */ class KPrefSubItemsBuilder( globalOptions: GlobalOptions, - titleRes: Int, + titleId: Int, override val itemBuilder: KPrefAdapterBuilder.() -> Unit - ) : KPrefSubItemsContract, CoreContract by CoreBuilder(globalOptions, titleRes) + ) : KPrefSubItemsContract, CoreContract by CoreBuilder(globalOptions, titleId) override fun getType(): Int = R.id.kau_item_pref_sub_item diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefText.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefText.kt index 29dd007..fa45b04 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefText.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefText.kt @@ -1,8 +1,8 @@ package ca.allanwang.kau.kpref.activity.items -import android.view.View import android.widget.TextView import ca.allanwang.kau.kpref.activity.GlobalOptions +import ca.allanwang.kau.kpref.activity.KClick import ca.allanwang.kau.kpref.activity.R import ca.allanwang.kau.utils.toast @@ -20,22 +20,21 @@ open class KPrefText(open val builder: KPrefTextContract) : KPrefItemBase< * Automatically reload on set */ override var pref: T - get() = base.getter.invoke() + get() = base.getter() set(value) { - base.setter.invoke(value) + base.setter(value) builder.reloadSelf() } - override fun defaultOnClick(itemView: View, innerContent: View?): Boolean { - itemView.context.toast("No click function set") - return true + override fun KClick.defaultOnClick() { + context.toast("No click function set") } override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { super.onPostBindView(viewHolder, textColor, accentColor) val textview = viewHolder.bindInnerView(R.layout.kau_pref_text) if (textColor != null) textview.setTextColor(textColor) - textview.text = builder.textGetter.invoke(pref) + textview.text = builder.textGetter(pref) } /** @@ -50,10 +49,10 @@ open class KPrefText(open val builder: KPrefTextContract) : KPrefItemBase< */ class KPrefTextBuilder( globalOptions: GlobalOptions, - titleRes: Int, + titleId: Int, getter: () -> T, setter: (value: T) -> Unit - ) : KPrefTextContract, BaseContract by BaseBuilder(globalOptions, titleRes, getter, setter) { + ) : KPrefTextContract, BaseContract by BaseBuilder(globalOptions, titleId, getter, setter) { override var textGetter: (T) -> String? = { it?.toString() } } diff --git a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefTimePicker.kt b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefTimePicker.kt index d4f854b..f6fc40a 100644 --- a/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefTimePicker.kt +++ b/kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefTimePicker.kt @@ -1,9 +1,9 @@ package ca.allanwang.kau.kpref.activity.items import android.app.TimePickerDialog -import android.view.View import android.widget.TimePicker import ca.allanwang.kau.kpref.activity.GlobalOptions +import ca.allanwang.kau.kpref.activity.KClick import ca.allanwang.kau.kpref.activity.R import java.util.* @@ -17,19 +17,24 @@ import java.util.* */ open class KPrefTimePicker(override val builder: KPrefTimeContract) : KPrefText(builder) { - interface KPrefTimeContract : KPrefText.KPrefTextContract { + interface KPrefTimeContract : KPrefText.KPrefTextContract, TimePickerDialog.OnTimeSetListener { var use24HourFormat: Boolean } + override fun KClick.defaultOnClick() { + val (hour, min) = pref.splitTime + TimePickerDialog(itemView.context, builder, hour, min, builder.use24HourFormat).show() + } + /** * Default implementation of [KPrefTimeContract] */ class KPrefTimeBuilder( globalOptions: GlobalOptions, - titleRes: Int, + titleId: Int, getter: () -> Int, setter: (value: Int) -> Unit - ) : KPrefTimeContract, BaseContract by BaseBuilder(globalOptions, titleRes, getter, setter), TimePickerDialog.OnTimeSetListener { + ) : KPrefTimeContract, BaseContract by BaseBuilder(globalOptions, titleId, getter, setter) { override var use24HourFormat: Boolean = false @@ -46,19 +51,14 @@ open class KPrefTimePicker(override val builder: KPrefTimeContract) : KPrefText< String.format(Locale.CANADA, "%d:%02d %s", hour % 12, min, if (hour >= 12) "PM" else "AM") } - override var onClick: ((itemView: View, innerContent: View?, item: KPrefItemBase) -> Boolean)? = { itemView, _, item -> - val (hour, min) = item.pref.splitTime - TimePickerDialog(itemView.context, this, hour, min, use24HourFormat).show() - true - } - - private val Int.splitTime: Pair - get() = Pair(this / 100, this % 100) - - private val Pair.mergeTime: Int - get() = first * 100 + second } override fun getType(): Int = R.id.kau_item_pref_time_picker -} \ No newline at end of file +} + +private val Int.splitTime: Pair + get() = Pair(this / 100, this % 100) + +private val Pair.mergeTime: Int + get() = first * 100 + second \ No newline at end of file -- cgit v1.2.3