From c7e1a0ccbc5ab366b8d14720b0b5b0e7ab34c126 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 3 Jul 2017 20:21:30 -0700 Subject: Make themable null safe --- .../kau/adapters/FastItemThemedAdapter.kt | 42 +++++++++++----------- .../ca/allanwang/kau/kpref/items/KPrefItemCore.kt | 6 +++- .../ca/allanwang/kau/searchview/SearchItem.kt | 13 ++++--- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/library/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt b/library/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt index 83b8726..0fc590d 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/adapters/FastItemThemedAdapter.kt @@ -135,13 +135,13 @@ class ThemableIItemColorsDelegate : ThemableIItemColors { */ interface ThemableIItem : ThemableIItemColors { var themeEnabled: Boolean - fun bindTextColor(vararg views: TextView) - fun bindTextColorSecondary(vararg views: TextView) - fun bindDividerColor(vararg views: View) - fun bindAccentColor(vararg views: TextView) - fun bindBackgroundColor(vararg views: View) - fun bindBackgroundRipple(vararg views: View) - fun bindIconColor(vararg views: ImageView) + fun bindTextColor(vararg views: TextView?) + fun bindTextColorSecondary(vararg views: TextView?) + fun bindDividerColor(vararg views: View?) + fun bindAccentColor(vararg views: TextView?) + fun bindBackgroundColor(vararg views: View?) + fun bindBackgroundRipple(vararg views: View?) + fun bindIconColor(vararg views: ImageView?) } /** @@ -150,40 +150,40 @@ interface ThemableIItem : ThemableIItemColors { class ThemableIItemDelegate : ThemableIItem, ThemableIItemColors by ThemableIItemColorsDelegate() { override var themeEnabled: Boolean = true - override fun bindTextColor(vararg views: TextView) { + override fun bindTextColor(vararg views: TextView?) { val color = textColor ?: return - views.forEach { it.setTextColor(color) } + views.forEach { it?.setTextColor(color) } } - override fun bindTextColorSecondary(vararg views: TextView) { + override fun bindTextColorSecondary(vararg views: TextView?) { val color = textColor?.adjustAlpha(0.8f) ?: return - views.forEach { it.setTextColor(color) } + views.forEach { it?.setTextColor(color) } } - override fun bindAccentColor(vararg views: TextView) { + override fun bindAccentColor(vararg views: TextView?) { val color = accentColor ?: textColor ?: return - views.forEach { it.setTextColor(color) } + views.forEach { it?.setTextColor(color) } } - override fun bindDividerColor(vararg views: View) { + override fun bindDividerColor(vararg views: View?) { val color = (textColor ?: accentColor)?.adjustAlpha(0.1f) ?: return - views.forEach { it.setBackgroundColor(color) } + views.forEach { it?.setBackgroundColor(color) } } - override fun bindBackgroundColor(vararg views: View) { + override fun bindBackgroundColor(vararg views: View?) { val color = backgroundColor ?: return - views.forEach { it.setBackgroundColor(color) } + views.forEach { it?.setBackgroundColor(color) } } - override fun bindBackgroundRipple(vararg views: View) { + override fun bindBackgroundRipple(vararg views: View?) { val foreground = accentColor ?: textColor ?: return val background = backgroundColor ?: return val ripple = createSimpleRippleDrawable(foreground, background) - views.forEach { it.background = ripple } + views.forEach { it?.background = ripple } } - override fun bindIconColor(vararg views: ImageView) { + override fun bindIconColor(vararg views: ImageView?) { val color = accentColor ?: textColor ?: return - views.forEach { it.drawable.setTintList(ColorStateList.valueOf(color)) } + views.forEach { it?.drawable.setTintList(ColorStateList.valueOf(color)) } } } \ No newline at end of file diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt index 7d0f9f7..3c4a052 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt @@ -12,6 +12,8 @@ import android.widget.ImageView import android.widget.LinearLayout import android.widget.TextView import ca.allanwang.kau.R +import ca.allanwang.kau.adapters.ThemableIItem +import ca.allanwang.kau.adapters.ThemableIItemDelegate import ca.allanwang.kau.kpref.GlobalOptions import ca.allanwang.kau.kpref.KPrefMarker import ca.allanwang.kau.utils.* @@ -24,7 +26,8 @@ import com.mikepenz.iconics.typeface.IIcon * Core class containing nothing but the view items */ -abstract class KPrefItemCore(val core: CoreContract) : AbstractItem() { +abstract class KPrefItemCore(val core: CoreContract) : AbstractItem(), + ThemableIItem by ThemableIItemDelegate() { override final fun getViewHolder(v: View) = ViewHolder(v) @@ -50,6 +53,7 @@ abstract class KPrefItemCore(val core: CoreContract) : AbstractItem() { +) : KotlinIItem( + R.id.kau_item_search, + R.layout.kau_search_iitem, + {ViewHolder(it)} +) { companion object { @JvmStatic var foregroundColor: Int = 0xdd000000.toInt() @@ -47,12 +52,6 @@ class SearchItem(val key: String, styledContent!!.setSpan(StyleSpan(Typeface.BOLD), index, index + subText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) } - override fun getLayoutRes(): Int = R.layout.kau_search_iitem - - override fun getType(): Int = R.id.kau_item_search - - override fun getViewHolder(v: View): ViewHolder = ViewHolder(v) - override fun bindView(holder: ViewHolder, payloads: MutableList?) { super.bindView(holder, payloads) holder.title.setTextColor(foregroundColor) -- cgit v1.2.3