aboutsummaryrefslogtreecommitdiff
path: root/library/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'library/src/main')
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt16
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt20
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt26
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt11
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt6
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt23
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt44
-rw-r--r--library/src/main/res/layout/kau_preference_checkbox.xml2
-rw-r--r--library/src/main/res/values/strings.xml2
9 files changed, 89 insertions, 61 deletions
diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt
index eb9202d..c441782 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt
@@ -1,6 +1,7 @@
package ca.allanwang.kau.kpref
import android.os.Bundle
+import android.support.annotation.StringRes
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
@@ -12,7 +13,6 @@ import ca.allanwang.kau.utils.resolveColor
import ca.allanwang.kau.views.RippleCanvas
import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter
-
abstract class KPrefActivity : AppCompatActivity() {
lateinit var adapter: FastItemAdapter<KPrefItemCore>
@@ -32,6 +32,20 @@ abstract class KPrefActivity : AppCompatActivity() {
adapter = recycler.setKPrefAdapter(onCreateKPrefs(savedInstanceState))
}
+ fun reload(vararg index: Int) {
+ if (index.isEmpty()) adapter.notifyAdapterDataSetChanged()
+ else index.forEach { adapter.notifyItemChanged(it) }
+ }
+
+ fun reloadByTitle(@StringRes vararg title:Int){
+ if (title.isEmpty()) return
+ adapter.adapterItems.forEachIndexed { index, item ->
+ if (title.any { item.title == it })
+ adapter.notifyItemChanged(index)
+ }
+ }
+
abstract fun onCreateKPrefs(savedInstanceState: Bundle?): KPrefAdapterBuilder.() -> Unit
}
+
diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt
index 57f4cb9..fc3b6ff 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt
@@ -3,6 +3,7 @@ package ca.allanwang.kau.kpref
import android.support.annotation.StringRes
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
+import ca.allanwang.kau.R
import ca.allanwang.kau.dialogs.color.Builder
import ca.allanwang.kau.kpref.items.KPrefCheckbox
import ca.allanwang.kau.kpref.items.KPrefColorPicker
@@ -17,7 +18,7 @@ import com.mikepenz.iconics.typeface.IIcon
fun RecyclerView.setKPrefAdapter(builder: KPrefAdapterBuilder.() -> Unit): FastItemAdapter<KPrefItemCore> {
layoutManager = LinearLayoutManager(context)
val adapter = FastItemAdapter<KPrefItemCore>()
- adapter.withOnClickListener { v, _, item, _ -> item.onClick(v) }
+ adapter.withOnClickListener { v, _, item, _ -> item.onClick(v, v.findViewById(R.id.kau_pref_inner_content)) }
val items = KPrefAdapterBuilder()
builder.invoke(items)
adapter.add(items.list)
@@ -27,30 +28,25 @@ fun RecyclerView.setKPrefAdapter(builder: KPrefAdapterBuilder.() -> Unit): FastI
class KPrefAdapterBuilder {
- var textColor: Int? = null
- get() = textColorGetter?.invoke() ?: field
- var accentColor: Int? = null
- get() = accentColorGetter?.invoke() ?: field
-
- var textColorGetter: (() -> Int)? = null
- var accentColorGetter: (() -> Int)? = null
+ var textColor: (() -> Int)? = null
+ var accentColor: (() -> Int)? = null
fun header(@StringRes title: Int) = list.add(KPrefHeader(this, title))
fun checkbox(@StringRes title: Int,
@StringRes description: Int = -1,
iicon: IIcon? = null,
- enabled: Boolean = true,
+ enabler: () -> Boolean = { true },
getter: () -> Boolean,
- setter: (value: Boolean) -> Unit) = list.add(KPrefCheckbox(this, title, description, iicon, enabled, getter, setter))
+ setter: (value: Boolean) -> Unit) = list.add(KPrefCheckbox(this, title, description, iicon, enabler, getter, setter))
fun colorPicker(@StringRes title: Int,
@StringRes description: Int = -1,
iicon: IIcon? = null,
- enabled: Boolean = true,
+ enabler: () -> Boolean = { true },
getter: () -> Int,
setter: (value: Int) -> Unit,
- configs: Builder.() -> Unit = {}) = list.add(KPrefColorPicker(this, title, description, iicon, enabled, getter, setter, configs))
+ configs: Builder.() -> Unit = {}) = list.add(KPrefColorPicker(this, title, description, iicon, enabler, getter, setter, configs))
internal val list: MutableList<KPrefItemCore> = mutableListOf()
diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt
index 474c3e5..75b480f 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt
@@ -5,7 +5,6 @@ import android.view.View
import android.widget.CheckBox
import ca.allanwang.kau.R
import ca.allanwang.kau.kpref.KPrefAdapterBuilder
-import ca.allanwang.kau.logging.SL
import ca.allanwang.kau.utils.tint
import com.mikepenz.iconics.typeface.IIcon
@@ -19,27 +18,24 @@ class KPrefCheckbox(builder: KPrefAdapterBuilder,
@StringRes title: Int,
@StringRes description: Int = -1,
iicon: IIcon? = null,
- enabled: Boolean = true,
+ enabler: () -> Boolean = { true },
getter: () -> Boolean,
- setter: (value: Boolean) -> Unit) : KPrefItemBase<Boolean>(builder, title, description, iicon, enabled, getter, setter) {
+ setter: (value: Boolean) -> Unit) : KPrefItemBase<Boolean>(builder, title, description, iicon, enabler, getter, setter) {
- override fun onClick(itemView: View): Boolean {
- val checkbox = itemView.findViewById(R.id.kau_pref_checkbox) as CheckBox
+ override fun onClick(itemView: View, innerContent: View?): Boolean {
pref = !pref
- checkbox.isChecked = pref
+ (innerContent as CheckBox).isChecked = pref
return true
}
- override fun onPostBindView(viewHolder: ViewHolder, builder: KPrefAdapterBuilder) {
- super.onPostBindView(viewHolder, builder)
- viewHolder.addInnerView(R.layout.kau_preference_checkbox)
- if (builder.accentColor != null) {
- val checkbox = viewHolder.itemView.findViewById(R.id.kau_pref_checkbox) as CheckBox
- checkbox.tint(builder.accentColor!!)
- checkbox.isChecked = pref //Checkbox tick needs to be delayed since notifyDataSetChanged will cancel the animation
- //It seems to work well here
- }
+ override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) {
+ super.onPostBindView(viewHolder, textColor, accentColor)
+ val checkbox = viewHolder.bindInnerView<CheckBox>(R.layout.kau_preference_checkbox)
+ if (accentColor != null) checkbox.tint(accentColor)
+ checkbox.isChecked = pref //Checkbox tick needs to be delayed since notifyDataSetChanged will cancel the animation
+ //It seems to work well here
+// checkbox.jumpDrawablesToCurrentState()
}
override fun getType(): Int = R.id.kau_item_pref_checkbox
diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt
index a9926ef..b590233 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt
@@ -18,17 +18,18 @@ class KPrefColorPicker(builder: KPrefAdapterBuilder,
@StringRes title: Int,
@StringRes description: Int = -1,
iicon: IIcon? = null,
- enabled: Boolean = true,
+ enabler: () -> Boolean = { true },
getter: () -> Int,
setter: (value: Int) -> Unit,
- val configs: Builder.() -> Unit = {}) : KPrefItemBase<Int>(builder, title, description, iicon, enabled, getter, setter) {
+ val configs: Builder.() -> Unit = {}) : KPrefItemBase<Int>(builder, title, description, iicon, enabler, getter, setter) {
- override fun onPostBindView(viewHolder: KPrefItemCore.ViewHolder, builder: KPrefAdapterBuilder) {
- super.onPostBindView(viewHolder, builder)
+ override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) {
+ super.onPostBindView(viewHolder, textColor, accentColor)
//TODO add color circle view
}
- override fun onClick(itemView: View): Boolean {
+
+ override fun onClick(itemView: View, innerContent: View?): Boolean {
itemView.context.colorPickerDialog {
titleRes = this@KPrefColorPicker.title
defaultColor = pref
diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt
index 1175fc2..0cd5901 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt
@@ -15,12 +15,12 @@ class KPrefHeader(builder: KPrefAdapterBuilder, @StringRes title: Int) : KPrefIt
override fun getLayoutRes(): Int = R.layout.kau_preference_header
- override fun onPostBindView(viewHolder: ViewHolder, builder: KPrefAdapterBuilder) {
+ override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) {
viewHolder.itemView.isClickable = false
- if (builder.accentColor != null) viewHolder.title.setTextColor(builder.accentColor!!)
+ if (accentColor != null) viewHolder.title.setTextColor(accentColor)
}
- override fun onClick(itemView: View): Boolean = true
+ override fun onClick(itemView: View, innerContent: View?): Boolean = true
override fun getType() = R.id.kau_item_pref_header
diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt
index adcbc8a..3c62b43 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt
@@ -2,9 +2,9 @@ package ca.allanwang.kau.kpref.items
import android.support.annotation.CallSuper
import android.support.annotation.StringRes
-import android.util.Log
import ca.allanwang.kau.R
import ca.allanwang.kau.kpref.KPrefAdapterBuilder
+import ca.allanwang.kau.logging.SL
import com.mikepenz.iconics.typeface.IIcon
/**
@@ -17,7 +17,7 @@ abstract class KPrefItemBase<T>(builder: KPrefAdapterBuilder,
@StringRes title: Int,
@StringRes description: Int = -1,
iicon: IIcon? = null,
- val enabled: Boolean = true,
+ val enabler: () -> Boolean = { true },
private val getter: () -> T,
private val setter: (value: T) -> Unit) : KPrefItemCore(builder, title, description, iicon) {
@@ -28,9 +28,22 @@ abstract class KPrefItemBase<T>(builder: KPrefAdapterBuilder,
}
@CallSuper
- override fun onPostBindView(viewHolder: ViewHolder, builder: KPrefAdapterBuilder) {
- viewHolder.itemView.isEnabled = enabled
- viewHolder.itemView.alpha = if (enabled) 1.0f else 0.3f
+ override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) {
+ val enabled = enabler.invoke()
+ with(viewHolder){
+ itemView.isEnabled = enabled
+ itemView.alpha = if (enabled) 1.0f else 0.3f
+ SL.d("Alpha ${itemView.isEnabled} ${itemView.alpha}")
+ }
+ }
+
+ override fun unbindView(holder: ViewHolder) {
+ super.unbindView(holder)
+ with(holder) {
+// itemView.isEnabled = true
+// itemView.alpha = 1.0f
+ SL.d("Unset alpha ${itemView.isEnabled} ${itemView.alpha}")
+ }
}
override final fun getLayoutRes(): Int = R.layout.kau_preference
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 98e977e..169c716 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
@@ -13,6 +13,7 @@ import android.widget.TextView
import butterknife.ButterKnife
import ca.allanwang.kau.R
import ca.allanwang.kau.kpref.KPrefAdapterBuilder
+import ca.allanwang.kau.logging.SL
import ca.allanwang.kau.utils.*
import com.mikepenz.fastadapter.items.AbstractItem
import com.mikepenz.iconics.typeface.IIcon
@@ -33,6 +34,7 @@ abstract class KPrefItemCore(val builder: KPrefAdapterBuilder,
@CallSuper
override fun bindView(viewHolder: ViewHolder, payloads: List<Any>) {
super.bindView(viewHolder, payloads)
+ SL.d("BINDVIEW")
with(viewHolder) {
val context = itemView.context
title.text = context.string(this@KPrefItemCore.title)
@@ -45,36 +47,31 @@ abstract class KPrefItemCore(val builder: KPrefAdapterBuilder,
icon?.setIcon(iicon, 48)
} else iconFrame?.gone()
innerFrame?.removeAllViews()
- setColors(this)
- onPostBindView(this, builder)
- }
- }
-
- fun setColors(viewHolder: ViewHolder) {
- with(viewHolder) {
- if (builder.textColor != null) {
- title.setTextColor(builder.textColor!!)
- desc?.setTextColor(builder.textColor!!)
+ val textColor = builder.textColor?.invoke()
+ if (textColor != null) {
+ title.setTextColor(textColor)
+ desc?.setTextColor(textColor)
}
- if (builder.accentColor != null) {
- icon?.drawable?.setTint(builder.accentColor!!)
+ val accentColor = builder.accentColor?.invoke()
+ if (accentColor != null) {
+ icon?.drawable?.setTint(accentColor)
}
+ onPostBindView(this, textColor, accentColor)
}
}
- abstract fun onPostBindView(viewHolder: ViewHolder, builder: KPrefAdapterBuilder)
+ abstract fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?)
- abstract fun onClick(itemView: View): Boolean
+ abstract fun onClick(itemView: View, innerContent: View?): Boolean
override fun unbindView(holder: ViewHolder) {
super.unbindView(holder)
+ SL.d("UNBINDVIEW")
with(holder) {
title.text = null
desc?.text = null
icon?.setImageDrawable(null)
- innerFrame?.removeAllViews()
- itemView.isEnabled = true
- itemView.alpha = 1.0f
+// innerFrame?.removeAllViews()
}
}
@@ -84,15 +81,24 @@ abstract class KPrefItemCore(val builder: KPrefAdapterBuilder,
val icon: ImageView? by bindOptionalView(R.id.kau_pref_icon)
val iconFrame: LinearLayout? by bindOptionalView(R.id.kau_pref_icon_frame)
val innerFrame: LinearLayout? by bindOptionalView(R.id.kau_pref_inner_frame)
+ val innerContent: View?
+ get() = itemView.findViewById(R.id.kau_pref_inner_content)
init {
ButterKnife.bind(v)
}
- fun addInnerView(@LayoutRes id: Int) {
- LayoutInflater.from(innerFrame!!.context).inflate(id, innerFrame)
+ inline fun <reified T : View> bindInnerView(@LayoutRes id: Int): 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)
+ }
+ return innerContent as T
}
+ inline fun <reified T : View> getInnerView() = innerContent as T
+
operator fun get(@IdRes id: Int): View = itemView.findViewById(id)
}
} \ No newline at end of file
diff --git a/library/src/main/res/layout/kau_preference_checkbox.xml b/library/src/main/res/layout/kau_preference_checkbox.xml
index 1f2807b..5ab368b 100644
--- a/library/src/main/res/layout/kau_preference_checkbox.xml
+++ b/library/src/main/res/layout/kau_preference_checkbox.xml
@@ -1,5 +1,5 @@
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/kau_pref_checkbox"
+ android:id="@+id/kau_pref_inner_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
diff --git a/library/src/main/res/values/strings.xml b/library/src/main/res/values/strings.xml
index 98dc0a1..10a73b9 100644
--- a/library/src/main/res/values/strings.xml
+++ b/library/src/main/res/values/strings.xml
@@ -12,4 +12,6 @@
<string name="kau_md_custom">Custom</string>
<string name="kau_md_presets">Presets</string>
<string name="kau_md_color_palette">Color Palette</string>
+
+ <string name="kau_kpref_title_placeholder">Title Placeholder</string>
</resources>