aboutsummaryrefslogtreecommitdiff
path: root/searchview/src
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2019-06-07 12:41:00 -0400
committerAllan Wang <me@allanwang.ca>2019-06-07 12:41:00 -0400
commit5d86d9089697b152192b2786fbe0c708dd8b5e2b (patch)
tree99b563b8c234330d2bbbc0145f086d8691ee9376 /searchview/src
parent572d470a2677eec0405a7b16ab9a2cfb954d6832 (diff)
parent879ac366074697dd0a7fbb2c3d99a48d7aeeb22d (diff)
downloadkau-5d86d9089697b152192b2786fbe0c708dd8b5e2b.tar.gz
kau-5d86d9089697b152192b2786fbe0c708dd8b5e2b.tar.bz2
kau-5d86d9089697b152192b2786fbe0c708dd8b5e2b.zip
Merge dev
Diffstat (limited to 'searchview/src')
-rw-r--r--searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt51
-rw-r--r--searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt133
-rw-r--r--searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchViewHolder.kt18
-rw-r--r--searchview/src/main/res/layout/kau_search_iitem.xml4
-rw-r--r--searchview/src/main/res/layout/kau_search_view.xml4
5 files changed, 157 insertions, 53 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 0ae856a..7d754c8 100644
--- a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt
+++ b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt
@@ -1,17 +1,36 @@
+/*
+ * Copyright 2018 Allan Wang
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package ca.allanwang.kau.searchview
import android.graphics.Typeface
import android.graphics.drawable.Drawable
-import android.support.constraint.ConstraintLayout
-import android.support.v7.widget.RecyclerView
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.StyleSpan
import android.view.View
import android.widget.ImageView
import android.widget.TextView
+import androidx.constraintlayout.widget.ConstraintLayout
+import androidx.recyclerview.widget.RecyclerView
import ca.allanwang.kau.iitems.KauIItem
-import ca.allanwang.kau.utils.*
+import ca.allanwang.kau.utils.adjustAlpha
+import ca.allanwang.kau.utils.gone
+import ca.allanwang.kau.utils.setIcon
+import ca.allanwang.kau.utils.setRippleBackground
+import ca.allanwang.kau.utils.visible
import com.mikepenz.google_material_typeface_library.GoogleMaterial
import com.mikepenz.iconics.typeface.IIcon
@@ -22,15 +41,16 @@ import com.mikepenz.iconics.typeface.IIcon
* Contains a [key] which acts as a unique identifier (eg url)
* and a [content] which is displayed in the item
*/
-class SearchItem(val key: String,
- val content: String = key,
- val description: String? = null,
- val iicon: IIcon? = GoogleMaterial.Icon.gmd_search,
- val image: Drawable? = null
+class SearchItem(
+ val key: String,
+ val content: String = key,
+ val description: String? = null,
+ val iicon: IIcon? = GoogleMaterial.Icon.gmd_search,
+ val image: Drawable? = null
) : KauIItem<SearchItem, SearchItem.ViewHolder>(
- R.layout.kau_search_iitem,
- { ViewHolder(it) },
- R.id.kau_item_search
+ R.layout.kau_search_iitem,
+ { ViewHolder(it) },
+ R.id.kau_item_search
) {
companion object {
@@ -48,7 +68,12 @@ class SearchItem(val key: String,
val index = content.indexOf(subText, ignoreCase = true)
if (index == -1) return
styledContent = SpannableStringBuilder(content)
- styledContent!!.setSpan(StyleSpan(Typeface.BOLD), index, index + subText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
+ styledContent!!.setSpan(
+ StyleSpan(Typeface.BOLD),
+ index,
+ index + subText.length,
+ Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
+ )
}
override fun bindView(holder: ViewHolder, payloads: MutableList<Any>) {
@@ -77,4 +102,4 @@ class SearchItem(val key: String,
val desc: TextView = v.findViewById(R.id.kau_search_desc)
val container: ConstraintLayout = v.findViewById(R.id.kau_search_item_frame)
}
-} \ No newline at end of file
+}
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 d223ec4..169e914 100644
--- a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt
+++ b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt
@@ -1,33 +1,71 @@
+/*
+ * Copyright 2018 Allan Wang
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package ca.allanwang.kau.searchview
import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
-import android.support.annotation.ColorInt
-import android.support.annotation.IdRes
-import android.support.transition.ChangeBounds
-import android.support.transition.TransitionManager
-import android.support.transition.TransitionSet
-import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
-import android.view.*
+import android.view.Menu
+import android.view.MenuItem
+import android.view.View
+import android.view.ViewGroup
+import android.view.ViewTreeObserver
import android.view.inputmethod.EditorInfo
import android.widget.FrameLayout
import android.widget.ImageView
+import androidx.annotation.ColorInt
+import androidx.annotation.IdRes
+import androidx.recyclerview.widget.RecyclerView
+import androidx.transition.ChangeBounds
+import androidx.transition.TransitionManager
+import androidx.transition.TransitionSet
import ca.allanwang.kau.kotlin.Debouncer2
import ca.allanwang.kau.kotlin.debounce
import ca.allanwang.kau.logging.KL
import ca.allanwang.kau.searchview.SearchView.Configs
-import ca.allanwang.kau.utils.*
+import ca.allanwang.kau.utils.INVALID_ID
+import ca.allanwang.kau.utils.addEndListener
+import ca.allanwang.kau.utils.adjustAlpha
+import ca.allanwang.kau.utils.circularHide
+import ca.allanwang.kau.utils.circularReveal
+import ca.allanwang.kau.utils.fadeIn
+import ca.allanwang.kau.utils.fadeOut
+import ca.allanwang.kau.utils.gone
+import ca.allanwang.kau.utils.goneIf
+import ca.allanwang.kau.utils.hideKeyboard
+import ca.allanwang.kau.utils.invisibleIf
+import ca.allanwang.kau.utils.isVisible
+import ca.allanwang.kau.utils.parentViewGroup
+import ca.allanwang.kau.utils.runOnUiThread
+import ca.allanwang.kau.utils.setIcon
+import ca.allanwang.kau.utils.setMarginTop
+import ca.allanwang.kau.utils.showKeyboard
+import ca.allanwang.kau.utils.string
+import ca.allanwang.kau.utils.tint
+import ca.allanwang.kau.utils.toDrawable
+import ca.allanwang.kau.utils.visible
+import ca.allanwang.kau.utils.withLinearAdapter
import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter
import com.mikepenz.google_material_typeface_library.GoogleMaterial
import com.mikepenz.iconics.typeface.IIcon
import kotlinx.android.synthetic.main.kau_search_view.view.*
-import org.jetbrains.anko.runOnUiThread
-
/**
* Created by Allan Wang on 2017-06-23.
@@ -41,7 +79,9 @@ import org.jetbrains.anko.runOnUiThread
* https://github.com/lapism/SearchView
*/
class SearchView @JvmOverloads constructor(
- context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
+ context: Context,
+ attrs: AttributeSet? = null,
+ defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
/**
@@ -162,13 +202,15 @@ class SearchView @JvmOverloads constructor(
* Click event for suggestion items
* This event is only triggered when [key] is not blank (like in [noResultsFound]
*/
- var onItemClick: (position: Int, key: String, content: String, searchView: SearchView) -> Unit = { _, _, _, _ -> }
+ var onItemClick: (position: Int, key: String, content: String, searchView: SearchView) -> Unit =
+ { _, _, _, _ -> }
/**
* Long click event for suggestion items
* This event is only triggered when [key] is not blank (like in [noResultsFound]
*/
- var onItemLongClick: (position: Int, key: String, content: String, searchView: SearchView) -> Unit = { _, _, _, _ -> }
+ var onItemLongClick: (position: Int, key: String, content: String, searchView: SearchView) -> Unit =
+ { _, _, _, _ -> }
/**
* If a [SearchItem]'s title contains the submitted query, make that portion bold
@@ -229,7 +271,8 @@ class SearchView @JvmOverloads constructor(
private val configs = Configs()
// views
- private var textCallback: Debouncer2<String, SearchView> = debounce(0) { query, _ -> KL.d { "Search query $query found; set your own textCallback" } }
+ private var textCallback: Debouncer2<String, SearchView> =
+ debounce(0) { query, _ -> KL.d { "Search query $query found; set your own textCallback" } }
private val adapter = FastItemAdapter<SearchItem>()
private var menuItem: MenuItem? = null
val isOpen: Boolean
@@ -274,7 +317,12 @@ class SearchView @JvmOverloads constructor(
if (item.key.isNotBlank()) configs.onItemClick(position, item.key, item.content, this@SearchView); true
}
withOnLongClickListener { _, _, item, position ->
- if (item.key.isNotBlank()) configs.onItemLongClick(position, item.key, item.content, this@SearchView); true
+ if (item.key.isNotBlank()) configs.onItemLongClick(
+ position,
+ item.key,
+ item.content,
+ this@SearchView
+ ); true
}
}
kau_search_edit_text.addTextChangedListener(object : TextWatcher {
@@ -293,8 +341,11 @@ class SearchView @JvmOverloads constructor(
})
kau_search_edit_text.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
- if (configs.searchCallback(kau_search_edit_text.text?.toString()
- ?: "", this)) revealClose()
+ if (configs.searchCallback(
+ kau_search_edit_text.text?.toString()
+ ?: "", this
+ )
+ ) revealClose()
else kau_search_edit_text.hideKeyboard()
return@setOnEditorActionListener true
}
@@ -309,12 +360,12 @@ class SearchView @JvmOverloads constructor(
internal fun cardTransition(builder: TransitionSet.() -> Unit = {}) {
TransitionManager.beginDelayedTransition(kau_search_cardview,
- //we are only using change bounds, as the recyclerview items may be animated as well,
- //which causes a measure IllegalStateException
- TransitionSet().addTransition(ChangeBounds()).apply {
- duration = configs.transitionDuration
- builder()
- })
+ //we are only using change bounds, as the recyclerview items may be animated as well,
+ //which causes a measure IllegalStateException
+ TransitionSet().addTransition(ChangeBounds()).apply {
+ duration = configs.transitionDuration
+ builder()
+ })
}
/**
@@ -330,10 +381,15 @@ class SearchView @JvmOverloads constructor(
* This is assuming that SearchView has already been added to a ViewGroup
* If not, see the extension function [bindSearchView]
*/
- fun bind(menu: Menu, @IdRes id: Int, @ColorInt menuIconColor: Int = Color.WHITE, config: Configs.() -> Unit = {}): SearchView {
+ fun bind(
+ menu: Menu,
+ @IdRes id: Int,
+ @ColorInt menuIconColor: Int = Color.WHITE,
+ config: Configs.() -> Unit = {}
+ ): SearchView {
config(config)
val menuItem = menu.findItem(id)
- ?: throw IllegalArgumentException("Menu item with given id doesn't exist")
+ ?: throw IllegalArgumentException("Menu item with given id doesn't exist")
if (menuItem.icon == null) menuItem.icon = GoogleMaterial.Icon.gmd_search.toDrawable(context, 18, menuIconColor)
kau_search_cardview.gone()
menuItem.setOnMenuItemClickListener { revealOpen(); true }
@@ -430,17 +486,16 @@ class SearchView @JvmOverloads constructor(
cardTransition {
addEndListener {
kau_search_cardview.circularHide(menuX, menuHalfHeight, duration = configs.revealDuration,
- onFinish = {
- configs.closeListener?.invoke(this@SearchView)
- if (configs.shouldClearOnClose) kau_search_edit_text.text?.clear()
- })
+ onFinish = {
+ configs.closeListener?.invoke(this@SearchView)
+ if (configs.shouldClearOnClose) kau_search_edit_text.text?.clear()
+ })
}
}
kau_search_recycler.gone()
kau_search_edit_text.hideKeyboard()
}
}
-
}
@DslMarker
@@ -450,7 +505,12 @@ annotation class KauSearch
* Helper function that binds to an activity's main view
*/
@KauSearch
-fun Activity.bindSearchView(menu: Menu, @IdRes id: Int, @ColorInt menuIconColor: Int = Color.WHITE, config: SearchView.Configs.() -> Unit = {}): SearchView = findViewById<ViewGroup>(android.R.id.content).bindSearchView(menu, id, menuIconColor, config)
+fun Activity.bindSearchView(
+ menu: Menu,
+ @IdRes id: Int,
+ @ColorInt menuIconColor: Int = Color.WHITE,
+ config: SearchView.Configs.() -> Unit = {}
+): SearchView = findViewById<ViewGroup>(android.R.id.content).bindSearchView(menu, id, menuIconColor, config)
/**
* Bind searchView to a menu item; call this in [Activity.onCreateOptionsMenu]
@@ -458,11 +518,16 @@ fun Activity.bindSearchView(menu: Menu, @IdRes id: Int, @ColorInt menuIconColor:
* it may be worthwhile to hold a reference to the searchview and only bind it if it hasn't been bound before
*/
@KauSearch
-fun ViewGroup.bindSearchView(menu: Menu, @IdRes id: Int, @ColorInt menuIconColor: Int = Color.WHITE, config: SearchView.Configs.() -> Unit = {}): SearchView {
+fun ViewGroup.bindSearchView(
+ menu: Menu,
+ @IdRes id: Int,
+ @ColorInt menuIconColor: Int = Color.WHITE,
+ config: SearchView.Configs.() -> Unit = {}
+): SearchView {
val searchView = SearchView(context)
- searchView.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
+ searchView.layoutParams =
+ FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
addView(searchView)
searchView.bind(menu, id, menuIconColor, config)
return searchView
}
-
diff --git a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchViewHolder.kt b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchViewHolder.kt
index 3f81dd0..a33b09b 100644
--- a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchViewHolder.kt
+++ b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchViewHolder.kt
@@ -1,3 +1,18 @@
+/*
+ * Copyright 2018 Allan Wang
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package ca.allanwang.kau.searchview
import android.view.MenuItem
@@ -21,5 +36,4 @@ interface SearchViewHolder {
searchView?.unBind(replacementMenuItemClickListener)
searchView = null
}
-
-} \ No newline at end of file
+}
diff --git a/searchview/src/main/res/layout/kau_search_iitem.xml b/searchview/src/main/res/layout/kau_search_iitem.xml
index 16df945..53e33ef 100644
--- a/searchview/src/main/res/layout/kau_search_iitem.xml
+++ b/searchview/src/main/res/layout/kau_search_iitem.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
-<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@id/kau_search_item_frame"
android:layout_width="match_parent"
@@ -58,4 +58,4 @@
app:layout_constraintStart_toEndOf="@id/kau_search_icon"
app:layout_constraintTop_toBottomOf="@id/kau_search_title" />
-</android.support.constraint.ConstraintLayout> \ No newline at end of file
+</androidx.constraintlayout.widget.ConstraintLayout> \ No newline at end of file
diff --git a/searchview/src/main/res/layout/kau_search_view.xml b/searchview/src/main/res/layout/kau_search_view.xml
index c975dad..cfcb2ed 100644
--- a/searchview/src/main/res/layout/kau_search_view.xml
+++ b/searchview/src/main/res/layout/kau_search_view.xml
@@ -43,7 +43,7 @@
android:focusable="true"
android:scaleType="centerInside" />
- <android.support.v7.widget.AppCompatEditText
+ <androidx.appcompat.widget.AppCompatEditText
android:id="@id/kau_search_edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
@@ -98,7 +98,7 @@
android:layout_height="@dimen/kau_search_divider"
android:background="?android:attr/listDivider" />
- <android.support.v7.widget.RecyclerView
+ <androidx.recyclerview.widget.RecyclerView
android:id="@id/kau_search_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"