aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-08-17 19:53:53 -0700
committerAllan Wang <me@allanwang.ca>2017-08-30 12:24:19 -0400
commit9432652b03ae4d01e3dda4325984637d9523b9e2 (patch)
tree75c1c1103ace3515649fe6246ea18c54e1617e18
parent5c45c832df044c400a76abad16a000a59d0bf54c (diff)
downloadkau-9432652b03ae4d01e3dda4325984637d9523b9e2.tar.gz
kau-9432652b03ae4d01e3dda4325984637d9523b9e2.tar.bz2
kau-9432652b03ae4d01e3dda4325984637d9523b9e2.zip
Add callback (#39)
-rw-r--r--sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt5
-rw-r--r--searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt23
2 files changed, 23 insertions, 5 deletions
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 93fc651..5d0bd36 100644
--- a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt
+++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt
@@ -230,6 +230,11 @@ class MainActivity : KPrefActivity() {
val items = wordBank.filter { it.contains(query) }.sorted().map { SearchItem(it) }
searchView.results = items
}
+ searchCallback = {
+ query, _ ->
+ toast("Enter pressed for $query")
+ true
+ }
textDebounceInterval = 0
noResultsFound = R.string.kau_no_results_found
shouldClearOnClose = false
diff --git a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt
index 1376115..e7d820e 100644
--- a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt
+++ b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt
@@ -15,6 +15,7 @@ import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.*
+import android.view.inputmethod.EditorInfo
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.ProgressBar
@@ -122,6 +123,11 @@ class SearchView @JvmOverloads constructor(
*/
var textCallback: (query: String, searchView: SearchView) -> Unit = { _, _ -> }
/**
+ * Callback for when the search action key is detected from the keyboard
+ * Returns true if the searchbar should close afterwards, and false otherwise
+ */
+ var searchCallback: (query: String, searchView: SearchView) -> Boolean = { _, _ -> false }
+ /**
* Debouncing interval between callbacks
*/
var textDebounceInterval: Long = 0
@@ -249,15 +255,21 @@ class SearchView @JvmOverloads constructor(
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}
- override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
+ override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
- override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
- val valid = !s.isNullOrBlank()
- if (valid) textCallback(s.toString().trim(), this@SearchView)
+ override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
+ if (s.isNotBlank()) textCallback(s.toString().trim(), this@SearchView)
else clearResults()
}
-
})
+ editText.setOnEditorActionListener { _, actionId, _ ->
+ if (actionId == EditorInfo.IME_ACTION_SEARCH) {
+ if (configs.searchCallback(editText.text.toString(), this)) revealClose()
+ else editText.hideKeyboard()
+ return@setOnEditorActionListener true
+ }
+ false
+ }
}
internal fun ImageView.setSearchIcon(iicon: IIcon?): ImageView {
@@ -389,6 +401,7 @@ class SearchView @JvmOverloads constructor(
recycler.gone()
editText.hideKeyboard()
}
+
}
@DslMarker