-
Notifications
You must be signed in to change notification settings - Fork 10
/
ConfigDataAdapter.kt
95 lines (80 loc) · 3.32 KB
/
ConfigDataAdapter.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
package kaist.iclab.abclogger.ui.config
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import kaist.iclab.abclogger.R
import kaist.iclab.abclogger.databinding.ItemConfigHeaderBinding
import kaist.iclab.abclogger.databinding.ItemConfigItemBinding
import kaist.iclab.abclogger.structure.config.Config
import kaist.iclab.abclogger.structure.config.ConfigData
import kaist.iclab.abclogger.structure.config.ConfigHeader
import kaist.iclab.abclogger.structure.config.ConfigItem
class ConfigDataAdapter : RecyclerView.Adapter<ConfigDataAdapter.ViewHolder>() {
var config: Config = Config()
set(value) {
field = value
notifyDataSetChanged()
}
interface OnItemClickListener {
fun onItemClick(position: Int, item: ConfigItem<*>)
}
private var onItemClickListener: OnItemClickListener? = null
fun setOnItemClickListener(listener: OnItemClickListener) {
onItemClickListener = listener
}
fun setOnItemClickListener(block: ((position: Int, item: ConfigItem<*>) -> Unit)) {
onItemClickListener = object : OnItemClickListener {
override fun onItemClick(position: Int, item: ConfigItem<*>) {
block.invoke(position, item)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
when (viewType) {
VIEW_TYPE_ITEM -> ConfigItemViewHolder(
DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_config_item, parent, false),
onItemClickListener
)
else -> ConfigHeaderViewHolder(
DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_config_header, parent, false)
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = config.getOrNull(position) ?: return
holder.onBind(position, item)
}
override fun getItemViewType(position: Int): Int =
when (config.getOrNull(position)) {
is ConfigHeader -> VIEW_TYPE_HEADER
else -> VIEW_TYPE_ITEM
}
override fun getItemCount(): Int = config.size
abstract class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
abstract fun onBind(position: Int, item: ConfigData)
}
data class ConfigHeaderViewHolder(private val binding: ItemConfigHeaderBinding) : ViewHolder(binding.root) {
override fun onBind(position: Int, item: ConfigData) {
if (item !is ConfigHeader) return
binding.item = item
}
}
data class ConfigItemViewHolder(
private val binding: ItemConfigItemBinding,
private val listener: OnItemClickListener?
): ViewHolder(binding.root) {
@Suppress("UNCHECKED_CAST")
override fun onBind(position: Int, item: ConfigData) {
if (item !is ConfigItem<*>) return
binding.item = item as ConfigItem<Any>
binding.root.setOnClickListener {
listener?.onItemClick(position, item)
}
}
}
companion object {
private const val VIEW_TYPE_HEADER = 0x0
private const val VIEW_TYPE_ITEM = 0x1
}
}