From e6c13788e94d10832116fc08e4c71d5f0d38511b Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Fri, 16 Jun 2017 23:35:22 -0700 Subject: Embed color attributes and clean up sample --- .../kotlin/ca/allanwang/kau/changelog/Changelog.kt | 4 +- .../kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt | 63 ++++++++++++---------- .../kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt | 5 ++ .../ca/allanwang/kau/kpref/items/KPrefCheckbox.kt | 4 +- .../allanwang/kau/kpref/items/KPrefColorPicker.kt | 27 ++++++---- .../ca/allanwang/kau/kpref/items/KPrefHeader.kt | 2 +- .../ca/allanwang/kau/kpref/items/KPrefItemBase.kt | 44 +++++++-------- .../ca/allanwang/kau/kpref/items/KPrefItemCore.kt | 32 +++++------ .../ca/allanwang/kau/kpref/items/KPrefText.kt | 27 ++++++---- .../src/main/kotlin/ca/allanwang/kau/logging/SL.kt | 6 --- .../ca/allanwang/kau/logging/TimberLogger.kt | 2 + 11 files changed, 119 insertions(+), 97 deletions(-) delete mode 100644 library/src/main/kotlin/ca/allanwang/kau/logging/SL.kt (limited to 'library/src/main') 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>) : RecyclerView.Adapter() { 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 { 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.() -> Unit = {}) - = list.add(KPrefCheckbox(this, KPrefItemBase.BaseBuilder() - .apply { - this.titleRes = title - this.getter = getter - this.setter = setter - builder() - })) + = list.add(KPrefCheckbox(KPrefItemBase.BaseBuilder(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 text(@StringRes title: Int, getter: (() -> T), setter: ((value: T) -> Unit), builder: KPrefText.KPrefTextContract.() -> Unit = {}) - = list.add(KPrefText(this, title, KPrefText.KPrefTextBuilder() - .apply { - this.titleRes = title - this.getter = getter - this.setter = setter - builder() - })) + = list.add(KPrefText(KPrefText.KPrefTextBuilder(this, title, getter, setter) + .apply { builder() })) internal val list: MutableList = 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) : LinkedHashSet(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 internal constructor(private val key: String, private val fallback: T, private val pref: KPref, var postSetter: (value: T) -> Unit = {}, lock: Any? = null) : Lazy, 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 -) : KPrefItemBase(adapterBuilder, builder) { +class KPrefCheckbox(builder: BaseContract) : KPrefItemBase(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(adapterBuilder, builder) { +class KPrefColorPicker(val builder: KPrefColorContract) : KPrefItemBase(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 by BaseBuilder(), 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, ColorContract { var showPreview: Boolean } + /** + * Default implementation of [KPrefColorContract] + */ + class KPrefColorBuilder(attributes: CoreAttributeContract, + titleRes: Int, + getter: () -> Int, + setter: (value: Int) -> Unit + ) : KPrefColorContract, BaseContract by BaseBuilder(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(adapterBuilder: KPrefAdapterBuilder, val base: BaseContract -) : KPrefItemCore(adapterBuilder, base) { +abstract class KPrefItemBase(val base: BaseContract) : 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(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(adapterBuilder: KPrefAdapterBuilder, val base: B override final fun getLayoutRes(): Int = R.layout.kau_preference - open class BaseBuilder : CoreBuilder(), BaseContract { - override var enabler: () -> Boolean = { true } - override var onClick: ((itemView: View, innerContent: View?, item: KPrefItemBase) -> Boolean)? = null - override var onDisabledClick: ((itemView: View, innerContent: View?, item: KPrefItemBase) -> 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 : CoreContract { var enabler: () -> Boolean var onClick: ((itemView: View, innerContent: View?, item: KPrefItemBase) -> Boolean)? var onDisabledClick: ((itemView: View, innerContent: View?, item: KPrefItemBase) -> Boolean)? - var getter: (() -> T)? - var setter: ((value: T) -> Unit)? + val getter: () -> T + val setter: (value: T) -> Unit } + + /** + * Default implementation of [BaseContract] + */ + class BaseBuilder(attributes: CoreAttributeContract, + titleRes: Int, + override val getter: () -> T, + override val setter: (value: T) -> Unit + ) : CoreContract by CoreBuilder(attributes, titleRes), BaseContract { + override var enabler: () -> Boolean = { true } + override var onClick: ((itemView: View, innerContent: View?, item: KPrefItemBase) -> Boolean)? = null + override var onDisabledClick: ((itemView: View, innerContent: View?, item: KPrefItemBase) -> 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() { +abstract class KPrefItemCore(val core: CoreContract) : AbstractItem() { 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(adapterBuilder: KPrefAdapterBuilder, - @StringRes title: Int, - val builder: KPrefTextContract -) : KPrefItemBase(adapterBuilder, title, builder) { +class KPrefText(val builder: KPrefTextContract) : KPrefItemBase(builder) { override fun defaultOnClick(itemView: View, innerContent: View?): Boolean { itemView.context.toast("No click function set") @@ -32,14 +28,25 @@ class KPrefText(adapterBuilder: KPrefAdapterBuilder, textview.text = builder.textGetter.invoke(pref) } - class KPrefTextBuilder : KPrefTextContract, BaseContract by BaseBuilder() { - override var textGetter: (T) -> String? = { it?.toString() } - } - + /** + * Extension of the base contract with an optional text getter + */ interface KPrefTextContract : BaseContract { var textGetter: (T) -> String? } + /** + * Default implementation of [KPrefTextContract] + */ + class KPrefTextBuilder( + attributes: CoreAttributeContract, + titleRes: Int, + getter: () -> T, + setter: (value: T) -> Unit + ) : KPrefTextContract, BaseContract by BaseBuilder(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" -- cgit v1.2.3