mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-22 13:48:52 +01:00
Don't return invalid contacts in MessageAdapter contact cache
This commit is contained in:
@@ -67,7 +67,7 @@ class MessagesAdapter @Inject constructor(
|
|||||||
if (field === value) return
|
if (field === value) return
|
||||||
|
|
||||||
field = value
|
field = value
|
||||||
contactMap.clear()
|
contactCache.clear()
|
||||||
|
|
||||||
// Update the theme
|
// Update the theme
|
||||||
val threadId = value?.first?.id ?: 0
|
val threadId = value?.first?.id ?: 0
|
||||||
@@ -83,7 +83,7 @@ class MessagesAdapter @Inject constructor(
|
|||||||
private val conversation: Conversation?
|
private val conversation: Conversation?
|
||||||
get() = data?.first?.takeIf { it.isValid }
|
get() = data?.first?.takeIf { it.isValid }
|
||||||
|
|
||||||
private val contactMap = HashMap<String, Recipient?>()
|
private val contactCache = ContactCache()
|
||||||
private val selected = HashMap<Long, Boolean>()
|
private val selected = HashMap<Long, Boolean>()
|
||||||
private val disposables = CompositeDisposable()
|
private val disposables = CompositeDisposable()
|
||||||
|
|
||||||
@@ -151,14 +151,8 @@ class MessagesAdapter @Inject constructor(
|
|||||||
val message = getItem(position)!!
|
val message = getItem(position)!!
|
||||||
if (message.isMe()) return
|
if (message.isMe()) return
|
||||||
|
|
||||||
val address = message.address
|
|
||||||
if (contactMap[address]?.isValid != true) {
|
|
||||||
contactMap[address] = conversation?.recipients
|
|
||||||
?.firstOrNull { PhoneNumberUtils.compare(it.address, address) } // See if any of the phone numbers match
|
|
||||||
}
|
|
||||||
|
|
||||||
view.avatar.threadId = conversation?.id ?: 0
|
view.avatar.threadId = conversation?.id ?: 0
|
||||||
view.avatar.setContact(contactMap[address])
|
view.avatar.setContact(contactCache[message.address])
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun bindStatus(view: View, position: Int) {
|
private fun bindStatus(view: View, position: Int) {
|
||||||
@@ -172,7 +166,7 @@ class MessagesAdapter @Inject constructor(
|
|||||||
message.isSending() -> context.getString(R.string.message_status_sending)
|
message.isSending() -> context.getString(R.string.message_status_sending)
|
||||||
message.isDelivered() -> context.getString(R.string.message_status_delivered, timestamp)
|
message.isDelivered() -> context.getString(R.string.message_status_delivered, timestamp)
|
||||||
message.isFailedMessage() -> context.getString(R.string.message_status_failed)
|
message.isFailedMessage() -> context.getString(R.string.message_status_failed)
|
||||||
!message.isMe() && conversation?.recipients?.size ?: 0 > 1 -> "${contactMap[message.address]?.getDisplayName()} • $timestamp"
|
!message.isMe() && conversation?.recipients?.size ?: 0 > 1 -> "${contactCache[message.address]?.getDisplayName()} • $timestamp"
|
||||||
else -> timestamp
|
else -> timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,4 +239,20 @@ class MessagesAdapter @Inject constructor(
|
|||||||
false -> VIEW_TYPE_MESSAGE_IN
|
false -> VIEW_TYPE_MESSAGE_IN
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cache the contacts in a map by the address, because the messages we're binding don't have
|
||||||
|
* a reference to the contact.
|
||||||
|
*/
|
||||||
|
private inner class ContactCache : HashMap<String, Recipient?>() {
|
||||||
|
|
||||||
|
override fun get(key: String): Recipient? {
|
||||||
|
if (super.get(key)?.isValid != true) {
|
||||||
|
set(key, conversation?.recipients?.firstOrNull { PhoneNumberUtils.compare(it.address, key) })
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.get(key)?.takeIf { it.isValid }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user