aboutsummaryrefslogtreecommitdiff
path: root/library/src
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-06-26 14:37:18 -0700
committerAllan Wang <me@allanwang.ca>2017-06-26 14:37:18 -0700
commitc09b59f09a24d79ff306aea7dd86e6ca2b2c3208 (patch)
tree60a872c925abd99fe4f9494a9b51c3170fe6010d /library/src
parentc9302a7b0f3c66b3514f1f5b83c86757db33008a (diff)
downloadkau-c09b59f09a24d79ff306aea7dd86e6ca2b2c3208.tar.gz
kau-c09b59f09a24d79ff306aea7dd86e6ca2b2c3208.tar.bz2
kau-c09b59f09a24d79ff306aea7dd86e6ca2b2c3208.zip
Improve search item layout
Diffstat (limited to 'library/src')
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt3
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt53
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt2
-rw-r--r--library/src/main/res/layout/kau_search_item.xml14
4 files changed, 46 insertions, 26 deletions
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<SearchItem>()
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<View>(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<Int, IIcon>) {
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)
}
}
diff --git a/library/src/main/res/layout/kau_search_item.xml b/library/src/main/res/layout/kau_search_item.xml
index 4de4ed6..efd7260 100644
--- a/library/src/main/res/layout/kau_search_item.xml
+++ b/library/src/main/res/layout/kau_search_item.xml
@@ -23,7 +23,7 @@
<TextView
android:id="@+id/search_title"
android:layout_width="0dp"
- android:layout_height="0dp"
+ android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_weight="1"
android:ellipsize="end"
@@ -33,24 +33,28 @@
android:paddingStart="@dimen/kau_search_key_line_8"
android:textIsSelectable="false"
android:textSize="@dimen/kau_search_text_small"
+ app:layout_constraintBottom_toTopOf="@+id/search_desc"
+ app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/search_icon"
- app:layout_constraintTop_toTopOf="parent" />
+ app:layout_constraintTop_toTopOf="parent"
+ app:layout_constraintVertical_bias="0.5" />
<TextView
- android:id="@+id/search_desc"
+ android:id="@id/search_desc"
android:layout_width="0dp"
- android:layout_height="0dp"
+ android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_weight="1"
android:ellipsize="end"
- android:visibility="gone"
android:gravity="start|center_vertical"
android:maxLines="1"
android:paddingEnd="@dimen/kau_search_key_line_16"
android:paddingStart="@dimen/kau_search_key_line_8"
android:textIsSelectable="false"
android:textSize="@dimen/kau_search_text_micro"
+ android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/search_icon"
app:layout_constraintTop_toBottomOf="@id/search_title" />