From 28c07ba66bf71dd322ffe3253b35862cdfc82e98 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 20 Jan 2018 21:59:35 -0500 Subject: Update kpref-activity encapsulation and internals --- .../allanwang/kau/kpref/activity/KPrefActivity.kt | 4 ++ .../kau/kpref/activity/items/KPrefCheckbox.kt | 9 +++-- .../kau/kpref/activity/items/KPrefColorPicker.kt | 10 ++--- .../kau/kpref/activity/items/KPrefHeader.kt | 5 ++- .../kau/kpref/activity/items/KPrefItemBase.kt | 24 ++++++----- .../kau/kpref/activity/items/KPrefItemCore.kt | 47 ++++++++++++++-------- .../kau/kpref/activity/items/KPrefSeekbar.kt | 12 +++--- .../kau/kpref/activity/items/KPrefSubItems.kt | 1 - .../kau/kpref/activity/items/KPrefText.kt | 10 ++--- 9 files changed, 72 insertions(+), 50 deletions(-) (limited to 'kpref-activity/src/main/kotlin/ca/allanwang') 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 0f88ea0..75e9c36 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 @@ -152,6 +152,10 @@ abstract class KPrefActivity : KauBaseActivity(), KPrefActivityContract { if (!backPress()) super.onBackPressed() } + /** + * Back press handler with status output + * Returns [true] if the press has been consumed, [false] otherwise + */ fun backPress(): Boolean { if (hasPrevPrefs) { showPrevPrefs() 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 708c133..3e06f6a 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 @@ -4,6 +4,7 @@ import android.support.v7.widget.AppCompatCheckBox import android.widget.CheckBox import ca.allanwang.kau.kpref.activity.KClick import ca.allanwang.kau.kpref.activity.R +import ca.allanwang.kau.logging.KL import ca.allanwang.kau.utils.tint /** @@ -19,10 +20,10 @@ open class KPrefCheckbox(builder: BaseContract) : KPrefItemBase(R.layout.kau_pref_checkbox) - if (accentColor != null) checkbox.tint(accentColor) + override fun bindView(holder: ViewHolder, payloads: List) { + super.bindView(holder, payloads) + val checkbox = holder.bindInnerView(R.layout.kau_pref_checkbox) + withAccentColor(checkbox::tint) checkbox.isChecked = pref checkbox.jumpDrawablesToCurrentState() //Cancel the animation } 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 bbf4d5c..3201fbd 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 @@ -16,14 +16,14 @@ import ca.allanwang.kau.kpref.activity.R */ open class KPrefColorPicker(open val builder: KPrefColorContract) : KPrefItemBase(builder) { - override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { - super.onPostBindView(viewHolder, textColor, accentColor) + override fun bindView(holder: ViewHolder, payloads: List) { + super.bindView(holder, payloads) builder.apply { titleRes = core.titleFun() colorCallback = { pref = it } } if (builder.showPreview) { - val preview = viewHolder.bindInnerView(R.layout.kau_pref_color) + val preview = holder.bindInnerView(R.layout.kau_pref_color) preview.setBackgroundColor(pref) preview.withBorder = true builder.apply { @@ -31,8 +31,8 @@ open class KPrefColorPicker(open val builder: KPrefColorContract) : KPrefItemBas pref = it if (builder.showPreview) preview.setBackgroundColor(it) - viewHolder.updateTitle() - viewHolder.updateDesc() + holder.updateTitle() + holder.updateDesc() } } } 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 10da26d..9b63a52 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 @@ -12,8 +12,9 @@ open class KPrefHeader(builder: CoreContract) : KPrefItemCore(builder) { override fun getLayoutRes(): Int = R.layout.kau_pref_header - override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { - if (accentColor != null) viewHolder.title.setTextColor(accentColor) + override fun bindView(holder: ViewHolder, payloads: List) { + super.bindView(holder, payloads) + withAccentColor(holder.title::setTextColor) } override fun getType() = R.id.kau_item_pref_header 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 344df5c..5081f50 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 @@ -12,7 +12,7 @@ import ca.allanwang.kau.utils.resolveDrawable * * Base class for pref setters that include the Shared Preference hooks */ -abstract class KPrefItemBase(val base: BaseContract) : KPrefItemCore(base) { +abstract class KPrefItemBase(protected val base: BaseContract) : KPrefItemCore(base) { open var pref: T get() = base.getter() @@ -20,29 +20,33 @@ abstract class KPrefItemBase(val base: BaseContract) : KPrefItemCore(base) base.setter(value) } - var enabled: Boolean = true + private var _enabled: Boolean = true + + val enabled + get() = _enabled init { if (base.onClick == null) base.onClick = { defaultOnClick() } } @CallSuper - override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { - enabled = base.enabler() - with(viewHolder) { - if (!enabled) container?.background = null - container?.alpha = if (enabled) 1.0f else 0.3f + override fun bindView(holder: ViewHolder, payloads: List) { + super.bindView(holder, payloads) + _enabled = base.enabler() + with(holder) { + if (!_enabled) container?.background = null + container?.alpha = if (_enabled) 1.0f else 0.3f } } - override final fun onClick(itemView: View) { + final override 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) { + if (_enabled) { val onClick = base.onClick ?: return kclick.onClick() } else { @@ -62,7 +66,7 @@ abstract class KPrefItemBase(val base: BaseContract) : KPrefItemCore(base) } } - override final fun getLayoutRes(): Int = R.layout.kau_pref_core + final override fun getLayoutRes(): Int = R.layout.kau_pref_core /** * Extension of the core contract 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 734824b..2afdd2e 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 @@ -29,7 +29,7 @@ import com.mikepenz.iconics.typeface.IIcon abstract class KPrefItemCore(val core: CoreContract) : AbstractItem(), ThemableIItem by ThemableIItemDelegate() { - override final fun getViewHolder(v: View) = ViewHolder(v) + final override fun getViewHolder(v: View) = ViewHolder(v) protected fun ViewHolder.updateDesc() { val descRes = core.descFun() @@ -43,30 +43,41 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem) { - super.bindView(viewHolder, payloads) - with(viewHolder) { + override fun bindView(holder: ViewHolder, payloads: List) { + super.bindView(holder, payloads) + with(holder) { updateTitle() updateDesc() if (core.iicon != null) icon?.visible()?.setIcon(core.iicon, 24) else icon?.gone() innerFrame?.removeAllViews() - val textColor = core.globalOptions.textColor?.invoke() - if (textColor != null) { - title.setTextColor(textColor) - desc?.setTextColor(textColor.adjustAlpha(0.65f)) - } - val accentColor = core.globalOptions.accentColor?.invoke() - if (accentColor != null && buildIsLollipopAndUp) { - icon?.drawable?.setTint(accentColor) + withTextColor { + title.setTextColor(it) + desc?.setTextColor(it.adjustAlpha(0.65f)) } - onPostBindView(this, textColor, accentColor) + if (buildIsLollipopAndUp) + withAccentColor { + icon?.drawable?.setTint(it) + } } } - abstract fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) + protected inline fun withAccentColor(action: (color: Int) -> Unit) = + withColor(core.globalOptions.accentColor, action) + + protected inline fun withTextColor(action: (color: Int) -> Unit) = + withColor(core.globalOptions.textColor, action) + + protected inline fun withColor(noinline supplier: (() -> Int)?, + action: (color: Int) -> Unit) { + val color = supplier?.invoke() ?: return + action(color) + } open fun onClick(itemView: View) = Unit @@ -95,7 +106,7 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem Boolean /** - * Attempts to reload current item by identifying it with its [titleId] + * Attempts to reload current item by identifying it with its [id] */ fun reloadSelf() } @@ -135,7 +146,8 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem bindInnerView(@LayoutRes id: Int) = bindInnerView(id) { _: T -> } inline fun bindInnerView(@LayoutRes id: Int, onFirstBind: (T) -> Unit): T { - val innerFrame = this.innerFrame ?: throw IllegalStateException("Cannot bind inner view when innerFrame does not exist") + 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) @@ -147,7 +159,8 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem bindLowerView(@LayoutRes id: Int) = bindLowerView(id) { _: T -> } inline fun bindLowerView(@LayoutRes id: Int, onFirstBind: (T) -> Unit): T { - val lowerFrame = this.lowerFrame ?: 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) 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 b27b2ae..83bbe87 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 @@ -17,15 +17,15 @@ open class KPrefSeekbar(val builder: KPrefSeekbarContract) : KPrefItemBase( override fun KClick.defaultOnClick() = Unit - override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { - super.onPostBindView(viewHolder, textColor, accentColor) - val text = viewHolder.bindInnerView(R.layout.kau_pref_seekbar_text) - if (textColor != null) text.setTextColor(textColor) + override fun bindView(holder: ViewHolder, payloads: List) { + super.bindView(holder, payloads) + val text = holder.bindInnerView(R.layout.kau_pref_seekbar_text) + withTextColor(text::setTextColor) val tvc = builder.textViewConfigs text.tvc() - val seekbar = viewHolder.bindLowerView(R.layout.kau_pref_seekbar) { + val seekbar = holder.bindLowerView(R.layout.kau_pref_seekbar) { it.max = builder.max - builder.min it.incrementProgressBy(builder.increments) it.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { @@ -41,7 +41,7 @@ open class KPrefSeekbar(val builder: KPrefSeekbarContract) : KPrefItemBase( } }) } - if (accentColor != null) seekbar.tint(accentColor) + withAccentColor(seekbar::tint) text.text = builder.toText(seekbar.progress.fromProgress) //set initial text in case no change occurs seekbar.progress = pref.toProgress } 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 5d5db96..6753142 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 @@ -20,7 +20,6 @@ open class KPrefSubItems(open val builder: KPrefSubItemsContract) : KPrefItemCor override fun getLayoutRes(): Int = R.layout.kau_pref_core - override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) {} /** * Extension of the base contract with an optional text getter */ 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 fa45b04..2ab911b 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 @@ -30,11 +30,11 @@ open class KPrefText(open val builder: KPrefTextContract) : KPrefItemBase< 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(pref) + override fun bindView(holder: ViewHolder, payloads: List) { + super.bindView(holder, payloads) + val textView = holder.bindInnerView(R.layout.kau_pref_text) + withTextColor(textView::setTextColor) + textView.text = builder.textGetter(pref) } /** -- cgit v1.2.3