aboutsummaryrefslogtreecommitdiff
path: root/library
diff options
context:
space:
mode:
Diffstat (limited to 'library')
-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