-
Notifications
You must be signed in to change notification settings - Fork 18
/
SheetMenu.kt
231 lines (202 loc) · 7.15 KB
/
SheetMenu.kt
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
package ru.whalemare.sheetmenu
import android.content.Context
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import ru.whalemare.sheetmenu.adapter.MenuAdapter
import ru.whalemare.sheetmenu.extension.inflate
import ru.whalemare.sheetmenu.extension.marginTop
import ru.whalemare.sheetmenu.extension.toList
/**
* Developed with ❤
* @since 2017
* @author Anton Vlasov - whalemare
* @param titleId id from resources with text for title. it is more important than common <b>title</b> param
* @param title string with text for title
* @param menu id from resources for auto-inflate menu
* @param layoutManager for RecyclerView. By default: vertical linear layout manager. If you set this, @param <b>mode</b> is not be used.
* @param click listener for menu items
*/
open class SheetMenu(
var titleId: Int = 0,
var title: String? = "",
var menu: Int = 0,
var layoutManager: RecyclerView.LayoutManager? = null,
var adapter: MenuAdapter? = null,
var click: MenuItem.OnMenuItemClickListener = MenuItem.OnMenuItemClickListener { false },
var autoCancel: Boolean = true,
var showIcons: Boolean = true
) {
private var dialog: BottomSheetDialog? = null
fun show(context: Context) {
val root = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_horizontal_list, null)
val textTitle = root.findViewById(R.id.text_title) as TextView
processTitle(textTitle)
val dialog = BottomSheetDialog(context).also {
it.setContentView(root)
processGrid(root)
}
this.dialog = dialog
val recycler = root.findViewById(R.id.recycler_view) as RecyclerView
processRecycler(recycler, dialog)
root.viewTreeObserver.addOnGlobalLayoutListener {
val bottomSheet = dialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
val behavior = BottomSheetBehavior.from(bottomSheet)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.peekHeight = 0
}
dialog.show()
}
fun dismiss() {
dialog?.dismiss()
dialog = null
}
protected open fun processGrid(root: View) {
if (root.findViewById<View>(R.id.text_title).visibility != View.VISIBLE) {
if (layoutManager is GridLayoutManager) {
root.marginTop(24)
}
}
}
protected open fun processTitle(textTitle: TextView) {
if (titleId > 0) {
textTitle.setText(titleId)
textTitle.visibility = View.VISIBLE
} else if (!title.isNullOrBlank()) {
textTitle.text = title
textTitle.visibility = View.VISIBLE
} else {
textTitle.visibility = View.GONE
}
}
protected open fun processRecycler(recycler: RecyclerView, dialog: BottomSheetDialog) {
if (menu > 0) {
if (layoutManager == null) {
layoutManager = LinearLayoutManager(recycler.context, RecyclerView.VERTICAL, false)
}
var itemLayoutId = R.layout.item_linear
if (layoutManager is GridLayoutManager) {
itemLayoutId = R.layout.item_grid
}
if (adapter == null) {
adapter = MenuAdapter(
menuItems = recycler.context.inflate(menu).toList(),
callback = MenuItem.OnMenuItemClickListener {
click.onMenuItemClick(it)
if (autoCancel) dialog.cancel()
true
},
itemLayoutId = itemLayoutId,
showIcons = showIcons
)
}
recycler.adapter = adapter
recycler.layoutManager = layoutManager
}
}
protected open fun processClick(dialog: BottomSheetDialog): MenuItem.OnMenuItemClickListener {
return if (autoCancel) {
MenuItem.OnMenuItemClickListener {
click.onMenuItemClick(it)
dialog.cancel()
true
}
} else {
click
}
}
//region java interop
companion object {
@JvmStatic
fun with(context: Context): SheetMenu.Builder {
return SheetMenu.Builder(context)
}
}
class Builder(private val context: Context) {
private var title = ""
private var menu: Int = 0
private var layoutManager: RecyclerView.LayoutManager? = null
private var adapter: MenuAdapter? = null
private var click: MenuItem.OnMenuItemClickListener = MenuItem.OnMenuItemClickListener { false }
private var autoCancel: Boolean = true
private var showIcons: Boolean = true
fun show() {
SheetMenu(
0,
title,
menu,
layoutManager,
null,
click,
autoCancel,
showIcons
).show(context)
}
/**
* @param titleId id from resources with text for title. it is more important than common <b>title</b> param
*/
fun setTitle(titleId: Int): Builder {
this.title = context.getString(titleId)
return this
}
/**
* @param title string with text for title
*/
fun setTitle(text: String): Builder {
this.title = text
return this
}
/**
* @param menu id from resources for auto-inflate menu
*/
fun setMenu(menu: Int): Builder {
this.menu = menu
return this
}
/**
* @param click listener for menu items
*/
fun setClick(click: MenuItem.OnMenuItemClickListener): Builder {
this.click = click
return this
}
/**
* @param layoutManager for RecyclerView. By default: vertical linear layout manager.
*/
fun setLayoutManager(layoutManager: RecyclerView.LayoutManager?): Builder {
this.layoutManager = layoutManager
return this
}
/**
* @param adapter for RecyclerView.
* @see MenuAdapter by default
*/
fun setAdapter(adapter: MenuAdapter): Builder {
this.adapter = adapter
return this
}
fun setAutoCancel(autoCancel: Boolean): Builder {
this.autoCancel = autoCancel
return this
}
fun showIcons(showIcons: Boolean): Builder {
this.showIcons = showIcons
return this
}
}
fun apply(action: ActionSingle<SheetMenu>): SheetMenu {
action.call(this)
return this
}
interface ActionSingle<in T> {
fun call(it: T)
}
//endregion
}