-
Notifications
You must be signed in to change notification settings - Fork 19
/
ConversationAdapter.kt
48 lines (38 loc) 路 1.74 KB
/
ConversationAdapter.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
package com.wire.android.feature.conversation.content.ui
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.wire.android.core.ui.recyclerview.ViewHolderInflater
class ConversationAdapter(private val viewHolderInflater: ViewHolderInflater) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var messages: List<Any> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =
ConversationTextMessageViewHolder(parent, viewHolderInflater)
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (getItemViewType(position) == VIEW_TYPE_TEXT_MESSAGE) {
val shouldShowAvatar = shouldShowAvatar(position)
(holder as ConversationTextMessageViewHolder).bind(
(messages[position] as CombinedMessageContact),
shouldShowAvatar
)
}
}
override fun getItemViewType(position: Int): Int {
return when (messages[position]) {
is CombinedMessageContact -> VIEW_TYPE_TEXT_MESSAGE
else -> VIEW_TYPE_UNKNOWN
}
}
override fun getItemCount(): Int = messages.size
fun setList(newItems: List<Any>) {
this.messages = newItems
notifyDataSetChanged()
}
private fun shouldShowAvatar(position: Int): Boolean {
val currentMessage = (messages[position] as CombinedMessageContact).message
return (position == 0) ||
(position > 0 && currentMessage.senderUserId != (messages[position - 1] as CombinedMessageContact).message.senderUserId)
}
companion object {
const val VIEW_TYPE_TEXT_MESSAGE = 10
const val VIEW_TYPE_UNKNOWN = -1
}
}