aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/KPrefActivity.kt4
-rw-r--r--kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefCheckbox.kt9
-rw-r--r--kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefColorPicker.kt10
-rw-r--r--kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefHeader.kt5
-rw-r--r--kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemBase.kt24
-rw-r--r--kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefItemCore.kt47
-rw-r--r--kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSeekbar.kt12
-rw-r--r--kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefSubItems.kt1
-rw-r--r--kpref-activity/src/main/kotlin/ca/allanwang/kau/kpref/activity/items/KPrefText.kt10
-rw-r--r--sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt7
10 files changed, 78 insertions, 51 deletions
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<Boolean>) : KPrefItemBase<Boolean
(innerView as AppCompatCheckBox).isChecked = pref
}
- override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) {
- super.onPostBindView(viewHolder, textColor, accentColor)
- val checkbox = viewHolder.bindInnerView<CheckBox>(R.layout.kau_pref_checkbox)
- if (accentColor != null) checkbox.tint(accentColor)
+ override fun bindView(holder: ViewHolder, payloads: List<Any>) {
+ super.bindView(holder, payloads)
+ val checkbox = holder.bindInnerView<CheckBox>(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<Int>(builder) {
- override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) {
- super.onPostBindView(viewHolder, textColor, accentColor)
+ override fun bindView(holder: ViewHolder, payloads: List<Any>) {
+ super.bindView(holder, payloads)
builder.apply {
titleRes = core.titleFun()
colorCallback = { pref = it }
}
if (builder.showPreview) {
- val preview = viewHolder.bindInnerView<CircleView>(R.layout.kau_pref_color)
+ val preview = holder.bindInnerView<CircleView>(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<Any>) {
+ 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<T>(val base: BaseContract<T>) : KPrefItemCore(base) {
+abstract class KPrefItemBase<T>(protected val base: BaseContract<T>) : KPrefItemCore(base) {
open var pref: T
get() = base.getter()
@@ -20,29 +20,33 @@ abstract class KPrefItemBase<T>(val base: BaseContract<T>) : 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<Any>) {
+ 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<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) {
+ if (_enabled) {
val onClick = base.onClick ?: return
kclick.onClick()
} else {
@@ -62,7 +66,7 @@ abstract class KPrefItemBase<T>(val base: BaseContract<T>) : 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<KPrefItemCore, KPrefItemCore.ViewHolder>(),
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<KPrefItemCor
title.setText(core.titleFun())
}
+ /**
+ * NewApi is suppressed as [buildIsLollipopAndUp] already covers it
+ */
@SuppressLint("NewApi")
@CallSuper
- override fun bindView(viewHolder: ViewHolder, payloads: List<Any>) {
- super.bindView(viewHolder, payloads)
- with(viewHolder) {
+ override fun bindView(holder: ViewHolder, payloads: List<Any>) {
+ 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<KPrefItemCor
var visible: () -> 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<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 {
- 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<KPrefItemCor
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 {
- 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<Int>(
override fun KClick<Int>.defaultOnClick() = Unit
- override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) {
- super.onPostBindView(viewHolder, textColor, accentColor)
- val text = viewHolder.bindInnerView<TextView>(R.layout.kau_pref_seekbar_text)
- if (textColor != null) text.setTextColor(textColor)
+ override fun bindView(holder: ViewHolder, payloads: List<Any>) {
+ super.bindView(holder, payloads)
+ val text = holder.bindInnerView<TextView>(R.layout.kau_pref_seekbar_text)
+ withTextColor(text::setTextColor)
val tvc = builder.textViewConfigs
text.tvc()
- val seekbar = viewHolder.bindLowerView<SeekBar>(R.layout.kau_pref_seekbar) {
+ val seekbar = holder.bindLowerView<SeekBar>(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<Int>(
}
})
}
- 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<T>(open val builder: KPrefTextContract<T>) : 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<TextView>(R.layout.kau_pref_text)
- if (textColor != null) textview.setTextColor(textColor)
- textview.text = builder.textGetter(pref)
+ override fun bindView(holder: ViewHolder, payloads: List<Any>) {
+ super.bindView(holder, payloads)
+ val textView = holder.bindInnerView<TextView>(R.layout.kau_pref_text)
+ withTextColor(textView::setTextColor)
+ textView.text = builder.textGetter(pref)
}
/**
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 b27a713..91def54 100644
--- a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt
+++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt
@@ -184,7 +184,12 @@ class MainActivity : KPrefActivity() {
header(R.string.long_prefs)
- checkbox(R.string.checkbox_3, { KPrefSample.check2 }, { KPrefSample.check2 = it; reloadByTitle(R.string.checkbox_3) }) {
+ /**
+ * Showcases a little trick. The id for reloading is [R.string.checkbox_2],
+ * but the title displayed is still [R.string.checkbox_3]
+ */
+ checkbox(R.string.checkbox_2, { KPrefSample.check2 }, { KPrefSample.check2 = it; reloadByTitle(R.string.checkbox_3) }) {
+ titleFun = { R.string.checkbox_3 }
descRes = R.string.kau_lorem_ipsum
}