From 90e61e0fef26b7134cdda1b97f1a358d7496c4ec Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Wed, 14 Jun 2017 16:09:43 -0700 Subject: Add text pref with example --- .../kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt | 13 ++++++-- .../ca/allanwang/kau/kpref/items/KPrefText.kt | 39 ++++++++++++++++++++++ .../kotlin/ca/allanwang/kau/utils/ContextUtils.kt | 7 ++++ .../src/main/res/layout/kau_preference_text.xml | 7 ++++ 4 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt create mode 100644 library/src/main/res/layout/kau_preference_text.xml (limited to 'library/src/main') diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt index cc267d6..3e33abf 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt @@ -31,14 +31,21 @@ class KPrefAdapterBuilder { fun checkbox(@StringRes title: Int, coreBuilder: KPrefItemCore.Builder.() -> Unit = {}, - itemBuilder: KPrefItemBase.Builder.() -> Unit = {}) = list.add(KPrefCheckbox( - this, title, coreBuilder, itemBuilder)) + itemBuilder: KPrefItemBase.Builder.() -> Unit = {} + ) = list.add(KPrefCheckbox(this, title, coreBuilder, itemBuilder)) fun colorPicker(@StringRes title: Int, coreBuilder: KPrefItemCore.Builder.() -> Unit = {}, itemBuilder: KPrefItemBase.Builder.() -> Unit = {}, colorBuilder: Builder.() -> Unit = {}, - showPreview: Boolean = true) = list.add(KPrefColorPicker(this, title, coreBuilder, itemBuilder, colorBuilder, showPreview)) + showPreview: Boolean = true + ) = list.add(KPrefColorPicker(this, title, coreBuilder, itemBuilder, colorBuilder, showPreview)) + + fun text(@StringRes title: Int, + coreBuilder: KPrefItemCore.Builder.() -> Unit = {}, + itemBuilder: KPrefItemBase.Builder.() -> Unit = {}, + textGetter: (T) -> String? = { it?.toString() } + ) = list.add(KPrefText(this, title, coreBuilder, itemBuilder, textGetter)) internal val list: MutableList = mutableListOf() diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt new file mode 100644 index 0000000..52e9614 --- /dev/null +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt @@ -0,0 +1,39 @@ +package ca.allanwang.kau.kpref.items + +import android.support.annotation.StringRes +import android.view.View +import android.widget.TextView +import ca.allanwang.kau.R +import ca.allanwang.kau.kpref.KPrefAdapterBuilder +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 + * + */ +class KPrefText(builder: KPrefAdapterBuilder, + @StringRes title: Int, + coreBuilder: KPrefItemCore.Builder.() -> Unit = {}, + itemBuilder: Builder.() -> Unit = {}, + val textGetter: (T) -> String? = { it?.toString() } +) : KPrefItemBase(builder, title, coreBuilder, itemBuilder) { + + override fun defaultOnClick(itemView: View, innerContent: View?): Boolean { + itemView.context.toast("No click function set") + return true + } + + override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { + super.onPostBindView(viewHolder, textColor, accentColor) + val textview = viewHolder.bindInnerView(R.layout.kau_preference_text) + if (textColor != null) textview.setTextColor(textColor) + textview.text = textGetter.invoke(pref) + } + + override fun getType(): Int = R.id.kau_item_pref_checkbox + +} \ No newline at end of file diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt index b6da0cb..f5361c3 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt @@ -133,6 +133,13 @@ fun Context.showChangelog(@XmlRes xmlRes: Int) { }).start() } +/** + * Wrapper function for the MaterialDialog builder + * Must end with build() to return the dialog + * Don't forget to call show() on the result to display the dialog + */ +fun Context.materialDialog(action: MaterialDialog.Builder.() -> MaterialDialog): MaterialDialog = MaterialDialog.Builder(this).action() + val Context.isNetworkAvailable: Boolean get() { val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager diff --git a/library/src/main/res/layout/kau_preference_text.xml b/library/src/main/res/layout/kau_preference_text.xml new file mode 100644 index 0000000..bbabdfb --- /dev/null +++ b/library/src/main/res/layout/kau_preference_text.xml @@ -0,0 +1,7 @@ + \ No newline at end of file -- cgit v1.2.3