aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/kotlin/ca/allanwang
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-06-16 23:35:22 -0700
committerAllan Wang <me@allanwang.ca>2017-06-16 23:35:22 -0700
commite6c13788e94d10832116fc08e4c71d5f0d38511b (patch)
treec1048b24f7ddc02016013819ffe80f0b71b72c63 /library/src/main/kotlin/ca/allanwang
parentfbbea20e78f196ffccda5a3a844a265772c68ea4 (diff)
downloadkau-e6c13788e94d10832116fc08e4c71d5f0d38511b.tar.gz
kau-e6c13788e94d10832116fc08e4c71d5f0d38511b.tar.bz2
kau-e6c13788e94d10832116fc08e4c71d5f0d38511b.zip
Embed color attributes and clean up sample
Diffstat (limited to 'library/src/main/kotlin/ca/allanwang')
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/changelog/Changelog.kt4
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt63
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt5
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt4
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt27
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt2
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt44
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt32
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt27
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/logging/SL.kt6
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt2
11 files changed, 119 insertions, 97 deletions
diff --git a/library/src/main/kotlin/ca/allanwang/kau/changelog/Changelog.kt b/library/src/main/kotlin/ca/allanwang/kau/changelog/Changelog.kt
index 1af03e1..58ba3f3 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/changelog/Changelog.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/changelog/Changelog.kt
@@ -19,9 +19,7 @@ import org.xmlpull.v1.XmlPullParser
internal class ChangelogAdapter(val items: List<Pair<String, ChangelogType>>) : RecyclerView.Adapter<ChangelogAdapter.ChangelogVH>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ChangelogVH(LayoutInflater.from(parent.context)
- .inflate(getLayout(viewType), parent, false))
-
- private fun getLayout(position: Int) = items[position].second.layout
+ .inflate(items[viewType].second.layout, parent, false))
override fun onBindViewHolder(holder: ChangelogVH, position: Int) {
holder.text.text = items[position].first
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 eec1e02..75c24bc 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt
@@ -9,6 +9,12 @@ import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter
/**
* Created by Allan Wang on 2017-06-08.
+ *
+ * Houses all the components that can be called externally to setup the kpref adapter
+ */
+
+/**
+ * Base extension that will register the layout manager and adapter with the given items
*/
fun RecyclerView.setKPrefAdapter(builder: KPrefAdapterBuilder.() -> Unit): FastItemAdapter<KPrefItemCore> {
layoutManager = LinearLayoutManager(context)
@@ -21,53 +27,54 @@ fun RecyclerView.setKPrefAdapter(builder: KPrefAdapterBuilder.() -> Unit): FastI
return adapter
}
-class KPrefAdapterBuilder {
+/**
+ * Contains attributes shared amongst all kpref items
+ */
+interface CoreAttributeContract {
+ var textColor: (() -> Int)?
+ var accentColor: (() -> Int)?
+}
+
+/**
+ * Implementation of [CoreAttributeContract]
+ */
+class CoreAttributeBuilder : CoreAttributeContract {
+ override var textColor: (() -> Int)? = null
+ override var accentColor: (() -> Int)? = null
+}
- var textColor: (() -> Int)? = null
- var accentColor: (() -> Int)? = null
+/**
+ * Builder for kpref items
+ * Contains DSLs for every possible item
+ * The arguments are all the mandatory values plus an optional builder housing all the possible configurations
+ * The mandatory values are final so they cannot be edited in the builder
+ */
+class KPrefAdapterBuilder : CoreAttributeContract by CoreAttributeBuilder() {
fun header(@StringRes title: Int)
- = list.add(KPrefHeader(this, KPrefItemCore.CoreBuilder()
- .apply {
- titleRes = title
- }))
+ = list.add(KPrefHeader(KPrefItemCore.CoreBuilder(this, title)))
fun checkbox(@StringRes title: Int,
getter: (() -> Boolean),
setter: ((value: Boolean) -> Unit),
builder: KPrefItemBase.BaseContract<Boolean>.() -> Unit = {})
- = list.add(KPrefCheckbox(this, KPrefItemBase.BaseBuilder<Boolean>()
- .apply {
- this.titleRes = title
- this.getter = getter
- this.setter = setter
- builder()
- }))
+ = list.add(KPrefCheckbox(KPrefItemBase.BaseBuilder<Boolean>(this, title, getter, setter)
+ .apply { builder() }))
fun colorPicker(@StringRes title: Int,
getter: (() -> Int),
setter: ((value: Int) -> Unit),
builder: KPrefColorPicker.KPrefColorContract.() -> Unit = {})
- = list.add(KPrefColorPicker(this, KPrefColorPicker.KPrefColorBuilder()
- .apply {
- this.titleRes = title
- this.getter = getter
- this.setter = setter
- builder()
- }))
+ = list.add(KPrefColorPicker(KPrefColorPicker.KPrefColorBuilder(this, title, getter, setter)
+ .apply { builder() }))
fun <T> text(@StringRes title: Int,
getter: (() -> T),
setter: ((value: T) -> Unit),
builder: KPrefText.KPrefTextContract<T>.() -> Unit = {})
- = list.add(KPrefText<T>(this, title, KPrefText.KPrefTextBuilder<T>()
- .apply {
- this.titleRes = title
- this.getter = getter
- this.setter = setter
- builder()
- }))
+ = list.add(KPrefText<T>(KPrefText.KPrefTextBuilder<T>(this, title, getter, setter)
+ .apply { builder() }))
internal val list: MutableList<KPrefItemCore> = mutableListOf()
diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt
index 8665d80..fb42e65 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt
@@ -15,6 +15,11 @@ fun KPref.kpref(key: String, fallback: String, postSetter: (value: String) -> Un
class StringSet(set: Collection<String>) : LinkedHashSet<String>(set)
+/**
+ * Implementation of a kpref data item
+ * Contains a unique key for the shared preference as well as a nonnull fallback item
+ * Also contains an optional mutable postSetter that will be called every time a new value is given
+ */
class KPrefDelegate<T : Any> internal constructor(private val key: String, private val fallback: T, private val pref: KPref, var postSetter: (value: T) -> Unit = {}, lock: Any? = null) : Lazy<T>, java.io.Serializable {
@Volatile private var _value: Any = UNINITIALIZED
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 e4b80a1..5495c9f 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
@@ -13,9 +13,7 @@ import ca.allanwang.kau.utils.tint
* Checkbox preference
* When clicked, will toggle the preference and the apply the result to the checkbox
*/
-class KPrefCheckbox(adapterBuilder: KPrefAdapterBuilder,
- builder: BaseContract<Boolean>
-) : KPrefItemBase<Boolean>(adapterBuilder, builder) {
+class KPrefCheckbox(builder: BaseContract<Boolean>) : KPrefItemBase<Boolean>(builder) {
override fun defaultOnClick(itemView: View, innerContent: View?): Boolean {
pref = !pref
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 7877b52..9917ffb 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
@@ -6,7 +6,7 @@ import ca.allanwang.kau.dialogs.color.CircleView
import ca.allanwang.kau.dialogs.color.ColorBuilder
import ca.allanwang.kau.dialogs.color.ColorContract
import ca.allanwang.kau.dialogs.color.colorPickerDialog
-import ca.allanwang.kau.kpref.KPrefAdapterBuilder
+import ca.allanwang.kau.kpref.CoreAttributeContract
/**
* Created by Allan Wang on 2017-06-07.
@@ -14,9 +14,7 @@ import ca.allanwang.kau.kpref.KPrefAdapterBuilder
* ColorPicker preference
* When a color is successfully selected in the dialog, it will be saved as an int
*/
-class KPrefColorPicker(adapterBuilder: KPrefAdapterBuilder,
- val builder: KPrefColorContract
-) : KPrefItemBase<Int>(adapterBuilder, builder) {
+class KPrefColorPicker(val builder: KPrefColorContract) : KPrefItemBase<Int>(builder) {
override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) {
super.onPostBindView(viewHolder, textColor, accentColor)
@@ -49,15 +47,26 @@ class KPrefColorPicker(adapterBuilder: KPrefAdapterBuilder,
return true
}
- class KPrefColorBuilder : KPrefColorContract, BaseContract<Int> by BaseBuilder<Int>(), ColorContract by ColorBuilder() {
- override var showPreview: Boolean = true
- override var titleRes: Int = -1
- }
-
+ /**
+ * Extension of the base contract and [ColorContract] along with a showPreview option
+ */
interface KPrefColorContract : BaseContract<Int>, ColorContract {
var showPreview: Boolean
}
+ /**
+ * Default implementation of [KPrefColorContract]
+ */
+ class KPrefColorBuilder(attributes: CoreAttributeContract,
+ titleRes: Int,
+ getter: () -> Int,
+ setter: (value: Int) -> Unit
+ ) : KPrefColorContract, BaseContract<Int> by BaseBuilder<Int>(attributes, titleRes, getter, setter),
+ ColorContract by ColorBuilder() {
+ override var showPreview: Boolean = true
+ override var titleRes: Int = -1
+ }
+
override fun getType(): Int = R.id.kau_item_pref_color_picker
} \ No newline at end of file
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 7abb631..4f4192c 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
@@ -10,7 +10,7 @@ import ca.allanwang.kau.kpref.KPrefAdapterBuilder
* Header preference
* This view just holds a titleRes and is not clickable. It is styled using the accent color
*/
-class KPrefHeader(adapterBuilder: KPrefAdapterBuilder, builder: CoreContract) : KPrefItemCore(adapterBuilder, builder) {
+class KPrefHeader(builder: CoreContract) : KPrefItemCore(builder) {
override fun getLayoutRes(): Int = R.layout.kau_preference_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 1b04e76..76fa01e 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
@@ -3,10 +3,8 @@ package ca.allanwang.kau.kpref.items
import android.support.annotation.CallSuper
import android.view.View
import ca.allanwang.kau.R
-import ca.allanwang.kau.kpref.KPrefAdapterBuilder
-import ca.allanwang.kau.kpref.KPrefException
+import ca.allanwang.kau.kpref.CoreAttributeContract
import ca.allanwang.kau.utils.resolveDrawable
-import ca.allanwang.kau.utils.string
/**
* Created by Allan Wang on 2017-06-05.
@@ -14,13 +12,12 @@ import ca.allanwang.kau.utils.string
* Base class for pref setters that include the Shared Preference hooks
*/
-abstract class KPrefItemBase<T>(adapterBuilder: KPrefAdapterBuilder, val base: BaseContract<T>
-) : KPrefItemCore(adapterBuilder, base) {
+abstract class KPrefItemBase<T>(val base: BaseContract<T>) : KPrefItemCore(base) {
var pref: T
- get() = base.getter!!.invoke()
+ get() = base.getter.invoke()
set(value) {
- base.setter!!.invoke(value)
+ base.setter.invoke(value)
}
var enabled: Boolean = true
@@ -36,9 +33,6 @@ abstract class KPrefItemBase<T>(adapterBuilder: KPrefAdapterBuilder, val base: B
@CallSuper
override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) {
- val c = viewHolder.itemView.context
- if (base.getter == null) throw KPrefException("getter not set for ${c.string(core.titleRes)}")
- if (base.setter == null) throw KPrefException("setter not set for ${c.string(core.titleRes)}")
enabled = base.enabler.invoke()
with(viewHolder) {
if (!enabled) container?.background = null
@@ -62,22 +56,30 @@ abstract class KPrefItemBase<T>(adapterBuilder: KPrefAdapterBuilder, val base: B
override final fun getLayoutRes(): Int = R.layout.kau_preference
- open class BaseBuilder<T> : CoreBuilder(), 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 getter: (() -> T)? = null
- override var setter: ((value: T) -> Unit)? = null
- }
-
/**
- * Mandatory values: [getter], [setter]
+ * Extension of the core contract
+ * Since everything that extends the base is an actual preference, there must be a getter and setter
+ * The rest are optional and will have their defaults
*/
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 getter: (() -> T)?
- var setter: ((value: T) -> Unit)?
+ val getter: () -> T
+ val setter: (value: T) -> Unit
}
+
+ /**
+ * Default implementation of [BaseContract]
+ */
+ class BaseBuilder<T>(attributes: CoreAttributeContract,
+ titleRes: Int,
+ override val getter: () -> T,
+ override val setter: (value: T) -> Unit
+ ) : CoreContract by CoreBuilder(attributes, titleRes), 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
+ }
+
} \ 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 adcf005..c73ea43 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,8 +12,7 @@ import android.widget.LinearLayout
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.kpref.CoreAttributeContract
import ca.allanwang.kau.utils.*
import com.mikepenz.fastadapter.items.AbstractItem
import com.mikepenz.iconics.typeface.IIcon
@@ -24,8 +23,7 @@ import com.mikepenz.iconics.typeface.IIcon
* Core class containing nothing but the view items
*/
-abstract class KPrefItemCore(val adapterBuilder: KPrefAdapterBuilder,
- val core: CoreContract) : AbstractItem<KPrefItemCore, KPrefItemCore.ViewHolder>() {
+abstract class KPrefItemCore(val core: CoreContract) : AbstractItem<KPrefItemCore, KPrefItemCore.ViewHolder>() {
override final fun getViewHolder(v: View) = ViewHolder(v)
@@ -42,12 +40,12 @@ abstract class KPrefItemCore(val adapterBuilder: KPrefAdapterBuilder,
if (core.iicon != null) icon?.visible()?.setIcon(core.iicon, 24)
else icon?.gone()
innerFrame?.removeAllViews()
- val textColor = adapterBuilder.textColor?.invoke()
+ val textColor = core.attributes.textColor?.invoke()
if (textColor != null) {
title.setTextColor(textColor)
desc?.setTextColor(textColor)
}
- val accentColor = adapterBuilder.accentColor?.invoke()
+ val accentColor = core.attributes.accentColor?.invoke()
if (accentColor != null) {
icon?.drawable?.setTint(accentColor)
}
@@ -69,21 +67,25 @@ abstract class KPrefItemCore(val adapterBuilder: KPrefAdapterBuilder,
}
}
- open class CoreBuilder : CoreContract {
- override var titleRes: Int = -1
- override var descRes: Int = -1
- override var iicon: IIcon? = null
- }
-
/**
- * Mandatory values: [titleRes]
+ * Core values for all kpref items
*/
interface CoreContract {
- var titleRes: Int
+ val attributes: CoreAttributeContract
+ val titleRes: Int
var descRes: Int
var iicon: IIcon?
}
+ /**
+ * Default impementation of [CoreContract]
+ */
+ class CoreBuilder(override val attributes: CoreAttributeContract,
+ override val titleRes: Int) : CoreContract {
+ override var descRes: Int = -1
+ override var iicon: IIcon? = null
+ }
+
class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
val title: TextView by bindView(R.id.kau_pref_title)
val container: ViewGroup? by bindOptionalView(R.id.kau_pref_container)
@@ -102,8 +104,6 @@ abstract class KPrefItemCore(val adapterBuilder: KPrefAdapterBuilder,
if (innerContent !is T) {
innerFrame!!.removeAllViews()
LayoutInflater.from(innerFrame!!.context).inflate(id, innerFrame)
- } else {
- SL.d("Inner view still attached")
}
return innerContent as T
}
diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt
index b7091a6..0daa054 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt
@@ -1,10 +1,9 @@
package ca.allanwang.kau.kpref.items
-import android.support.annotation.StringRes
import android.view.View
import android.widget.TextView
import ca.allanwang.kau.R
-import ca.allanwang.kau.kpref.KPrefAdapterBuilder
+import ca.allanwang.kau.kpref.CoreAttributeContract
import ca.allanwang.kau.utils.toast
/**
@@ -15,10 +14,7 @@ import ca.allanwang.kau.utils.toast
* This is still a generic preference
*
*/
-class KPrefText<T>(adapterBuilder: KPrefAdapterBuilder,
- @StringRes title: Int,
- val builder: KPrefTextContract<T>
-) : KPrefItemBase<T>(adapterBuilder, title, builder) {
+class KPrefText<T>(val builder: KPrefTextContract<T>) : KPrefItemBase<T>(builder) {
override fun defaultOnClick(itemView: View, innerContent: View?): Boolean {
itemView.context.toast("No click function set")
@@ -32,14 +28,25 @@ class KPrefText<T>(adapterBuilder: KPrefAdapterBuilder,
textview.text = builder.textGetter.invoke(pref)
}
- class KPrefTextBuilder<T> : KPrefTextContract<T>, BaseContract<T> by BaseBuilder<T>() {
- override var textGetter: (T) -> String? = { it?.toString() }
- }
-
+ /**
+ * Extension of the base contract with an optional text getter
+ */
interface KPrefTextContract<T> : BaseContract<T> {
var textGetter: (T) -> String?
}
+ /**
+ * Default implementation of [KPrefTextContract]
+ */
+ class KPrefTextBuilder<T>(
+ attributes: CoreAttributeContract,
+ titleRes: Int,
+ getter: () -> T,
+ setter: (value: T) -> Unit
+ ) : KPrefTextContract<T>, BaseContract<T> by BaseBuilder<T>(attributes, titleRes, getter, setter) {
+ override var textGetter: (T) -> String? = { it?.toString() }
+ }
+
override fun getType(): Int = R.id.kau_item_pref_checkbox
} \ No newline at end of file
diff --git a/library/src/main/kotlin/ca/allanwang/kau/logging/SL.kt b/library/src/main/kotlin/ca/allanwang/kau/logging/SL.kt
deleted file mode 100644
index 5f67e23..0000000
--- a/library/src/main/kotlin/ca/allanwang/kau/logging/SL.kt
+++ /dev/null
@@ -1,6 +0,0 @@
-package ca.allanwang.kau.logging
-
-/**
- * Created by Allan Wang on 2017-06-08.
- */
-object SL: TimberLogger("KAU Sample") \ No newline at end of file
diff --git a/library/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt b/library/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt
index 2bbd4a6..3b4d651 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt
@@ -5,6 +5,8 @@ import timber.log.Timber
/**
* Created by Allan Wang on 2017-05-28.
+ *
+ * Timber extension that will embed the tag as part of the message for each log item
*/
open class TimberLogger(tag: String) {
internal val TAG = "$tag: %s"