From ced1a5b5acb763e914f62ad1ad338c9877e606e6 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 11 Jun 2017 14:40:51 -0700 Subject: Add color preference preview --- .../kotlin/ca/allanwang/kau/dialogs/color/CircleView.kt | 14 ++++++++++++-- .../src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt | 3 ++- .../ca/allanwang/kau/kpref/items/KPrefColorPicker.kt | 15 +++++++++++++-- .../src/main/res/layout/kau_preference_color_preview.xml | 6 ++++++ 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 library/src/main/res/layout/kau_preference_color_preview.xml (limited to 'library/src/main') diff --git a/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/CircleView.kt b/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/CircleView.kt index a40895e..1b5e0fe 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/CircleView.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/CircleView.kt @@ -37,6 +37,7 @@ import ca.allanwang.kau.utils.toHSV */ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) { + private val borderWidthMicro: Float = context.getDip(1f) private val borderWidthSmall: Float = context.getDip(3f) private val borderWidthLarge: Float = context.getDip(5f) private var whiteOuterBound: Float = borderWidthLarge @@ -45,6 +46,14 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet private val whitePaint: Paint = Paint().apply { isAntiAlias = true; color = Color.WHITE } private val innerPaint: Paint = Paint().apply { isAntiAlias = true } private var selected: Boolean = false + var withBorder: Boolean = false + get() = field + set(value) { + if (field != value) { + field = value + invalidate() + } + } init { update(Color.DKGRAY) @@ -134,18 +143,19 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet super.onDraw(canvas) val centerWidth = (measuredWidth / 2).toFloat() val centerHeight = (measuredHeight / 2).toFloat() + if (withBorder) canvas.drawCircle(centerWidth, centerHeight, centerWidth, whitePaint) if (selected) { val whiteRadius = centerWidth - whiteOuterBound val innerRadius = whiteRadius - borderWidthSmall if (whiteRadius >= centerWidth) { canvas.drawCircle(centerWidth, centerHeight, centerWidth, whitePaint) } else { - canvas.drawCircle(centerWidth, centerHeight, centerWidth, outerPaint) + canvas.drawCircle(centerWidth, centerHeight, if (withBorder) centerWidth - borderWidthMicro else centerWidth, outerPaint) canvas.drawCircle(centerWidth, centerHeight, whiteRadius, whitePaint) } canvas.drawCircle(centerWidth, centerHeight, innerRadius, innerPaint) } else { - canvas.drawCircle(centerWidth, centerHeight, centerWidth, innerPaint) + canvas.drawCircle(centerWidth, centerHeight, if (withBorder) centerWidth - borderWidthMicro else centerWidth, innerPaint) } } 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 fc3b6ff..62f3d45 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt @@ -46,7 +46,8 @@ class KPrefAdapterBuilder { enabler: () -> Boolean = { true }, getter: () -> Int, setter: (value: Int) -> Unit, - configs: Builder.() -> Unit = {}) = list.add(KPrefColorPicker(this, title, description, iicon, enabler, getter, setter, configs)) + configs: Builder.() -> Unit = {}, + showPreview: Boolean = true) = list.add(KPrefColorPicker(this, title, description, iicon, enabler, getter, setter, configs, showPreview)) internal val list: MutableList = mutableListOf() 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 b590233..f157d1c 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 @@ -4,6 +4,7 @@ import android.support.annotation.StringRes import android.view.View import ca.allanwang.kau.R import ca.allanwang.kau.dialogs.color.Builder +import ca.allanwang.kau.dialogs.color.CircleView import ca.allanwang.kau.dialogs.color.colorPickerDialog import ca.allanwang.kau.kpref.KPrefAdapterBuilder import com.mikepenz.iconics.typeface.IIcon @@ -21,11 +22,17 @@ class KPrefColorPicker(builder: KPrefAdapterBuilder, enabler: () -> Boolean = { true }, getter: () -> Int, setter: (value: Int) -> Unit, - val configs: Builder.() -> Unit = {}) : KPrefItemBase(builder, title, description, iicon, enabler, getter, setter) { + val configs: Builder.() -> Unit = {}, + val showPreview: Boolean = false) : KPrefItemBase(builder, title, description, iicon, enabler, getter, setter) { override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { super.onPostBindView(viewHolder, textColor, accentColor) //TODO add color circle view + if (showPreview) { + val preview = viewHolder.bindInnerView(R.layout.kau_preference_color_preview) + preview.setBackgroundColor(pref) + preview.withBorder = true + } } @@ -33,7 +40,11 @@ class KPrefColorPicker(builder: KPrefAdapterBuilder, itemView.context.colorPickerDialog { titleRes = this@KPrefColorPicker.title defaultColor = pref - colorCallbacks.add { pref = it } + colorCallbacks.add { + pref = it + if (showPreview) + (innerContent as CircleView).setBackgroundColor(it) + } applyNestedBuilder(configs) }.show() return true diff --git a/library/src/main/res/layout/kau_preference_color_preview.xml b/library/src/main/res/layout/kau_preference_color_preview.xml new file mode 100644 index 0000000..2374971 --- /dev/null +++ b/library/src/main/res/layout/kau_preference_color_preview.xml @@ -0,0 +1,6 @@ + \ No newline at end of file -- cgit v1.2.3