package ca.allanwang.kau.kpref.activity.items import android.widget.TextView import ca.allanwang.kau.kpref.activity.GlobalOptions import ca.allanwang.kau.kpref.activity.KClick import ca.allanwang.kau.kpref.activity.R import ca.allanwang.kau.utils.toast /** * Created by Allan Wang on 2017-06-14. * * Text preference * Holds a textview to display data on the right * This is still a generic preference * */ open class KPrefText(open val builder: KPrefTextContract) : KPrefItemBase(builder) { /** * Automatically reload on set */ override var pref: T get() = base.getter() set(value) { base.setter(value) builder.reloadSelf() } override fun KClick.defaultOnClick() { context.toast("No click function set") } override fun bindView(holder: ViewHolder, payloads: List) { super.bindView(holder, payloads) val textView = holder.bindInnerView(R.layout.kau_pref_text) withTextColor(textView::setTextColor) textView.text = builder.textGetter(pref) } /** * Extension of the base contract with an optional text getter */ interface KPrefTextContract : BaseContract { var textGetter: (T) -> String? } /** * Default implementation of [KPrefTextContract] */ class KPrefTextBuilder( globalOptions: GlobalOptions, titleId: Int, getter: () -> T, setter: (value: T) -> Unit ) : KPrefTextContract, BaseContract by BaseBuilder(globalOptions, titleId, getter, setter) { override var textGetter: (T) -> String? = { it?.toString() } } override fun getType(): Int = R.id.kau_item_pref_text }