aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/kotlin/ca
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-06-16 20:13:55 -0700
committerAllan Wang <me@allanwang.ca>2017-06-16 20:13:55 -0700
commitfbbea20e78f196ffccda5a3a844a265772c68ea4 (patch)
treeab9d10bd73eaa808e86236850610a8cbe65188ec /library/src/main/kotlin/ca
parentd9de334c67dc34dec3a93055c1b198db7d3eb3cb (diff)
downloadkau-fbbea20e78f196ffccda5a3a844a265772c68ea4.tar.gz
kau-fbbea20e78f196ffccda5a3a844a265772c68ea4.tar.bz2
kau-fbbea20e78f196ffccda5a3a844a265772c68ea4.zip
Completely remap kpref binders
Diffstat (limited to 'library/src/main/kotlin/ca')
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/changelog/Changelog.kt2
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerDialog.kt92
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerView.kt16
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt50
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt8
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt52
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt5
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt49
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemCore.kt39
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt18
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt2
11 files changed, 198 insertions, 135 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 7f71195..1af03e1 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/changelog/Changelog.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/changelog/Changelog.kt
@@ -53,7 +53,7 @@ internal fun parse(context: Context, @XmlRes xmlRes: Int): List<Pair<String, Cha
}
internal enum class ChangelogType(val tag: String, val attr: String, @LayoutRes val layout: Int) {
- TITLE("title", "version", R.layout.kau_changelog_title),
+ TITLE("titleRes", "version", R.layout.kau_changelog_title),
ITEM("item", "text", R.layout.kau_changelog_content);
companion object {
diff --git a/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerDialog.kt b/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerDialog.kt
index 0ce236c..53d94ef 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerDialog.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerDialog.kt
@@ -3,65 +3,77 @@ package ca.allanwang.kau.dialogs.color
import android.content.Context
import android.graphics.Color
import ca.allanwang.kau.R
-import ca.allanwang.kau.utils.*
+import ca.allanwang.kau.utils.string
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.Theme
-class Builder {
- var title: String? = null
- var titleRes: Int = -1
- var allowCustom: Boolean = true
- var allowCustomAlpha: Boolean = false
- var isAccent: Boolean = false
- var defaultColor: Int = Color.BLACK
- var doneText: Int = R.string.kau_done
- var backText: Int = R.string.kau_back
- var cancelText: Int = R.string.kau_cancel
- var presetText: Int = R.string.kau_md_presets
- var customText: Int = R.string.kau_md_custom
+class ColorBuilder : ColorContract {
+ override var title: String? = null
+ override var titleRes: Int = -1
+ override var allowCustom: Boolean = true
+ override var allowCustomAlpha: Boolean = false
+ override var isAccent: Boolean = false
+ override var defaultColor: Int = Color.BLACK
+ override var doneText: Int = R.string.kau_done
+ override var backText: Int = R.string.kau_back
+ override var cancelText: Int = R.string.kau_cancel
+ override var presetText: Int = R.string.kau_md_presets
+ override var customText: Int = R.string.kau_md_custom
get() = if (allowCustom) field else 0
- var dynamicButtonColors: Boolean = true
- var circleSizeRes: Int = R.dimen.kau_color_circle_size
- var colorCallbacks: MutableList<((selectedColor: Int) -> Unit)> = mutableListOf()
- var colorsTop: IntArray? = null
- internal fun colorsTop(): IntArray =
- if (colorsTop != null) colorsTop!!
- else if (isAccent) ColorPalette.ACCENT_COLORS
- else ColorPalette.PRIMARY_COLORS
-
- var colorsSub: Array<IntArray>? = null
- internal fun colorsSub(): Array<IntArray>? =
- if (colorsTop != null) colorsSub
- else if (isAccent) ColorPalette.ACCENT_COLORS_SUB
- else ColorPalette.PRIMARY_COLORS_SUB
-
- var theme: Theme? = null
+ override var dynamicButtonColors: Boolean = true
+ override var circleSizeRes: Int = R.dimen.kau_color_circle_size
+ override var colorCallback: ((selectedColor: Int) -> Unit)? = null
+ override var colorsTop: IntArray? = null
+ override var colorsSub: Array<IntArray>? = null
+ override var theme: Theme? = null
+}
- fun applyNestedBuilder(action: Builder.() -> Unit) = this.action()
+interface ColorContract {
+ var title: String?
+ var titleRes: Int
+ var allowCustom: Boolean
+ var allowCustomAlpha: Boolean
+ var isAccent: Boolean
+ var defaultColor: Int
+ var doneText: Int
+ var backText: Int
+ var cancelText: Int
+ var presetText: Int
+ var customText: Int
+ var dynamicButtonColors: Boolean
+ var circleSizeRes: Int
+ var colorCallback: ((selectedColor: Int) -> Unit)?
+ var colorsTop: IntArray?
+ var colorsSub: Array<IntArray>?
+ var theme: Theme?
}
/**
* This is the extension that allows us to initialize the dialog
* Note that this returns just the dialog; you still need to call .show() to show it
*/
-fun Context.colorPickerDialog(action: Builder.() -> Unit): MaterialDialog {
- val b = Builder()
+fun Context.colorPickerDialog(action: ColorContract.() -> Unit): MaterialDialog {
+ val b = ColorBuilder()
b.action()
+ return colorPickerDialog(b)
+}
+
+fun Context.colorPickerDialog(contract: ColorContract): MaterialDialog {
val view = ColorPickerView(this)
val dialog = with(MaterialDialog.Builder(this)) {
- title(string(b.titleRes, b.title) ?: string(R.string.kau_md_color_palette))
+ title(string(contract.titleRes, contract.title) ?: string(R.string.kau_md_color_palette))
customView(view, false)
autoDismiss(false)
- positiveText(b.doneText)
- negativeText(b.cancelText)
- if (b.allowCustom) neutralText(b.presetText)
- onPositive { dialog, _ -> b.colorCallbacks.forEach { it.invoke(view.selectedColor) }; dialog.dismiss() }
+ positiveText(contract.doneText)
+ negativeText(contract.cancelText)
+ if (contract.allowCustom) neutralText(contract.presetText)
+ onPositive { dialog, _ -> contract.colorCallback?.invoke(view.selectedColor); dialog.dismiss() }
onNegative { dialog, _ -> view.backOrCancel() }
- if (b.allowCustom) onNeutral { dialog, _ -> view.toggleCustom() }
+ if (contract.allowCustom) onNeutral { dialog, _ -> view.toggleCustom() }
showListener { view.refreshColors() }
- if (b.theme != null) theme(b.theme!!)
+ if (contract.theme != null) theme(contract.theme!!)
build()
}
- view.bind(b, dialog)
+ view.bind(contract, dialog)
return dialog
} \ No newline at end of file
diff --git a/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerView.kt b/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerView.kt
index 8c26645..2643d27 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerView.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/ColorPickerView.kt
@@ -34,7 +34,7 @@ internal class ColorPickerView @JvmOverloads constructor(
if (context.resolveColor(android.R.attr.textColorPrimary).isColorDark()) Color.WHITE else 0xff424242.toInt())
val backgroundColorTint = backgroundColor.colorToForeground(0.2f)
lateinit var dialog: MaterialDialog
- lateinit var builder: Builder
+ lateinit var builder: ColorContract
lateinit var colorsTop: IntArray
var colorsSub: Array<IntArray>? = null
var topIndex: Int = -1
@@ -75,11 +75,19 @@ internal class ColorPickerView @JvmOverloads constructor(
View.inflate(context, R.layout.md_dialog_colorchooser, this)
}
- fun bind(builder: Builder, dialog: MaterialDialog) {
+ fun bind(builder: ColorContract, dialog: MaterialDialog) {
this.builder = builder
this.dialog = dialog
- this.colorsTop = builder.colorsTop()
- this.colorsSub = builder.colorsSub()
+ this.colorsTop = with(builder) {
+ if (colorsTop != null) colorsTop!!
+ else if (isAccent) ColorPalette.ACCENT_COLORS
+ else ColorPalette.PRIMARY_COLORS
+ }
+ this.colorsSub = with(builder) {
+ if (colorsTop != null) colorsSub
+ else if (isAccent) ColorPalette.ACCENT_COLORS_SUB
+ else ColorPalette.PRIMARY_COLORS_SUB
+ }
this.selectedColor = builder.defaultColor
if (builder.allowCustom) {
if (!builder.allowCustomAlpha) {
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 3e33abf..eec1e02 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt
@@ -4,7 +4,6 @@ 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.*
import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter
@@ -27,25 +26,48 @@ class KPrefAdapterBuilder {
var textColor: (() -> Int)? = null
var accentColor: (() -> Int)? = null
- fun header(@StringRes title: Int) = list.add(KPrefHeader(this, title))
+ fun header(@StringRes title: Int)
+ = list.add(KPrefHeader(this, KPrefItemCore.CoreBuilder()
+ .apply {
+ titleRes = title
+ }))
fun checkbox(@StringRes title: Int,
- coreBuilder: KPrefItemCore.Builder.() -> Unit = {},
- itemBuilder: KPrefItemBase.Builder<Boolean>.() -> Unit = {}
- ) = list.add(KPrefCheckbox(this, title, coreBuilder, itemBuilder))
+ 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()
+ }))
+
fun colorPicker(@StringRes title: Int,
- coreBuilder: KPrefItemCore.Builder.() -> Unit = {},
- itemBuilder: KPrefItemBase.Builder<Int>.() -> Unit = {},
- colorBuilder: Builder.() -> Unit = {},
- showPreview: Boolean = true
- ) = list.add(KPrefColorPicker(this, title, coreBuilder, itemBuilder, colorBuilder, showPreview))
+ 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()
+ }))
fun <T> text(@StringRes title: Int,
- coreBuilder: KPrefItemCore.Builder.() -> Unit = {},
- itemBuilder: KPrefItemBase.Builder<T>.() -> Unit = {},
- textGetter: (T) -> String? = { it?.toString() }
- ) = list.add(KPrefText<T>(this, title, coreBuilder, itemBuilder, textGetter))
+ 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()
+ }))
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 cc09a33..e4b80a1 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,11 +13,9 @@ import ca.allanwang.kau.utils.tint
* Checkbox preference
* When clicked, will toggle the preference and the apply the result to the checkbox
*/
-class KPrefCheckbox(builder: KPrefAdapterBuilder,
- @StringRes title: Int,
- coreBuilder: KPrefItemCore.Builder.() -> Unit = {},
- itemBuilder: Builder<Boolean>.() -> Unit = {}
-) : KPrefItemBase<Boolean>(builder, title, coreBuilder, itemBuilder) {
+class KPrefCheckbox(adapterBuilder: KPrefAdapterBuilder,
+ builder: BaseContract<Boolean>
+) : KPrefItemBase<Boolean>(adapterBuilder, 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 3d1ad21..7877b52 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
@@ -1,9 +1,10 @@
package ca.allanwang.kau.kpref.items
-import android.support.annotation.StringRes
import android.view.View
import ca.allanwang.kau.R
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
@@ -13,39 +14,50 @@ 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(builder: KPrefAdapterBuilder,
- @StringRes title: Int,
- coreBuilder: KPrefItemCore.Builder.() -> Unit = {},
- itemBuilder: KPrefItemBase.Builder<Int>.() -> Unit = {},
- val colorBuilder: ca.allanwang.kau.dialogs.color.Builder.() -> Unit = {},
- val showPreview: Boolean = false
-) : KPrefItemBase<Int>(builder, title, coreBuilder, itemBuilder) {
+class KPrefColorPicker(adapterBuilder: KPrefAdapterBuilder,
+ val builder: KPrefColorContract
+) : KPrefItemBase<Int>(adapterBuilder, builder) {
override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) {
super.onPostBindView(viewHolder, textColor, accentColor)
- //TODO add color circle view
- if (showPreview) {
+ builder.apply {
+ titleRes = core.titleRes
+ colorCallback = {
+ pref = it
+ }
+ }
+ if (builder.showPreview) {
val preview = viewHolder.bindInnerView<CircleView>(R.layout.kau_preference_color_preview)
preview.setBackgroundColor(pref)
preview.withBorder = true
+ builder.apply {
+ colorCallback = {
+ pref = it
+ if (builder.showPreview)
+ preview.setBackgroundColor(it)
+ }
+ }
}
}
override fun defaultOnClick(itemView: View, innerContent: View?): Boolean {
- itemView.context.colorPickerDialog {
- titleRes = this@KPrefColorPicker.title
- defaultColor = pref
- colorCallbacks.add {
- pref = it
- if (showPreview)
- (innerContent as CircleView).setBackgroundColor(it)
- }
- applyNestedBuilder(colorBuilder)
- }.show()
+ builder.apply {
+ defaultColor = pref //update color
+ }
+ itemView.context.colorPickerDialog(builder).show()
return true
}
+ class KPrefColorBuilder : KPrefColorContract, BaseContract<Int> by BaseBuilder<Int>(), ColorContract by ColorBuilder() {
+ override var showPreview: Boolean = true
+ override var titleRes: Int = -1
+ }
+
+ interface KPrefColorContract : BaseContract<Int>, ColorContract {
+ var showPreview: Boolean
+ }
+
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 94ef745..7abb631 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
@@ -1,6 +1,5 @@
package ca.allanwang.kau.kpref.items
-import android.support.annotation.StringRes
import android.view.View
import ca.allanwang.kau.R
import ca.allanwang.kau.kpref.KPrefAdapterBuilder
@@ -9,9 +8,9 @@ import ca.allanwang.kau.kpref.KPrefAdapterBuilder
* Created by Allan Wang on 2017-06-07.
*
* Header preference
- * This view just holds a title and is not clickable. It is styled using the accent color
+ * This view just holds a titleRes and is not clickable. It is styled using the accent color
*/
-class KPrefHeader(builder: KPrefAdapterBuilder, @StringRes title: Int) : KPrefItemCore(builder, title = title) {
+class KPrefHeader(adapterBuilder: KPrefAdapterBuilder, builder: CoreContract) : KPrefItemCore(adapterBuilder, 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 3300f37..1b04e76 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
@@ -1,7 +1,6 @@
package ca.allanwang.kau.kpref.items
import android.support.annotation.CallSuper
-import android.support.annotation.StringRes
import android.view.View
import ca.allanwang.kau.R
import ca.allanwang.kau.kpref.KPrefAdapterBuilder
@@ -15,24 +14,19 @@ import ca.allanwang.kau.utils.string
* Base class for pref setters that include the Shared Preference hooks
*/
-abstract class KPrefItemBase<T>(builder: KPrefAdapterBuilder,
- @StringRes title: Int,
- coreBuilder: KPrefItemCore.Builder.() -> Unit = {},
- itemBuilder: Builder<T>.() -> Unit = {}) : KPrefItemCore(builder, title, coreBuilder) {
+abstract class KPrefItemBase<T>(adapterBuilder: KPrefAdapterBuilder, val base: BaseContract<T>
+) : KPrefItemCore(adapterBuilder, base) {
var pref: T
- get() = itemBase.getter!!.invoke()
+ get() = base.getter!!.invoke()
set(value) {
- itemBase.setter!!.invoke(value)
+ base.setter!!.invoke(value)
}
var enabled: Boolean = true
- val itemBase: Builder<T>
init {
- itemBase = Builder<T>()
- itemBase.itemBuilder()
- if (itemBase.onClick == null) itemBase.onClick = {
+ if (base.onClick == null) base.onClick = {
itemView, innerContent, _ ->
defaultOnClick(itemView, innerContent)
}
@@ -43,9 +37,9 @@ abstract class KPrefItemBase<T>(builder: KPrefAdapterBuilder,
@CallSuper
override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) {
val c = viewHolder.itemView.context
- if (itemBase.getter == null) throw KPrefException("getter not set for ${c.string(title)}")
- if (itemBase.setter == null) throw KPrefException("setter not set for ${c.string(title)}")
- enabled = itemBase.enabler.invoke()
+ 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
container?.alpha = if (enabled) 1.0f else 0.3f
@@ -53,8 +47,8 @@ abstract class KPrefItemBase<T>(builder: KPrefAdapterBuilder,
}
override final fun onClick(itemView: View, innerContent: View?): Boolean {
- return if (enabled) itemBase.onClick?.invoke(itemView, innerContent, this) ?: false
- else itemBase.onDisabledClick?.invoke(itemView, innerContent, this) ?: false
+ return if (enabled) base.onClick?.invoke(itemView, innerContent, this) ?: false
+ else base.onDisabledClick?.invoke(itemView, innerContent, this) ?: false
}
override fun unbindView(holder: ViewHolder) {
@@ -68,11 +62,22 @@ abstract class KPrefItemBase<T>(builder: KPrefAdapterBuilder,
override final fun getLayoutRes(): Int = R.layout.kau_preference
- open class Builder<T> {
- var enabler: () -> Boolean = { true }
- var onClick: ((itemView: View, innerContent: View?, item: KPrefItemBase<T>) -> Boolean)? = null
- var onDisabledClick: ((itemView: View, innerContent: View?, item: KPrefItemBase<T>) -> Boolean)? = null
- var getter: (() -> T)? = null
- var setter: ((value: T) -> Unit)? = null
+ 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]
+ */
+ 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)?
}
} \ 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 b5741b0..adcf005 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
@@ -3,7 +3,6 @@ package ca.allanwang.kau.kpref.items
import android.support.annotation.CallSuper
import android.support.annotation.IdRes
import android.support.annotation.LayoutRes
-import android.support.annotation.StringRes
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
@@ -25,38 +24,30 @@ import com.mikepenz.iconics.typeface.IIcon
* Core class containing nothing but the view items
*/
-abstract class KPrefItemCore(val builder: KPrefAdapterBuilder,
- @StringRes val title: Int,
- coreBuilder: Builder.() -> Unit = {}) : AbstractItem<KPrefItemCore, KPrefItemCore.ViewHolder>() {
+abstract class KPrefItemCore(val adapterBuilder: KPrefAdapterBuilder,
+ val core: CoreContract) : AbstractItem<KPrefItemCore, KPrefItemCore.ViewHolder>() {
override final fun getViewHolder(v: View) = ViewHolder(v)
- val core: Builder
-
- init {
- core = Builder()
- core.coreBuilder()
- }
-
@CallSuper
override fun bindView(viewHolder: ViewHolder, payloads: List<Any>) {
super.bindView(viewHolder, payloads)
with(viewHolder) {
val context = itemView.context
- title.text = context.string(this@KPrefItemCore.title)
- if (core.description > 0)
- desc?.visible()?.setText(core.description)
+ title.text = context.string(core.titleRes)
+ if (core.descRes > 0)
+ desc?.visible()?.setText(core.descRes)
else
desc?.gone()
if (core.iicon != null) icon?.visible()?.setIcon(core.iicon, 24)
else icon?.gone()
innerFrame?.removeAllViews()
- val textColor = builder.textColor?.invoke()
+ val textColor = adapterBuilder.textColor?.invoke()
if (textColor != null) {
title.setTextColor(textColor)
desc?.setTextColor(textColor)
}
- val accentColor = builder.accentColor?.invoke()
+ val accentColor = adapterBuilder.accentColor?.invoke()
if (accentColor != null) {
icon?.drawable?.setTint(accentColor)
}
@@ -78,9 +69,19 @@ abstract class KPrefItemCore(val builder: KPrefAdapterBuilder,
}
}
- class Builder {
- var description: Int = -1
- var iicon: IIcon? = null
+ open class CoreBuilder : CoreContract {
+ override var titleRes: Int = -1
+ override var descRes: Int = -1
+ override var iicon: IIcon? = null
+ }
+
+ /**
+ * Mandatory values: [titleRes]
+ */
+ interface CoreContract {
+ var titleRes: Int
+ var descRes: Int
+ var iicon: IIcon?
}
class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
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 52e9614..b7091a6 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
@@ -15,12 +15,10 @@ import ca.allanwang.kau.utils.toast
* This is still a generic preference
*
*/
-class KPrefText<T>(builder: KPrefAdapterBuilder,
+class KPrefText<T>(adapterBuilder: KPrefAdapterBuilder,
@StringRes title: Int,
- coreBuilder: KPrefItemCore.Builder.() -> Unit = {},
- itemBuilder: Builder<T>.() -> Unit = {},
- val textGetter: (T) -> String? = { it?.toString() }
-) : KPrefItemBase<T>(builder, title, coreBuilder, itemBuilder) {
+ val builder: KPrefTextContract<T>
+) : KPrefItemBase<T>(adapterBuilder, title, builder) {
override fun defaultOnClick(itemView: View, innerContent: View?): Boolean {
itemView.context.toast("No click function set")
@@ -31,7 +29,15 @@ class KPrefText<T>(builder: KPrefAdapterBuilder,
super.onPostBindView(viewHolder, textColor, accentColor)
val textview = viewHolder.bindInnerView<TextView>(R.layout.kau_preference_text)
if (textColor != null) textview.setTextColor(textColor)
- textview.text = textGetter.invoke(pref)
+ textview.text = builder.textGetter.invoke(pref)
+ }
+
+ class KPrefTextBuilder<T> : KPrefTextContract<T>, BaseContract<T> by BaseBuilder<T>() {
+ override var textGetter: (T) -> String? = { it?.toString() }
+ }
+
+ interface KPrefTextContract<T> : BaseContract<T> {
+ var textGetter: (T) -> String?
}
override fun getType(): Int = R.id.kau_item_pref_checkbox
diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
index 77238f1..cc72921 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
@@ -141,7 +141,7 @@ fun Context.showChangelog(@XmlRes xmlRes: Int, customize: MaterialDialog.Builder
}
/**
- * Wrapper function for the MaterialDialog builder
+ * Wrapper function for the MaterialDialog adapterBuilder
* There is no need to call build() or show() as those are done by default
*/
fun Context.materialDialog(action: MaterialDialog.Builder.() -> Unit): MaterialDialog {