1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
package ca.allanwang.kau.searchview
import android.content.Context
import android.support.annotation.ColorInt
import android.support.annotation.IdRes
import android.support.v7.widget.AppCompatEditText
import android.support.v7.widget.CardView
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.ProgressBar
import ca.allanwang.kau.R
import ca.allanwang.kau.logging.KL
import ca.allanwang.kau.utils.*
import com.mikepenz.fastadapter.commons.adapters.FastItemAdapter
import com.mikepenz.google_material_typeface_library.GoogleMaterial
import com.mikepenz.iconics.typeface.IIcon
/**
* Created by Allan Wang on 2017-06-23.
*/
class SearchView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
companion object {
fun bind(parent: ViewGroup, menu: Menu, @IdRes id: Int, config: Configs.() -> Unit = {}) {
SearchView(parent.context).apply {
layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
parent.addView(this)
this.bind(parent, menu, id, config)
}
}
}
//configs
inner class Configs {
var foregroundColor: Int = 0xddffffff.toInt()
set(value) {
if (field == value) return
field = value
tintForeground(value)
}
var backgroundColor: Int = 0xfffafafa.toInt()
set(value) {
if (field == value) return
field = value
tintBackground(value)
}
var navIcon: IIcon? = GoogleMaterial.Icon.gmd_arrow_back
set(value) {
field = value
iconNav.setIcon(value)
if (value == null) iconNav.gone()
}
var micIcon: IIcon? = GoogleMaterial.Icon.gmd_mic
set(value) {
field = value
iconMic.setIcon(value)
if (value == null) iconMic.gone()
}
var clearIcon: IIcon? = GoogleMaterial.Icon.gmd_clear
set(value) {
field = value
iconClear.setIcon(value)
if (value == null) iconClear.gone()
}
var revealDuration: Long = 300L
var shouldClearOnOpen: Boolean = true
var openListener: (() -> Unit)? = null
var closeListener: (() -> Unit)? = null
}
val configs = Configs()
//views
private val shadow: View by bindView(R.id.search_shadow)
private val card: CardView by bindView(R.id.search_cardview)
private val iconNav: ImageView by bindView(R.id.search_nav)
private val editText: AppCompatEditText by bindView(R.id.search_edit_text)
private val progress: ProgressBar by bindView(R.id.search_progress)
private val iconMic: ImageView by bindView(R.id.search_mic)
private val iconClear: ImageView by bindView(R.id.search_clear)
private val recycler: RecyclerView by bindView(R.id.search_recycler)
private val adapter = FastItemAdapter<SearchItem>()
private lateinit var parent: ViewGroup
val isOpen: Boolean
get() = card.isVisible()
//menu view
private var revealX: Int = -1
private var revealY: Int = -1
init {
View.inflate(context, R.layout.kau_search_view, this)
iconNav.setIcon(configs.navIcon)
iconMic.setIcon(configs.micIcon)
iconClear.setIcon(configs.clearIcon)
tintForeground(configs.foregroundColor)
tintBackground(configs.backgroundColor)
with(recycler) {
layoutManager = LinearLayoutManager(context)
addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) hideKeyboard()
}
})
adapter = this@SearchView.adapter
}
}
internal fun ImageView.setSearchIcon(iicon: IIcon) {
setIcon(iicon, sizeDp = 20, color = configs.foregroundColor)
}
fun config(config: Configs.() -> Unit) {
configs.config()
}
fun bind(parent: ViewGroup, menu: Menu, @IdRes id: Int, config: Configs.() -> Unit = {}) {
config(config)
this.parent = parent
val item = menu.findItem(id)
if (item.icon == null) item.icon = GoogleMaterial.Icon.gmd_search.toDrawable(context, 20)
card.gone()
item.setOnMenuItemClickListener { configureCoords(it); revealOpen(); true }
shadow.setOnClickListener { revealClose() }
}
fun configureCoords(item: MenuItem) {
val view = parent.findViewById<View>(item.itemId) ?: return
val locations = IntArray(2)
view.getLocationOnScreen(locations)
revealX = (locations[0] + view.width / 2)
revealY = (locations[1] + view.height / 2)
val topAlignment = revealY - card.height / 2
val params = (card.layoutParams as MarginLayoutParams).apply {
topMargin = topAlignment
}
card.layoutParams = params
}
fun tintForeground(@ColorInt color: Int) {
iconNav.drawable.setTint(color)
iconMic.drawable.setTint(color)
iconClear.drawable.setTint(color)
SearchItem.foregroundColor = color
}
fun tintBackground(@ColorInt color: Int) {
card.setCardBackgroundColor(color)
}
fun revealOpen() {
if (isOpen) return
card.circularReveal(revealX, revealY, duration = configs.revealDuration,
onStart = {
configs.openListener?.invoke()
if (configs.shouldClearOnOpen) editText.text.clear()
},
onFinish = {
editText.requestFocus()
shadow.fadeIn()
})
}
fun revealClose() {
if (!isOpen) return
shadow.fadeOut() {
card.circularHide(revealX, revealY, duration = configs.revealDuration,
onFinish = {
configs.closeListener?.invoke()
})
}
}
}
|