aboutsummaryrefslogtreecommitdiff
path: root/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerDialog.kt
diff options
context:
space:
mode:
Diffstat (limited to 'colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerDialog.kt')
-rw-r--r--colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerDialog.kt102
1 files changed, 32 insertions, 70 deletions
diff --git a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerDialog.kt b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerDialog.kt
index 4202db1..02a1fff 100644
--- a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerDialog.kt
+++ b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerDialog.kt
@@ -15,90 +15,52 @@
*/
package ca.allanwang.kau.colorpicker
-import android.content.Context
+import android.annotation.SuppressLint
import android.graphics.Color
-import androidx.annotation.DimenRes
-import androidx.annotation.StringRes
-import ca.allanwang.kau.utils.INVALID_ID
-import ca.allanwang.kau.utils.string
+import androidx.annotation.ColorInt
import com.afollestad.materialdialogs.MaterialDialog
-import com.afollestad.materialdialogs.Theme
+import com.afollestad.materialdialogs.color.ColorCallback
+import com.afollestad.materialdialogs.color.colorChooser
+
+sealed class ColorOptions(val colors: IntArray, val subColors: Array<IntArray>?)
+
+object PrimaryColors : ColorOptions(ColorPalette.PRIMARY_COLORS, ColorPalette.PRIMARY_COLORS_SUB)
+object AccentColors : ColorOptions(ColorPalette.ACCENT_COLORS, ColorPalette.ACCENT_COLORS_SUB)
+class CustomColors(colors: IntArray, subColors: Array<IntArray>? = null) : ColorOptions(colors, subColors)
class ColorBuilder : ColorContract {
- override var title: String? = null
- override var titleRes: Int = INVALID_ID
+ override var colors: ColorOptions = PrimaryColors
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_custom
- get() = if (allowCustom) field else 0
- 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
+ override var callback: ColorCallback = null
}
interface ColorContract {
- var title: String?
- @setparam:StringRes
- var titleRes: Int
+ var colors: ColorOptions
var allowCustom: Boolean
var allowCustomAlpha: Boolean
- var isAccent: Boolean
- @setparam:StringRes
+ @setparam:ColorInt
var defaultColor: Int
- @setparam:StringRes
- var doneText: Int
- @setparam:StringRes
- var backText: Int
- @setparam:StringRes
- var cancelText: Int
- @setparam:StringRes
- var presetText: Int
- @setparam:StringRes
- var customText: Int
- var dynamicButtonColors: Boolean
- @setparam:DimenRes
- var circleSizeRes: Int
- var colorCallback: ((selectedColor: Int) -> Unit)?
- var colorsTop: IntArray?
- var colorsSub: Array<IntArray>?
- var theme: Theme?
+ var callback: ColorCallback
}
+@SuppressLint("CheckResult")
+fun MaterialDialog.kauColorChooser(action: ColorContract.() -> Unit) =
+ kauColorChooser(ColorBuilder().apply(action))
+
/**
- * 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
+ * Thin wrapper that exposes color chooser options as [ColorContract]
*/
-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(contract.titleRes, contract.title) ?: string(R.string.kau_md_color_palette))
- customView(view, false)
- autoDismiss(false)
- positiveText(contract.doneText)
- negativeText(contract.cancelText)
- if (contract.allowCustom) neutralText(contract.presetText)
- onPositive { dialog, _ -> contract.colorCallback?.invoke(view.selectedColor); dialog.dismiss() }
- onNegative { _, _ -> view.backOrCancel() }
- if (contract.allowCustom) onNeutral { _, _ -> view.toggleCustom() }
- showListener { view.refreshColors() }
- if (contract.theme != null) theme(contract.theme!!)
- build()
- }
- view.bind(contract, dialog)
- return dialog
-}
+@SuppressLint("CheckResult")
+fun MaterialDialog.kauColorChooser(c: ColorContract) {
+ colorChooser(
+ colors = c.colors.colors,
+ subColors = c.colors.subColors,
+ initialSelection = c.defaultColor,
+ allowCustomArgb = c.allowCustom,
+ showAlphaSelector = c.allowCustomAlpha,
+ selection = c.callback
+ )
+ positiveButton(R.string.kau_done)
+} \ No newline at end of file