diff options
author | Allan Wang <me@allanwang.ca> | 2018-09-06 11:18:45 -0400 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2018-09-06 11:18:45 -0400 |
commit | dca58435ac0ca941a488c117363e2920e083dfa9 (patch) | |
tree | 7c99353fe7b19980634d8806722964334b01acf7 /searchview/src | |
parent | 5c870486a1162ea18014d66fd42e8d6de3ef2069 (diff) | |
download | kau-dca58435ac0ca941a488c117363e2920e083dfa9.tar.gz kau-dca58435ac0ca941a488c117363e2920e083dfa9.tar.bz2 kau-dca58435ac0ca941a488c117363e2920e083dfa9.zip |
Add hint tint on foreground and update for nullable text
Diffstat (limited to 'searchview/src')
-rw-r--r-- | searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt | 5 | ||||
-rw-r--r-- | searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt | 11 |
2 files changed, 9 insertions, 7 deletions
diff --git a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt index 4175fd5..a3dc7f4 100644 --- a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt +++ b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt @@ -38,12 +38,13 @@ class SearchItem(val key: String, var backgroundColor: Int = 0xfffafafa.toInt() } - var styledContent: SpannableStringBuilder? = null + private var styledContent: SpannableStringBuilder? = null /** * Highlight the subText if it is present in the content */ - fun withHighlights(subText: String) { + internal fun withHighlights(subText: String?) { + subText ?: return val index = content.indexOf(subText, ignoreCase = true) if (index == -1) return styledContent = SpannableStringBuilder(content) 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 837b714..11e1748 100644 --- a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt +++ b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt @@ -215,7 +215,7 @@ class SearchView @JvmOverloads constructor( val list = if (value.isEmpty() && configs.noResultsFound != INVALID_ID) listOf(SearchItem("", context.string(configs.noResultsFound), iicon = null)) else value - if (configs.highlightQueryText && value.isNotEmpty()) list.forEach { it.withHighlights(editText.text.toString()) } + if (configs.highlightQueryText && value.isNotEmpty()) list.forEach { it.withHighlights(editText.text?.toString()) } cardTransition() adapter.setNewList(list) } @@ -250,7 +250,7 @@ class SearchView @JvmOverloads constructor( * The current text located in our searchview */ val query: String - get() = editText.text.toString().trim() + get() = editText.text?.toString()?.trim() ?: "" /* * Ripple start points and search view offset @@ -265,7 +265,7 @@ class SearchView @JvmOverloads constructor( View.inflate(context, R.layout.kau_search_view, this) z = 99f iconNav.setSearchIcon(configs.navIcon).setOnClickListener { revealClose() } - iconClear.setSearchIcon(configs.clearIcon).setOnClickListener { editText.text.clear() } + iconClear.setSearchIcon(configs.clearIcon).setOnClickListener { editText.text?.clear() } tintForeground(configs.foregroundColor) tintBackground(configs.backgroundColor) with(recycler) { @@ -304,7 +304,7 @@ class SearchView @JvmOverloads constructor( }) editText.setOnEditorActionListener { _, actionId, _ -> if (actionId == EditorInfo.IME_ACTION_SEARCH) { - if (configs.searchCallback(editText.text.toString(), this)) revealClose() + if (configs.searchCallback(editText.text?.toString() ?: "", this)) revealClose() else editText.hideKeyboard() return@setOnEditorActionListener true } @@ -403,6 +403,7 @@ class SearchView @JvmOverloads constructor( divider.setBackgroundColor(color.adjustAlpha(0.1f)) editText.tint(color) editText.setTextColor(ColorStateList.valueOf(color)) + editText.setHintTextColor(color.adjustAlpha(0.7f)) } /** @@ -441,7 +442,7 @@ class SearchView @JvmOverloads constructor( card.circularHide(menuX, menuHalfHeight, duration = configs.revealDuration, onFinish = { configs.closeListener?.invoke(this@SearchView) - if (configs.shouldClearOnClose) editText.text.clear() + if (configs.shouldClearOnClose) editText.text?.clear() }) } } |