aboutsummaryrefslogtreecommitdiff
path: root/library/src/main
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-06-14 16:09:43 -0700
committerAllan Wang <me@allanwang.ca>2017-06-14 16:09:43 -0700
commit90e61e0fef26b7134cdda1b97f1a358d7496c4ec (patch)
tree4a446cb56f80db8f382a9cec38f1da66ebe95901 /library/src/main
parent7edb081d0e808982ba0d5c5d2a0021aa1befcd8c (diff)
downloadkau-90e61e0fef26b7134cdda1b97f1a358d7496c4ec.tar.gz
kau-90e61e0fef26b7134cdda1b97f1a358d7496c4ec.tar.bz2
kau-90e61e0fef26b7134cdda1b97f1a358d7496c4ec.zip
Add text pref with example
Diffstat (limited to 'library/src/main')
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt13
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefText.kt39
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt7
-rw-r--r--library/src/main/res/layout/kau_preference_text.xml7
4 files changed, 63 insertions, 3 deletions
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<Boolean>.() -> Unit = {}) = list.add(KPrefCheckbox(
- this, title, coreBuilder, itemBuilder))
+ itemBuilder: KPrefItemBase.Builder<Boolean>.() -> Unit = {}
+ ) = list.add(KPrefCheckbox(this, title, coreBuilder, itemBuilder))
fun colorPicker(@StringRes title: Int,
coreBuilder: KPrefItemCore.Builder.() -> Unit = {},
itemBuilder: KPrefItemBase.Builder<Int>.() -> 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 <T> text(@StringRes title: Int,
+ coreBuilder: KPrefItemCore.Builder.() -> Unit = {},
+ itemBuilder: KPrefItemBase.Builder<T>.() -> Unit = {},
+ textGetter: (T) -> String? = { it?.toString() }
+ ) = list.add(KPrefText<T>(this, title, coreBuilder, itemBuilder, textGetter))
internal val list: MutableList<KPrefItemCore> = 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<T>(builder: KPrefAdapterBuilder,
+ @StringRes title: Int,
+ coreBuilder: KPrefItemCore.Builder.() -> Unit = {},
+ itemBuilder: Builder<T>.() -> Unit = {},
+ val textGetter: (T) -> String? = { it?.toString() }
+) : KPrefItemBase<T>(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<TextView>(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 @@
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+ android:id="@+id/kau_pref_inner_content"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:focusable="false"
+ android:clickable="false"
+ android:background="@null" /> \ No newline at end of file