aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/preferences/PreferenceView.kt
blob: 60c3bb9dfc1ff281dd34c569c7aa6e7301ff28cb (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
package com.pitchedapps.frost.preferences

import android.annotation.SuppressLint
import android.content.Context
import android.support.annotation.ColorInt
import android.view.View
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import butterknife.ButterKnife
import com.pitchedapps.frost.R
import com.pitchedapps.frost.utils.bindView
import com.pitchedapps.frost.utils.toDrawable

@SuppressLint("ViewConstructor")
/**
 * Created by Allan Wang on 2017-06-06.
 */
open class PreferenceView<T>(
        context: Context, builder: PrefItem<T>, themeBuilder: ThemeBuilder?
) : LinearLayout(context) {

    val iconFrame: LinearLayout by bindView(R.id.icon_frame)
    val icon: ImageView by bindView(R.id.icon)
    val title: TextView by bindView(R.id.title)
    val desc: TextView by bindView(R.id.summary)
    val checkbox: CheckBox by bindView(R.id.checkbox)
    val key = builder.key
    private val getter = builder.getter
    private val setter = builder.setter
    var pref: T
        get() = getter.invoke(key)
        set(value) {
            setter.invoke(key, value)
        }
    val original = pref
    val hasChanged: Boolean
        get() = original == pref

    init {
        ButterKnife.bind(this)
        title.text = builder.title
        desc.text = builder.description
        if (builder.iicon == null) iconFrame.visibility = View.GONE
        else icon.setImageDrawable(builder.iicon.toDrawable(context, sizeDp = 48))
        if (themeBuilder != null) {
            with(themeBuilder) {
                if (textColor != null) setTextColor(textColor)
                if (accentColor != null) setAccentColor(accentColor)
            }
        }
        setClick(builder.onClick)
    }

    fun setClick(listener: (key: String, current: T, callback: (T) -> Unit) -> Unit) {
        viewWithClick().setOnClickListener {
            listener.invoke(key, pref, { pref = it })
        }
    }

    open fun viewWithClick(): View = this

    open fun setTextColor(@ColorInt color: Int) {
        title.setTextColor(color)
        desc.setTextColor(color)
        desc.alpha = 0.7f
    }

    //Accent color is not needed for basic prefs
    open fun setAccentColor(@ColorInt color: Int) {}

}