aboutsummaryrefslogtreecommitdiff
path: root/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt
blob: d223ec43ebe91d6abf73805b671e4da4b05b5f2b (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
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.inputmethod.EditorInfo
import android.widget.FrameLayout
import android.widget.ImageView
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 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.
 *
 * A materialized SearchView with complete theming and customization
 * This view can be added programmatically and configured using the [Configs] DSL
 * It is preferred to add the view through an activity, but it can be attached to any ViewGroup
 * Beware of where specifically this is added, as its view or the keyboard may affect positioning
 *
 * Huge thanks to @lapism for his base
 * https://github.com/lapism/SearchView
 */
class SearchView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    /**
     * Collection of all possible arguments when building the SearchView
     * Everything is made as opened as possible, so additional components may be found in the [SearchView]
     * However, these are the main config options
     */
    class Configs {

        /**
         * The foreground color accounts for all text colors and icon colors
         * Various alpha levels may be used for sub texts/dividers etc
         */
        var foregroundColor: Int = SearchItem.foregroundColor

        /**
         * Namely the background for the card and recycler view
         */
        var backgroundColor: Int = SearchItem.backgroundColor

        /**
         * Icon for the leftmost ImageView, which typically contains the hamburger menu/back arror
         */
        var navIcon: IIcon? = GoogleMaterial.Icon.gmd_arrow_back

        /**
         * Optional icon just to the left of the clear icon
         * This is not implemented by default, but can be used for anything, such as mic or redirects
         * Returns the extra imageview
         * Set the iicon as null to hide the extra icon
         */
        var extraIcon: Pair<IIcon, OnClickListener>? = null

        /**
         * Icon for the rightmost ImageView, which typically contains a close icon
         */
        var clearIcon: IIcon? = GoogleMaterial.Icon.gmd_clear

        /**
         * Duration for the circular reveal animation
         */
        var revealDuration: Long = 300L

        /**
         * Duration for the auto transition, which is namely used to resize the recycler view
         */
        var transitionDuration: Long = 100L

        /**
         * Defines whether the edit text and mainAdapter should clear themselves when the searchView is closed
         */
        var shouldClearOnClose: Boolean = false

        /**
         * Callback that will be called every time the searchView opens
         */
        var openListener: ((searchView: SearchView) -> Unit)? = null

        /**
         * Callback that will be called every time the searchView closes
         */
        var closeListener: ((searchView: SearchView) -> Unit)? = null

        /**
         * Draw a divider between the search bar and the suggestion items
         * The divider is colored based on the [foregroundColor]
         */
        var withDivider: Boolean = true

        /**
         * Hint string to be set in the searchView
         */
        var hintText: String? = null

        /**
         * Hint string res to be set in the searchView
         */
        var hintTextRes: Int = INVALID_ID

        /**
         * StringRes for a "no results found" item
         * If [results] is ever set to an empty list, it will default to
         * a list with one item with this string
         *
         * For simplicity, kau contains [R.string.kau_no_results_found]
         * which you may use
         */
        var noResultsFound: Int = INVALID_ID

        /**
         * Callback for when the query changes
         * This callback does not run on the ui thread!
         * It is always on a worker thread, so there is no need for asynchronous calls
         * Likewise, calls modifying the UI should be passed through [runOnUiThread]
         */
        var textCallback: (query: String, searchView: SearchView) -> Unit = { _, _ -> }

        /**
         * Callback for when the query is changed to an empty string
         * Typically, this may be ignored as the adapter will simply be cleared,
         * but if we wish to do something else, we may pass a function
         * Returns [true] if the action was consumed, [false] otherwise (to execute default behaviour)
         */
        var textClearedCallback: (searchView: SearchView) -> Boolean = { _ -> false }

        /**
         * Callback for when the search action key is detected from the keyboard
         * Returns true if the searchbar should close afterwards, and false otherwise
         */
        var searchCallback: (query: String, searchView: SearchView) -> Boolean = { _, _ -> false }

        /**
         * Debouncing interval between callbacks
         */
        var textDebounceInterval: Long = 0

        /**
         * 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 = { _, _, _, _ -> }

        /**
         * 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 = { _, _, _, _ -> }

        /**
         * If a [SearchItem]'s title contains the submitted query, make that portion bold
         * See [SearchItem.withHighlights]
         */
        var highlightQueryText: Boolean = true

        /**
         * Sets config attributes to the given searchView
         */
        internal fun apply(searchView: SearchView) {
            with(searchView) {
                if (SearchItem.foregroundColor != foregroundColor) {
                    SearchItem.foregroundColor = foregroundColor
                    tintForeground(foregroundColor)
                }
                if (SearchItem.backgroundColor != backgroundColor) {
                    SearchItem.backgroundColor = backgroundColor
                    tintBackground(backgroundColor)
                }
                val icons = mutableListOf(navIcon to kau_search_nav, clearIcon to kau_search_clear)
                val extra = extraIcon
                if (extra != null) icons.add(extra.first to kau_search_extra)
                icons.forEach { (iicon, view) -> view.goneIf(iicon == null).setSearchIcon(iicon) }

                if (extra != null) kau_search_extra.setOnClickListener(extra.second)
                kau_search_divider.invisibleIf(!withDivider)
                kau_search_edit_text.hint = context.string(hintTextRes, hintText)
                textCallback.terminate()
                textCallback = debounce(textDebounceInterval, this@Configs.textCallback)
            }
        }
    }

    /**
     * Contract for mainAdapter items
     * Setting results will ensure that the values are sent on the UI thread
     */
    var results: List<SearchItem>
        get() = adapter.adapterItems
        set(value) = context.runOnUiThread {
            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(kau_search_edit_text.text?.toString()) }
            cardTransition()
            adapter.setNewList(list)
        }

    /**
     * Empties the list on the UI thread
     * The noResults item will not be added
     */
    internal fun clearResults() {
        textCallback.cancel()
        context.runOnUiThread { cardTransition(); adapter.clear() }
    }

    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 val adapter = FastItemAdapter<SearchItem>()
    private var menuItem: MenuItem? = null
    val isOpen: Boolean
        get() = parent != null && kau_search_cardview.isVisible

    /**
     * The current text located in our searchview
     */
    val query: String
        get() = kau_search_edit_text.text?.toString()?.trim() ?: ""

    /*
     * Ripple start points and search view offset
     * These are calculated every time the search view is opened,
     * and can be overridden with the open listener if necessary
     */
    var menuX: Int = -1             //starting x for circular reveal
    var menuY: Int = -1             //reference for cardview's marginTop
    var menuHalfHeight: Int = -1    //starting y for circular reveal (relative to the cardview)

    init {
        View.inflate(context, R.layout.kau_search_view, this)
        z = 99f
        kau_search_nav.setSearchIcon(configs.navIcon).setOnClickListener { revealClose() }
        kau_search_clear.setSearchIcon(configs.clearIcon).setOnClickListener { kau_search_edit_text.text?.clear() }
        tintForeground(configs.foregroundColor)
        tintBackground(configs.backgroundColor)
        with(kau_search_recycler) {
            isNestedScrollingEnabled = false
            withLinearAdapter(this@SearchView.adapter)
            addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    super.onScrollStateChanged(recyclerView, newState)
                    if (newState == RecyclerView.SCROLL_STATE_DRAGGING) hideKeyboard()
                }
            })
            itemAnimator = null
        }
        with(adapter) {
            withSelectable(true)
            withOnClickListener { _, _, item, position ->
                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
            }
        }
        kau_search_edit_text.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {}

            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                val text = s.toString().trim()
                textCallback.cancel()
                if (text.isNotEmpty())
                    textCallback(text, this@SearchView)
                else if (!configs.textClearedCallback(this@SearchView))
                    clearResults()
            }
        })
        kau_search_edit_text.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                if (configs.searchCallback(kau_search_edit_text.text?.toString()
                                ?: "", this)) revealClose()
                else kau_search_edit_text.hideKeyboard()
                return@setOnEditorActionListener true
            }
            false
        }
    }

    internal fun ImageView.setSearchIcon(iicon: IIcon?): ImageView {
        setIcon(iicon, sizeDp = 18, color = configs.foregroundColor)
        return this
    }

    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()
                })
    }

    /**
     * Update the base configurations and apply them to the searchView
     */
    fun config(config: Configs.() -> Unit) {
        configs.config()
        configs.apply(this)
    }

    /**
     * Binds the SearchView to a menu item and handles everything internally
     * 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 {
        config(config)
        val menuItem = menu.findItem(id)
                ?: 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 }
        kau_search_shadow.setOnClickListener { revealClose() }
        this.menuItem = menuItem
        return this
    }

    /**
     * Call to remove the searchView from the original menuItem,
     * with the option to replace the item click listener
     */
    fun unBind(replacementMenuItemClickListener: ((item: MenuItem) -> Boolean)? = null) {
        (parent as? ViewGroup)?.removeView(this)
        menuItem?.setOnMenuItemClickListener(replacementMenuItemClickListener)
        menuItem = null
    }

    private val locations = IntArray(2)

    private fun configureCoords(item: MenuItem?) {
        item ?: return
        if (parent !is ViewGroup) return
        val view = parentViewGroup.findViewById<View>(item.itemId) ?: return
        view.getLocationInWindow(locations)
        menuX = (locations[0] + view.width / 2)
        menuHalfHeight = view.height / 2
        menuY = (locations[1] + menuHalfHeight)
        kau_search_cardview.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
            override fun onPreDraw(): Boolean {
                view.viewTreeObserver.removeOnPreDrawListener(this)
                kau_search_cardview.setMarginTop(menuY - kau_search_cardview.height / 2)
                return true
            }
        })
    }

    /**
     * Handle a back press event
     * Returns true if back press is consumed, false otherwise
     */
    fun onBackPressed(): Boolean {
        if (isOpen && menuItem != null) {
            revealClose()
            return true
        }
        return false
    }

    /**
     * Tint foreground attributes
     * This can be done publicly through [configs], which will also save the color
     */
    internal fun tintForeground(@ColorInt color: Int) {
        kau_search_nav.drawable.setTint(color)
        kau_search_clear.drawable.setTint(color)
        kau_search_divider.setBackgroundColor(color.adjustAlpha(0.1f))
        kau_search_edit_text.tint(color)
        kau_search_edit_text.setTextColor(ColorStateList.valueOf(color))
        kau_search_edit_text.setHintTextColor(color.adjustAlpha(0.7f))
    }

    /**
     * Tint background attributes
     * This can be done publicly through [configs], which will also save the color
     */
    internal fun tintBackground(@ColorInt color: Int) {
        kau_search_cardview.setCardBackgroundColor(color)
    }

    fun revealOpen() {
        if (parent == null || isOpen) return
        context.runOnUiThread {
            /**
             * The y component is relative to the cardView, but it hasn't been drawn yet so its own height is 0
             * We therefore use half the menuItem height, which is a close approximation to our intended value
             * The cardView matches the parent's width, so menuX is correct
             */
            configureCoords(menuItem)
            configs.openListener?.invoke(this@SearchView)
            kau_search_shadow.fadeIn()
            kau_search_edit_text.showKeyboard()
            kau_search_cardview.circularReveal(menuX, menuHalfHeight, duration = configs.revealDuration) {
                cardTransition()
                kau_search_recycler.visible()
            }
        }
    }

    fun revealClose() {
        if (parent == null || !isOpen) return
        context.runOnUiThread {
            kau_search_shadow.fadeOut(duration = configs.transitionDuration)
            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()
                            })
                }
            }
            kau_search_recycler.gone()
            kau_search_edit_text.hideKeyboard()
        }
    }

}

@DslMarker
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)

/**
 * Bind searchView to a menu item; call this in [Activity.onCreateOptionsMenu]
 * Be wary that if you may reinflate the menu many times (eg through [Activity.invalidateOptionsMenu]),
 * 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 {
    val searchView = SearchView(context)
    searchView.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
    addView(searchView)
    searchView.bind(menu, id, menuIconColor, config)
    return searchView
}