Skip to content
Snippets Groups Projects
Commit 844dcdfc authored by Adrien Béraud's avatar Adrien Béraud
Browse files

account: reduce contention

Change-Id: I67e22f3ebf82353ff645703badad44f49624a5b4
parent 883e04ed
Branches
No related tags found
No related merge requests found
...@@ -127,24 +127,26 @@ class Account( ...@@ -127,24 +127,26 @@ class Account(
Log.e(TAG, "conversationStarted ${conversation.accountId} ${conversation.uri} ${conversation.contacts} ${conversation.mode.blockingFirst()}", e) Log.e(TAG, "conversationStarted ${conversation.accountId} ${conversation.uri} ${conversation.contacts} ${conversation.mode.blockingFirst()}", e)
} }
} }
conversationChanged()
} }
conversationChanged()
} }
fun getSwarm(conversationId: String): Conversation? { fun getSwarm(conversationId: String): Conversation? =
synchronized(conversations) { return swarmConversations[conversationId] } synchronized(conversations) { swarmConversations[conversationId] }
}
fun newSwarm(conversationId: String, mode: Conversation.Mode): Conversation { fun newSwarm(conversationId: String, mode: Conversation.Mode): Conversation {
Log.d(TAG, "newSwarm $conversationId")
val conv: Conversation?
synchronized(conversations) { synchronized(conversations) {
var c = swarmConversations[conversationId] conv = swarmConversations[conversationId]
if (c == null) { if (conv == null) {
c = Conversation(accountId, Uri(Uri.SWARM_SCHEME, conversationId), mode) val c = Conversation(accountId, Uri(Uri.SWARM_SCHEME, conversationId), mode)
swarmConversations[conversationId] = c swarmConversations[conversationId] = c
return c
} }
c.setMode(mode)
return c
} }
conv!!.setMode(mode)
return conv
} }
fun removeSwarm(conversationId: String) { fun removeSwarm(conversationId: String) {
...@@ -153,14 +155,14 @@ class Account( ...@@ -153,14 +155,14 @@ class Account(
val conversation = swarmConversations.remove(conversationId) val conversation = swarmConversations.remove(conversationId)
if (conversation != null) { if (conversation != null) {
try { try {
val c = conversations.remove(conversation.uri.uri) val c = conversations.remove(conversation.uri.uri)!!
val contact = c!!.contact val contact = c.contact
Log.w(TAG, "removeSwarm: adding back contact conversation " + contact + " " + contact!!.conversationUri.blockingFirst() + " " + c.uri) Log.w(TAG, "removeSwarm: adding back contact conversation " + contact + " " + contact!!.conversationUri.blockingFirst() + " " + c.uri)
if (contact.conversationUri.blockingFirst().equals(c.uri)) { if (contact.conversationUri.blockingFirst() == c.uri) {
contact.setConversationUri(contact.uri) contact.setConversationUri(contact.uri)
contactAdded(contact) contactAdded(contact)
} }
} catch (ignored: Exception) { } catch (_: Exception) {
} }
conversationChanged() conversationChanged()
} }
...@@ -238,24 +240,27 @@ class Account( ...@@ -238,24 +240,27 @@ class Account(
pendingSubject.onNext(getSortedPending()) pendingSubject.onNext(getSortedPending())
} }
private fun conversationRefreshed(conversation: Conversation) { private fun conversationRefreshed(conversation: Conversation, sorted: List<Conversation>) {
if (historyLoaded) { if (historyLoaded) {
conversationSubject.onNext(conversation) conversationSubject.onNext(conversation)
updateUnreadConversations() updateUnreadConversations(sorted)
} }
} }
fun conversationChanged() { fun conversationChanged() {
val sortedList: List<Conversation>?
synchronized(conversations) { synchronized(conversations) {
conversationsChanged = true conversationsChanged = true
if (historyLoaded) { sortedList = if (historyLoaded) { ArrayList(getSortedConversations()) } else null
conversationsSubject.onNext(ArrayList(getSortedConversations())) }
updateUnreadConversations() if (sortedList != null) {
} conversationsSubject.onNext(sortedList)
updateUnreadConversations(sortedList)
} }
} }
fun conversationUpdated(conversation: Conversation) { fun conversationUpdated(conversation: Conversation) {
val sortedList: List<Conversation>
synchronized(conversations) { synchronized(conversations) {
if (!historyLoaded) return if (!historyLoaded) return
if (conversationsChanged) { if (conversationsChanged) {
...@@ -264,11 +269,12 @@ class Account( ...@@ -264,11 +269,12 @@ class Account(
conversation.sortHistory() conversation.sortHistory()
sortedConversations.sortWith(ConversationComparator()) sortedConversations.sortWith(ConversationComparator())
} }
// TODO: remove next line when profile is updated through dedicated signal sortedList = ArrayList(sortedConversations)
conversationSubject.onNext(conversation)
conversationsSubject.onNext(ArrayList(sortedConversations))
updateUnreadConversations()
} }
// TODO: remove next line when profile is updated through dedicated signal
conversationSubject.onNext(conversation)
conversationsSubject.onNext(sortedList)
updateUnreadConversations(sortedList)
} }
/** /**
...@@ -313,7 +319,8 @@ class Account( ...@@ -313,7 +319,8 @@ class Account(
if (conversation == cache[key]) { if (conversation == cache[key]) {
if (isJami && !conversation.isSwarm if (isJami && !conversation.isSwarm
//&& conversation.contacts.size == 1 //&& conversation.contacts.size == 1
&& !conversation.contact!!.conversationUri.blockingFirst().equals(conversation.uri)) { && conversation.contact!!.conversationUri.blockingFirst() != conversation.uri
) {
return return
} }
if (mContacts.containsKey(key) || !isJami) { if (mContacts.containsKey(key) || !isJami) {
...@@ -330,7 +337,7 @@ class Account( ...@@ -330,7 +337,7 @@ class Account(
fun refreshed(conversation: Conversation) { fun refreshed(conversation: Conversation) {
synchronized(conversations) { synchronized(conversations) {
if (conversations.containsValue(conversation)) { if (conversations.containsValue(conversation)) {
conversationRefreshed(conversation) conversationRefreshed(conversation, sortedConversations)
return return
} }
} }
...@@ -727,9 +734,9 @@ class Account( ...@@ -727,9 +734,9 @@ class Account(
updated(c) updated(c)
} }
historyLoaded = true historyLoaded = true
conversationChanged()
pendingChanged()
} }
conversationChanged()
pendingChanged()
} }
private fun getSortedConversations(): List<Conversation> { private fun getSortedConversations(): List<Conversation> {
...@@ -738,7 +745,8 @@ class Account( ...@@ -738,7 +745,8 @@ class Account(
if (conversations.isNotEmpty()) { if (conversations.isNotEmpty()) {
sortedConversations.addAll(conversations.values) sortedConversations.addAll(conversations.values)
for (c in sortedConversations) c.sortHistory() for (c in sortedConversations) c.sortHistory()
Collections.sort(sortedConversations, ConversationComparator()) //Collections.sort(sortedConversations, ConversationComparator())
sortedConversations.sortWith(ConversationComparator())
} }
conversationsChanged = false conversationsChanged = false
} }
...@@ -778,8 +786,8 @@ class Account( ...@@ -778,8 +786,8 @@ class Account(
} }
pendingConversation.addContactEvent(contact) pendingConversation.addContactEvent(contact)
} }
conversationChanged()
} }
conversationChanged()
} }
private fun contactRemoved(uri: Uri, conversationUri: Uri) { private fun contactRemoved(uri: Uri, conversationUri: Uri) {
...@@ -789,8 +797,8 @@ class Account( ...@@ -789,8 +797,8 @@ class Account(
synchronized(pending) { if (pending.remove(key) != null) pendingChanged() } synchronized(pending) { if (pending.remove(key) != null) pendingChanged() }
conversations.remove(key) conversations.remove(key)
//swarmConversations.remove(conversationUri.uri) //swarmConversations.remove(conversationUri.uri)
conversationChanged()
} }
conversationChanged()
} }
private fun getConversationByCallId(callId: String): Conversation? { private fun getConversationByCallId(callId: String): Conversation? {
...@@ -812,8 +820,10 @@ class Account( ...@@ -812,8 +820,10 @@ class Account(
else -> Contact.PresenceStatus.CONNECTED else -> Contact.PresenceStatus.CONNECTED
}) })
synchronized(conversations) { synchronized(conversations) {
conversations[contactUri]?.let { conversationRefreshed(it) } Pair(conversations[contactUri], sortedConversations)
} }.let { it.first?.let {
conv -> conversationRefreshed(conv, it.second)
}}
synchronized(pending) { if (pending.containsKey(contactUri)) pendingRefreshed() } synchronized(pending) { if (pending.containsKey(contactUri)) pendingRefreshed() }
} }
...@@ -920,13 +930,10 @@ class Account( ...@@ -920,13 +930,10 @@ class Account(
fun setActiveCalls(conversationId: String, activeCalls: List<Conversation.ActiveCall>) = fun setActiveCalls(conversationId: String, activeCalls: List<Conversation.ActiveCall>) =
getSwarm(conversationId)?.setActiveCalls(activeCalls) getSwarm(conversationId)?.setActiveCalls(activeCalls)
private fun updateUnreadConversations() { private fun updateUnreadConversations(sortedConversations: List<Conversation>) {
var unread = 0 unreadConversationsSubject.onNext(sortedConversations.count {
for (model in sortedConversations) { it.lastEvent?.isRead == false
val last = model.lastEvent })
if (last != null && !last.isRead) unread++
}
unreadConversationsSubject.onNext(unread)
} }
private class ConversationComparator : Comparator<Conversation> { private class ConversationComparator : Comparator<Conversation> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment