aboutsummaryrefslogtreecommitdiff
path: root/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/ColorPickerDialog.kt
blob: b264e580333cbff17a0bc0a3f67ffd46100e6f0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package ca.allanwang.kau.colorpicker

import android.content.Context
import android.graphics.Color
import android.support.annotation.DimenRes
import android.support.annotation.StringRes
import ca.allanwang.kau.utils.INVALID_ID
import ca.allanwang.kau.utils.string
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.Theme

class ColorBuilder : ColorContract {
    override var title: String? = null
    override var titleRes: Int = INVALID_ID
    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
}

interface ColorContract {
    var title: String?
    @setparam:StringRes
    var titleRes: Int
    var allowCustom: Boolean
    var allowCustomAlpha: Boolean
    var isAccent: Boolean
    @setparam:StringRes
    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?
}

/**
 * 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: 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
}