aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt
blob: cc09a3303a591095bdca39ec81918b2a5d6e6a62 (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
package ca.allanwang.kau.kpref.items

import android.support.annotation.StringRes
import android.view.View
import android.widget.CheckBox
import ca.allanwang.kau.R
import ca.allanwang.kau.kpref.KPrefAdapterBuilder
import ca.allanwang.kau.utils.tint

/**
 * Created by Allan Wang on 2017-06-07.
 *
 * 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) {

    override fun defaultOnClick(itemView: View, innerContent: View?): Boolean {
        pref = !pref
        (innerContent as CheckBox).isChecked = pref
        return true
    }

    override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) {
        super.onPostBindView(viewHolder, textColor, accentColor)
        val checkbox = viewHolder.bindInnerView<CheckBox>(R.layout.kau_preference_checkbox)
        if (accentColor != null) checkbox.tint(accentColor)
        checkbox.isChecked = pref
        checkbox.jumpDrawablesToCurrentState() //Cancel the animation
    }

    override fun getType(): Int = R.id.kau_item_pref_checkbox

}