@@ -6,16 +6,37 @@ import android.view.LayoutInflater
66import android.view.View
77import android.view.ViewGroup
88import androidx.annotation.StyleRes
9+ import androidx.appcompat.widget.SearchView
910import androidx.fragment.app.Fragment
1011import androidx.fragment.app.FragmentActivity
1112import androidx.fragment.app.FragmentManager
13+ import com.google.android.material.bottomsheet.BottomSheetBehavior
14+ import com.google.android.material.bottomsheet.BottomSheetDialog
1215import com.google.android.material.bottomsheet.BottomSheetDialogFragment
1316import kotlinx.android.synthetic.main.dialog_sheet_selection.*
1417
1518class SheetSelection private constructor() : BottomSheetDialogFragment() {
1619
1720 var onItemClickListener: OnItemSelectedListener ? = null
1821
22+ private val adapter by lazy {
23+ SheetSelectionAdapter (
24+ source = arguments?.getParcelableArrayList(ARGS_ITEMS ) ? : emptyList(),
25+ selectedPosition = arguments?.getInt(ARGS_SELECTED_POSITION , NO_SELECT ) ? : NO_SELECT ,
26+ onItemSelectedListener = onItemSelectedListener
27+ )
28+ }
29+
30+ private val screenHeight by lazy {
31+ val statusBarHeight = try {
32+ val resourceId = resources.getIdentifier(" status_bar_height" , " dimen" , " android" )
33+ resources.getDimensionPixelSize(resourceId)
34+ } catch (e: Exception ) {
35+ 0
36+ }
37+ resources.displayMetrics.heightPixels - statusBarHeight
38+ }
39+
1940 override fun getTheme (): Int = arguments?.getInt(ARGS_THEME ) ? : super .getTheme()
2041
2142 override fun onCreateView (
@@ -36,23 +57,61 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
3657 val title = args.getString(ARGS_TITLE )
3758 if (title.isNullOrEmpty()) {
3859 textViewTitle.visibility = View .GONE
60+ textViewTitle.text = null
3961 } else {
62+ textViewTitle.visibility = View .VISIBLE
4063 textViewTitle.text = title
4164 }
4265
43- recyclerViewSelectionItems.adapter = SheetSelectionAdapter (
44- items = args.getParcelableArrayList(ARGS_ITEMS ) ? : emptyList(),
45- selectedPosition = args.getInt(ARGS_SELECTED_POSITION , NO_SELECT ),
46- onItemSelectedListener = internalOnItemSelectedListener
47- )
66+ if (args.getBoolean(ARGS_SEARCH_ENABLED )) {
67+ buttonSearch.visibility = View .VISIBLE
68+ buttonSearch.setOnClickListener(onSearchClickListener)
69+ searchView.setOnCloseListener(onSearchCloseListener)
70+ searchView.setOnQueryTextListener(onSearchQueryTextListener)
71+ }
72+
73+ recyclerViewSelectionItems.setHasFixedSize(true )
74+ recyclerViewSelectionItems.adapter = adapter
4875 }
4976 }
5077
51- private val internalOnItemSelectedListener: OnItemSelectedListener = { item, position ->
78+ private fun updateSheetHeight (viewHeight : Int ) {
79+ rootLayout.layoutParams = rootLayout.layoutParams
80+ .apply { height = viewHeight }
81+ }
82+
83+ private val onItemSelectedListener: OnItemSelectedListener = { item, position ->
5284 dismiss()
5385 onItemClickListener?.invoke(item, position)
5486 }
5587
88+ private val onSearchClickListener = View .OnClickListener {
89+ (dialog as ? BottomSheetDialog )?.run {
90+ behavior.state = BottomSheetBehavior .STATE_EXPANDED
91+ }
92+ updateSheetHeight(screenHeight)
93+ viewSwitcherHeader.displayedChild = 1
94+ searchView.isIconified = false
95+ }
96+
97+ private val onSearchCloseListener = SearchView .OnCloseListener {
98+ updateSheetHeight(ViewGroup .LayoutParams .WRAP_CONTENT )
99+ viewSwitcherHeader.displayedChild = 0
100+ true
101+ }
102+
103+ private val onSearchQueryTextListener = object : SearchView .OnQueryTextListener {
104+ override fun onQueryTextChange (newText : String? ): Boolean {
105+ adapter.search(newText)
106+ return true
107+ }
108+
109+ override fun onQueryTextSubmit (query : String? ): Boolean {
110+ adapter.search(query)
111+ return true
112+ }
113+ }
114+
56115 class Builder (context : Context ) {
57116 private val manager: FragmentManager ? = when (context) {
58117 is FragmentActivity -> context.supportFragmentManager
@@ -66,6 +125,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
66125 private var items: List <SheetSelectionItem > = emptyList()
67126 private var selectedPosition: Int = NO_SELECT
68127 private var showDraggedIndicator: Boolean = false
128+ private var searchEnabled: Boolean = false
69129 private var listener: OnItemSelectedListener ? = null
70130
71131 fun theme (@StyleRes themeId : Int ) = apply {
@@ -93,6 +153,10 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
93153 this .showDraggedIndicator = show
94154 }
95155
156+ fun searchEnabled (enabled : Boolean ) = apply {
157+ this .searchEnabled = enabled
158+ }
159+
96160 fun onItemClickListener (listener : OnItemSelectedListener ) = apply {
97161 this .listener = listener
98162 }
@@ -105,6 +169,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
105169 putParcelableArrayList(ARGS_ITEMS , ArrayList (items))
106170 putInt(ARGS_SELECTED_POSITION , selectedPosition)
107171 putBoolean(ARGS_SHOW_DRAGGED_INDICATOR , showDraggedIndicator)
172+ putBoolean(ARGS_SEARCH_ENABLED , searchEnabled)
108173 }
109174 onItemClickListener = listener
110175 }
@@ -124,5 +189,6 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
124189 private const val ARGS_ITEMS = " SheetSelection:ARGS_ITEMS"
125190 private const val ARGS_SELECTED_POSITION = " SheetSelection:ARGS_SELECTED_POSITION"
126191 private const val ARGS_SHOW_DRAGGED_INDICATOR = " SheetSelection:ARGS_SHOW_DRAGGED_INDICATOR"
192+ private const val ARGS_SEARCH_ENABLED = " SheetSelection:ARGS_SEARCH_ENABLED"
127193 }
128194}
0 commit comments