forked from upstream-mirrors/quik
Use fewer viewTypes in MessagesAdapter
This commit is contained in:
@@ -56,7 +56,8 @@ class MessagesAdapter @Inject constructor(
|
|||||||
) : RealmRecyclerViewAdapter<Message, QkViewHolder>(null, true) {
|
) : RealmRecyclerViewAdapter<Message, QkViewHolder>(null, true) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val VIEW_TYPE_ME = 1
|
private const val VIEW_TYPE_MESSAGE_IN = 0
|
||||||
|
private const val VIEW_TYPE_MESSAGE_OUT = 1
|
||||||
private const val TIMESTAMP_THRESHOLD = 10
|
private const val TIMESTAMP_THRESHOLD = 10
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +69,6 @@ class MessagesAdapter @Inject constructor(
|
|||||||
if (field === value) return
|
if (field === value) return
|
||||||
|
|
||||||
field = value
|
field = value
|
||||||
people.clear()
|
|
||||||
contactMap.clear()
|
contactMap.clear()
|
||||||
|
|
||||||
// Update the theme
|
// Update the theme
|
||||||
@@ -79,7 +79,6 @@ class MessagesAdapter @Inject constructor(
|
|||||||
updateData(value?.second)
|
updateData(value?.second)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val people = ArrayList<String>()
|
|
||||||
private val contactMap = HashMap<String, Contact>()
|
private val contactMap = HashMap<String, Contact>()
|
||||||
private val selected = HashMap<Long, Boolean>()
|
private val selected = HashMap<Long, Boolean>()
|
||||||
private val disposables = CompositeDisposable()
|
private val disposables = CompositeDisposable()
|
||||||
@@ -97,37 +96,19 @@ class MessagesAdapter @Inject constructor(
|
|||||||
val layoutInflater = LayoutInflater.from(context)
|
val layoutInflater = LayoutInflater.from(context)
|
||||||
val view: View
|
val view: View
|
||||||
|
|
||||||
val hasThumbnail = viewType < 0
|
if (viewType == VIEW_TYPE_MESSAGE_OUT) {
|
||||||
val absViewType = Math.abs(viewType)
|
|
||||||
|
|
||||||
if (absViewType == VIEW_TYPE_ME) {
|
|
||||||
view = layoutInflater.inflate(R.layout.message_list_item_out, parent, false)
|
view = layoutInflater.inflate(R.layout.message_list_item_out, parent, false)
|
||||||
disposables += colors.bubble
|
disposables += colors.bubble
|
||||||
.subscribe { color -> view.messageBackground.setBackgroundTint(color) }
|
.subscribe { color -> view.messageBackground.setBackgroundTint(color) }
|
||||||
} else {
|
} else {
|
||||||
view = layoutInflater.inflate(R.layout.message_list_item_in, parent, false)
|
view = layoutInflater.inflate(R.layout.message_list_item_in, parent, false)
|
||||||
|
view.avatar.threadId = data?.first?.id ?: 0
|
||||||
view.subject.textColorObservable = textPrimaryOnTheme
|
view.subject.textColorObservable = textPrimaryOnTheme
|
||||||
view.body.textColorObservable = textPrimaryOnTheme
|
view.body.textColorObservable = textPrimaryOnTheme
|
||||||
disposables += theme
|
disposables += theme
|
||||||
.subscribe { color -> view.messageBackground.setBackgroundTint(color) }
|
.subscribe { color -> view.messageBackground.setBackgroundTint(color) }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasThumbnail) {
|
|
||||||
view.messageBackground.clipToOutline = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if (absViewType != VIEW_TYPE_ME) {
|
|
||||||
val address = people[absViewType - 2]
|
|
||||||
if (!contactMap.containsKey(address)) {
|
|
||||||
contactMap[address] = data?.first?.recipients?.mapNotNull { it.contact } // Map the conversation to its contacts
|
|
||||||
?.firstOrNull { it.numbers.any { PhoneNumberUtils.compare(it.address, address) } } // See if any of the phone numbers match
|
|
||||||
?: Contact(numbers = RealmList(PhoneNumber(address = address))) // Fallback to a fake contact
|
|
||||||
}
|
|
||||||
|
|
||||||
view.avatar.threadId = data?.first?.id ?: 0
|
|
||||||
view.avatar.contact = contactMap[address]
|
|
||||||
}
|
|
||||||
|
|
||||||
return QkViewHolder(view)
|
return QkViewHolder(view)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,14 +136,33 @@ class MessagesAdapter @Inject constructor(
|
|||||||
|
|
||||||
view.timestamp.text = dateFormatter.getMessageTimestamp(message.date)
|
view.timestamp.text = dateFormatter.getMessageTimestamp(message.date)
|
||||||
|
|
||||||
|
bindAvatar(view, position)
|
||||||
bindMmsPreview(view, position)
|
bindMmsPreview(view, position)
|
||||||
bindStatus(view, position)
|
bindStatus(view, position)
|
||||||
bindGrouping(view, position)
|
bindGrouping(view, position)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun bindAvatar(view: View, position: Int) {
|
||||||
|
val message = getItem(position)!!
|
||||||
|
if (message.isMe()) return
|
||||||
|
|
||||||
|
val address = message.address
|
||||||
|
if (!contactMap.containsKey(address)) {
|
||||||
|
contactMap[address] = data?.first?.recipients?.mapNotNull { it.contact } // Map the conversation to its contacts
|
||||||
|
?.firstOrNull { it.numbers.any { PhoneNumberUtils.compare(it.address, address) } } // See if any of the phone numbers match
|
||||||
|
?: Contact(numbers = RealmList(PhoneNumber(address = address))) // Fallback to a fake contact
|
||||||
|
}
|
||||||
|
|
||||||
|
view.avatar.threadId = data?.first?.id ?: 0
|
||||||
|
view.avatar.contact = contactMap[address]
|
||||||
|
}
|
||||||
|
|
||||||
private fun bindMmsPreview(view: View, position: Int) {
|
private fun bindMmsPreview(view: View, position: Int) {
|
||||||
val message = getItem(position)!!
|
val message = getItem(position)!!
|
||||||
view.mmsPreview.parts = message.parts
|
view.mmsPreview.parts = message.parts
|
||||||
|
|
||||||
|
// If we're showing any thumbnails, set clipToOutline to true
|
||||||
|
view.messageBackground.clipToOutline = message.parts.filter { it.isImage() }.any()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun bindStatus(view: View, position: Int) {
|
private fun bindStatus(view: View, position: Int) {
|
||||||
@@ -238,21 +238,9 @@ class MessagesAdapter @Inject constructor(
|
|||||||
|
|
||||||
override fun getItemViewType(position: Int): Int {
|
override fun getItemViewType(position: Int): Int {
|
||||||
val message = getItem(position)!!
|
val message = getItem(position)!!
|
||||||
|
return when (message.isMe()) {
|
||||||
var index = if (message.isMe()) {
|
true -> VIEW_TYPE_MESSAGE_OUT
|
||||||
VIEW_TYPE_ME
|
false -> VIEW_TYPE_MESSAGE_IN
|
||||||
} else {
|
|
||||||
if (!people.contains(message.address)) {
|
|
||||||
people.add(message.address)
|
|
||||||
}
|
|
||||||
2 + people.indexOf(message.address)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it contains a thumbnail, then use the negative viewtype
|
|
||||||
if (message.parts.filter { it.isImage() }.any()) {
|
|
||||||
index *= -1
|
|
||||||
}
|
|
||||||
|
|
||||||
return index
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user