Skip to content

Commit 744eb52

Browse files
Fix a potential contact caching issue (#1376)
1 parent 3ddf66e commit 744eb52

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationAdapter.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import org.thoughtcrime.securesms.database.CursorRecyclerViewAdapter
3131
import org.thoughtcrime.securesms.database.model.MessageId
3232
import org.thoughtcrime.securesms.database.model.MessageRecord
3333
import org.thoughtcrime.securesms.dependencies.DatabaseComponent
34+
import java.util.concurrent.ConcurrentHashMap
3435
import kotlin.math.max
3536

3637
class ConversationAdapter(
@@ -56,8 +57,8 @@ class ConversationAdapter(
5657
var visibleMessageViewDelegate: VisibleMessageViewDelegate? = null
5758

5859
private val updateQueue = Channel<String>(1024, onBufferOverflow = BufferOverflow.DROP_OLDEST)
59-
private val contactCache = SparseArray<Contact>(100)
60-
private val contactLoadedCache = SparseBooleanArray(100)
60+
private val contactCache = ConcurrentHashMap<String, Contact>(100)
61+
private val contactLoadedCache = ConcurrentHashMap<String, Boolean>(100)
6162
private val lastSeen = AtomicLong(originalLastSeen)
6263

6364
var lastSentMessageId: MessageId? = null
@@ -79,8 +80,8 @@ class ConversationAdapter(
7980
while (isActive) {
8081
val item = updateQueue.receive()
8182
val contact = getSenderInfo(item) ?: continue
82-
contactCache[item.hashCode()] = contact
83-
contactLoadedCache[item.hashCode()] = true
83+
contactCache[item] = contact
84+
contactLoadedCache[item] = true
8485
}
8586
}
8687
}
@@ -131,18 +132,17 @@ class ConversationAdapter(
131132
visibleMessageView.snIsSelected = isSelected
132133
visibleMessageView.indexInAdapter = position
133134
val senderId = message.individualRecipient.address.toString()
134-
val senderIdHash = senderId.hashCode()
135135
updateQueue.trySend(senderId)
136-
if (contactCache[senderIdHash] == null && !contactLoadedCache.getOrDefault(
137-
senderIdHash,
136+
if (contactCache[senderId] == null && !contactLoadedCache.getOrDefault(
137+
senderId,
138138
false
139139
)
140140
) {
141141
getSenderInfo(senderId)?.let { contact ->
142-
contactCache[senderIdHash] = contact
142+
contactCache[senderId] = contact
143143
}
144144
}
145-
val contact = contactCache[senderIdHash]
145+
val contact = contactCache[senderId]
146146
val isExpanded = expandedMessageIds.contains(message.messageId)
147147

148148
visibleMessageView.bind(

0 commit comments

Comments
 (0)