From c09b59f09a24d79ff306aea7dd86e6ca2b2c3208 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 26 Jun 2017 14:37:18 -0700 Subject: Improve search item layout --- .../ca/allanwang/kau/searchview/SearchItem.kt | 3 +- .../ca/allanwang/kau/searchview/SearchView.kt | 53 ++++++++++++++-------- .../kotlin/ca/allanwang/kau/utils/ActivityUtils.kt | 2 +- 3 files changed, 37 insertions(+), 21 deletions(-) (limited to 'library/src/main/kotlin') diff --git a/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt b/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt index 6fea9b0..3882a06 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt @@ -1,6 +1,7 @@ package ca.allanwang.kau.searchview import android.graphics.drawable.Drawable +import android.support.constraint.ConstraintLayout import android.support.v7.widget.RecyclerView import android.view.View import android.widget.ImageView @@ -61,6 +62,6 @@ class SearchItem(val key: String, val icon: ImageView by bindView(R.id.search_icon) val title: TextView by bindView(R.id.search_title) val desc: TextView by bindView(R.id.search_desc) - val container: LinearLayout by bindView(R.id.search_item_frame) + val container: ConstraintLayout by bindView(R.id.search_item_frame) } } \ No newline at end of file diff --git a/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt b/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt index 46bce6a..bfeed52 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt @@ -2,6 +2,7 @@ package ca.allanwang.kau.searchview 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.annotation.StringRes @@ -37,16 +38,18 @@ class SearchView @JvmOverloads constructor( ) : FrameLayout(context, attrs, defStyleAttr) { inner class Configs { - var foregroundColor: Int = SearchItem.foregroundColor + var foregroundColor: Int + get() = SearchItem.foregroundColor set(value) { - if (field == value) return - field = value + if (SearchItem.foregroundColor == value) return + SearchItem.foregroundColor = value tintForeground(value) } - var backgroundColor: Int = SearchItem.backgroundColor + var backgroundColor: Int + get() = SearchItem.backgroundColor set(value) { - if (field == value) return - field = value + if (SearchItem.backgroundColor == value) return + SearchItem.backgroundColor = value tintBackground(value) } var navIcon: IIcon? = GoogleMaterial.Icon.gmd_arrow_back @@ -137,16 +140,15 @@ class SearchView @JvmOverloads constructor( cardTransition() adapter.setNewList( if (configs.noResultsFound > 0 && value.isEmpty()) - listOf(SearchItem("", context.string(configs.noResultsFound), null)) + listOf(SearchItem("", context.string(configs.noResultsFound), iicon = null)) else value) } /** * Empties the list on the UI thread - * Note that this does not include any animations - * Use results = listOf() for the animated version + * The noResults item will not be added */ - internal fun clearResults() = context.runOnUiThread { adapter.clear() } + internal fun clearResults() = context.runOnUiThread { cardTransition(); adapter.clear() } val configs = Configs() //views @@ -162,6 +164,7 @@ class SearchView @JvmOverloads constructor( private val recycler: RecyclerView by bindView(R.id.search_recycler) val adapter = FastItemAdapter() lateinit var parent: ViewGroup + var menuItem: MenuItem? = null val isOpen: Boolean get() = card.isVisible() @@ -223,18 +226,23 @@ class SearchView @JvmOverloads constructor( configs.config() } - fun bind(parent: ViewGroup, menu: Menu, @IdRes id: Int, config: Configs.() -> Unit = {}): SearchView { + fun bind(parent: ViewGroup, menu: Menu, @IdRes id: Int, @ColorInt menuIconColor: Int = Color.WHITE, config: Configs.() -> Unit = {}): SearchView { config(config) configs.textObserver(textEvents.filter { it.isNotBlank() }, this) this.parent = parent - val item = menu.findItem(id) - if (item.icon == null) item.icon = GoogleMaterial.Icon.gmd_search.toDrawable(context, 20) + menuItem = menu.findItem(id) + if (menuItem!!.icon == null) menuItem!!.icon = GoogleMaterial.Icon.gmd_search.toDrawable(context, 18, menuIconColor) card.gone() - item.setOnMenuItemClickListener { configureCoords(it); revealOpen(); true } + menuItem!!.setOnMenuItemClickListener { configureCoords(it); revealOpen(); true } shadow.setOnClickListener { revealClose() } return this } + fun unBind(replacementMenuItemClickListener: MenuItem.OnMenuItemClickListener? = null) { + parent.removeView(this) + menuItem?.setOnMenuItemClickListener(replacementMenuItemClickListener) + } + fun configureCoords(item: MenuItem) { val view = parent.findViewById(item.itemId) ?: return val locations = IntArray(2) @@ -255,16 +263,23 @@ class SearchView @JvmOverloads constructor( }) } - fun tintForeground(@ColorInt color: Int) { + /** + * Tint foreground attributes + * This can be done publicly through [configs], which will also save the color + */ + internal fun tintForeground(@ColorInt color: Int) { iconNav.drawable.setTint(color) iconClear.drawable.setTint(color) - SearchItem.foregroundColor = color divider.setBackgroundColor(color.adjustAlpha(0.1f)) editText.tint(color) editText.setTextColor(ColorStateList.valueOf(color)) } - fun tintBackground(@ColorInt color: Int) { + /** + * Tint background attributes + * This can be done publicly through [configs], which will also save the color + */ + internal fun tintBackground(@ColorInt color: Int) { card.setCardBackgroundColor(color) } @@ -302,10 +317,10 @@ class SearchView @JvmOverloads constructor( } } -fun ViewGroup.bindSearchView(menu: Menu, @IdRes id: Int, 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) addView(searchView) - searchView.bind(this, menu, id, config) + searchView.bind(this, menu, id, menuIconColor, config) return searchView } diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt index f712337..ae486a4 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt @@ -51,7 +51,7 @@ var Activity.statusBarColor: Int */ fun Activity.setMenuIcons(menu: Menu, @ColorInt color: Int = Color.WHITE, vararg iicons: Pair) { iicons.forEach { (id, iicon) -> - menu.findItem(id).icon = iicon.toDrawable(this, sizeDp = 20, color = color) + menu.findItem(id).icon = iicon.toDrawable(this, sizeDp = 18, color = color) } } -- cgit v1.2.3