diff options
author | Allan Wang <me@allanwang.ca> | 2017-12-26 04:01:23 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-26 04:01:23 -0500 |
commit | 7a3165ac7404381eb85ea40525db1a7a7d980761 (patch) | |
tree | 2341ea4ef0488b9abeea1fbe70697705d295aeb2 /kpref-activity/src/main | |
parent | fb9ca21757068c0fb4123a5e30b1471ae4c32cf3 (diff) | |
download | kau-7a3165ac7404381eb85ea40525db1a7a7d980761.tar.gz kau-7a3165ac7404381eb85ea40525db1a7a7d980761.tar.bz2 kau-7a3165ac7404381eb85ea40525db1a7a7d980761.zip |
Update/kpref activity (#113)
* Revamp kpref
* Clean up data
* Fix script
* Test emulator
* Test google api
* Test again
* Test gpg
* Update dependencies
Diffstat (limited to 'kpref-activity/src/main')
12 files changed, 148 insertions, 109 deletions
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<T> { + + 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<T> +}
\ 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<Boolean>) : KPrefItemBase<Boolean>(builder) { - override fun defaultOnClick(itemView: View, innerContent: View?): Boolean { + override fun KClick<Boolean>.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<CircleView>(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<Int>.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<Int> by BaseBuilder<Int>(globalOptions, titleRes, getter, setter), + ) : KPrefColorContract, BaseContract<Int> 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<T>(val base: BaseContract<T>) : 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<T> { + override val context = itemView.context + override val itemView = itemView + override val innerView: View? by lazy { itemView.findViewById<View>(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<T>.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<T>(val base: BaseContract<T>) : KPrefItemCore(base) */ interface BaseContract<T> : CoreContract { var enabler: () -> Boolean - var onClick: ((itemView: View, innerContent: View?, item: KPrefItemBase<T>) -> Boolean)? - var onDisabledClick: ((itemView: View, innerContent: View?, item: KPrefItemBase<T>) -> Boolean)? + var onClick: (KClick<T>.() -> Unit)? + var onDisabledClick: (KClick<T>.() -> Unit)? val getter: () -> T val setter: (value: T) -> Unit } @@ -72,13 +81,13 @@ abstract class KPrefItemBase<T>(val base: BaseContract<T>) : KPrefItemCore(base) * Default implementation of [BaseContract] */ class BaseBuilder<T>(globalOptions: GlobalOptions, - titleRes: Int, + titleId: Int, override val getter: () -> T, override val setter: (value: T) -> Unit - ) : CoreContract by CoreBuilder(globalOptions, titleRes), BaseContract<T> { + ) : CoreContract by CoreBuilder(globalOptions, titleId), BaseContract<T> { override var enabler: () -> Boolean = { true } - override var onClick: ((itemView: View, innerContent: View?, item: KPrefItemBase<T>) -> Boolean)? = null - override var onDisabledClick: ((itemView: View, innerContent: View?, item: KPrefItemBase<T>) -> Boolean)? = null + override var onClick: (KClick<T>.() -> Unit)? = null + override var onDisabledClick: (KClick<T>.() -> 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<KPrefItemCor override final fun getViewHolder(v: View) = ViewHolder(v) + protected fun ViewHolder.updateDesc() { + val descRes = core.descFun() + if (descRes > 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<Any>) { 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<KPrefItemCor abstract fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) - abstract fun onClick(itemView: View, innerContent: View?): Boolean + open fun onClick(itemView: View) = Unit override fun unbindView(holder: ViewHolder) { super.unbindView(holder) @@ -80,14 +87,15 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem<KPrefItemCor @KPrefMarker interface CoreContract { val globalOptions: GlobalOptions - @get:StringRes val titleRes: Int + val titleId: Int + var titleFun: () -> 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<KPrefItemCor * Default implementation of [CoreContract] */ class CoreBuilder(override val globalOptions: GlobalOptions, - override @param:StringRes val titleRes: Int) : CoreContract { + override val titleId: Int) : CoreContract { override var descRes: Int = -1 + set(value) { + field = value + descFun = { field } + } + override var descFun = { -1 } override var iicon: IIcon? = null - override var visible: () -> 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<KPrefItemCor 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? + val innerView: View? get() = itemView.findViewById(R.id.kau_pref_inner_content) val lowerContent: View? get() = itemView.findViewById(R.id.kau_pref_lower_content) @@ -121,22 +135,22 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem<KPrefItemCor inline fun <reified T : View> bindInnerView(@LayoutRes id: Int) = bindInnerView(id) { _: 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) + 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 <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") + 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<Unit>(builder) { - override fun defaultOnClick(itemView: View, innerContent: View?): Boolean { - //nothing - return true - } + override fun KClick<Unit>.defaultOnClick() = Unit class KPrefPlainTextBuilder( globalOptions: GlobalOptions, - titleRes: Int - ) : BaseContract<Unit> by BaseBuilder(globalOptions, titleRes, {}, {}) + titleId: Int + ) : BaseContract<Unit> 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<Int>(builder) { - - override fun defaultOnClick(itemView: View, innerContent: View?): Boolean = false + override fun KClick<Int>.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<Int>( */ class KPrefSeekbarBuilder( globalOptions: GlobalOptions, - titleRes: Int, + titleId: Int, getter: () -> Int, setter: (value: Int) -> Unit - ) : KPrefSeekbarContract, BaseContract<Int> by BaseBuilder(globalOptions, titleRes, getter, setter) { + ) : KPrefSeekbarContract, BaseContract<Int> 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<T>(open val builder: KPrefTextContract<T>) : 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<T>.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<TextView>(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<T>(open val builder: KPrefTextContract<T>) : KPrefItemBase< */ class KPrefTextBuilder<T>( globalOptions: GlobalOptions, - titleRes: Int, + titleId: Int, getter: () -> T, setter: (value: T) -> Unit - ) : KPrefTextContract<T>, BaseContract<T> by BaseBuilder<T>(globalOptions, titleRes, getter, setter) { + ) : KPrefTextContract<T>, BaseContract<T> by BaseBuilder<T>(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<Int>(builder) { - interface KPrefTimeContract : KPrefText.KPrefTextContract<Int> { + interface KPrefTimeContract : KPrefText.KPrefTextContract<Int>, TimePickerDialog.OnTimeSetListener { var use24HourFormat: Boolean } + override fun KClick<Int>.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<Int> by BaseBuilder<Int>(globalOptions, titleRes, getter, setter), TimePickerDialog.OnTimeSetListener { + ) : KPrefTimeContract, BaseContract<Int> by BaseBuilder<Int>(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<Int>) -> Boolean)? = { itemView, _, item -> - val (hour, min) = item.pref.splitTime - TimePickerDialog(itemView.context, this, hour, min, use24HourFormat).show() - true - } - - private val Int.splitTime: Pair<Int, Int> - get() = Pair(this / 100, this % 100) - - private val Pair<Int, Int>.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<Int, Int> + get() = Pair(this / 100, this % 100) + +private val Pair<Int, Int>.mergeTime: Int + get() = first * 100 + second
\ No newline at end of file |