aboutsummaryrefslogtreecommitdiff
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
parent7edb081d0e808982ba0d5c5d2a0021aa1befcd8c (diff)
downloadkau-90e61e0fef26b7134cdda1b97f1a358d7496c4ec.tar.gz
kau-90e61e0fef26b7134cdda1b97f1a358d7496c4ec.tar.bz2
kau-90e61e0fef26b7134cdda1b97f1a358d7496c4ec.zip
Add text pref with example
-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
-rw-r--r--sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt1
-rw-r--r--sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt28
-rw-r--r--sample/src/main/res/values/strings.xml2
7 files changed, 89 insertions, 8 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
diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt
index 96c7c71..a0fbc59 100644
--- a/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt
+++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt
@@ -14,4 +14,5 @@ object KPrefSample : KPref() {
var check1: Boolean by kpref("check1", true)
var check2: Boolean by kpref("check2", false)
var check3: Boolean by kpref("check3", false)
+ var text: String by kpref("text", "empty")
} \ No newline at end of file
diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt
index 4efd9f1..b8ba393 100644
--- a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt
+++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt
@@ -5,10 +5,7 @@ import android.view.Menu
import android.view.MenuItem
import ca.allanwang.kau.kpref.KPrefActivity
import ca.allanwang.kau.kpref.KPrefAdapterBuilder
-import ca.allanwang.kau.utils.darken
-import ca.allanwang.kau.utils.navigationBarColor
-import ca.allanwang.kau.utils.startActivitySlideIn
-import ca.allanwang.kau.utils.toast
+import ca.allanwang.kau.utils.*
import ca.allanwang.kau.views.RippleCanvas
import com.mikepenz.google_material_typeface_library.GoogleMaterial
@@ -30,7 +27,7 @@ class MainActivity : KPrefActivity() {
checkbox(title = R.string.checkbox_2, itemBuilder = {
getter = { KPrefSample.check2 }
- setter = { KPrefSample.check2 = it; reload(3) }
+ setter = { KPrefSample.check2 = it; reloadByTitle(R.string.checkbox_3) }
})
checkbox(title = R.string.checkbox_3, coreBuilder = {
@@ -80,6 +77,27 @@ class MainActivity : KPrefActivity() {
allowCustomAlpha = true
allowCustom = true
})
+
+ text<String>(title = R.string.text, coreBuilder = {
+ description = R.string.text_desc
+ }, itemBuilder = {
+ getter = { KPrefSample.text }
+ setter = { KPrefSample.text = it }
+ onClick = {
+ itemView, _, item ->
+ itemView.context.materialDialog {
+ title("Type Text")
+ input("Type here", item.pref, {
+ _, input ->
+ item.pref = input.toString()
+ reloadByTitle(R.string.text)
+ })
+ inputRange(0, 20)
+ build()
+ }.show()
+ true
+ }
+ })
}
override fun onCreate(savedInstanceState: Bundle?) {
diff --git a/sample/src/main/res/values/strings.xml b/sample/src/main/res/values/strings.xml
index 56addfd..7399593 100644
--- a/sample/src/main/res/values/strings.xml
+++ b/sample/src/main/res/values/strings.xml
@@ -13,4 +13,6 @@
<string name="color_custom">This selector allows custom colors</string>
<string name="color_no_custom">This selector does not allow custom colors</string>
<string name="color_custom_alpha">This selector allows for custom colors with alpha values</string>
+ <string name="text">Text Pref</string>
+ <string name="text_desc">Saves the text</string>
</resources>