diff --git a/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Account.kt b/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Account.kt index 0efe7476d3c1e6f09cdd341f1fe0e24b9b58aae8..29286c23ea1d714e5a32f563ea9c2e01132103ac 100644 --- a/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Account.kt +++ b/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Account.kt @@ -25,7 +25,8 @@ import io.reactivex.rxjava3.subjects.Subject import net.jami.model.Interaction.InteractionStatus import net.jami.services.AccountService import net.jami.utils.Log -import java.util.* +import java.util.Collections +import java.util.Date import kotlin.collections.ArrayList class Account( @@ -60,10 +61,6 @@ class Account( private val conversationSubject: Subject<Conversation> = PublishSubject.create() private val pendingSubject: Subject<List<Conversation>> = BehaviorSubject.create() private val conversationsSubject: Subject<List<Conversation>> = BehaviorSubject.create() - val unreadConversations: Observable<List<Conversation>> = - conversationsSubject.map { conversations -> - conversations.filter { it.lastEvent?.isRead == false } - } private val contactListSubject = BehaviorSubject.create<Collection<Contact>>() private val contactLocations: MutableMap<Contact, Observable<ContactLocation>> = HashMap() private val mLocationSubject: Subject<Map<Contact, Observable<ContactLocation>>> = BehaviorSubject.createDefault(contactLocations) @@ -226,7 +223,7 @@ class Account( getSortedPending() } else { conversation?.sortHistory() - sortedPending.sortWith { a, b -> Interaction.compare(b.lastEvent, a.lastEvent) } + sortedPending.sortWith(ConversationComparator()) } pendingSubject.onNext(getSortedPending()) } @@ -253,8 +250,7 @@ class Account( getSortedConversations() } else { conversation.sortHistory() - sortedConversations.sortWith { a: Conversation, b: Conversation -> - Interaction.compare(b.lastEvent, a.lastEvent) } + sortedConversations.sortWith(ConversationComparator()) } // TODO: remove next line when profile is updated through dedicated signal conversationSubject.onNext(conversation) @@ -406,10 +402,9 @@ class Account( val displayUsername: String? get() { if (isJami) { - val registeredName: String? = registeredName - if (registeredName != null && registeredName.isNotEmpty()) { + val registeredName = registeredName + if (registeredName.isNotEmpty()) return registeredName - } } return username } @@ -900,9 +895,8 @@ class Account( getSwarm(conversationId)?.setActiveCalls(activeCalls.map { Conversation.ActiveCall(it) }) private class ConversationComparator : Comparator<Conversation> { - override fun compare(a: Conversation, b: Conversation): Int { - return Interaction.compare(b.lastEvent, a.lastEvent) - } + override fun compare(a: Conversation, b: Conversation): Int = + Interaction.compare(b.lastEvent, a.lastEvent) } companion object { diff --git a/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Conversation.kt b/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Conversation.kt index bd98f43cb357630df071865483662c211c6dd00c..24026f579f74df6f712ea614ebb0098a34ebf8ef 100644 --- a/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Conversation.kt +++ b/jami-android/libjamiclient/src/main/kotlin/net/jami/model/Conversation.kt @@ -112,7 +112,13 @@ class Conversation : ConversationHistory { sortHistory() aggregateHistory } - val lastEventSubject: Subject<Interaction> = BehaviorSubject.create() + var lastEvent: Interaction? = null + private set(e) { + field = e + if (e != null) + lastEventSubject.onNext(e) + } + private val lastEventSubject: Subject<Interaction> = BehaviorSubject.create() val currentStateObservable: Observable<Pair<Interaction, Boolean>> = Observable.combineLatest( lastEventSubject, @@ -195,7 +201,7 @@ class Conversation : ConversationHistory { } // Update the last event if it was just read interactions.firstOrNull { it.type != Interaction.InteractionType.INVALID }?.let { - lastEventSubject.onNext(it) + lastEvent = it } return interactions } @@ -411,19 +417,11 @@ class Conversation : ConversationHistory { fun sortHistory() { if (mDirty) { aggregateHistory.sortWith { c1, c2 -> c1.timestamp.compareTo(c2.timestamp) } - aggregateHistory.lastOrNull { it.type != Interaction.InteractionType.INVALID }?.let { - lastEventSubject.onNext(it) - } + lastEvent = aggregateHistory.lastOrNull { it.type != Interaction.InteractionType.INVALID } mDirty = false } } - val lastEvent: Interaction? - @Synchronized - get() { - sortHistory() - return aggregateHistory.lastOrNull { it.type != Interaction.InteractionType.INVALID } - } val currentCall: Conference? get() = if (currentCalls.isEmpty()) null else currentCalls[0] @@ -509,6 +507,7 @@ class Conversation : ConversationHistory { @Synchronized fun setHistory(loadedConversation: List<Interaction>) { + mDirty = true aggregateHistory.ensureCapacity(loadedConversation.size) for (i in loadedConversation) { val interaction = getTypedInteraction(i) @@ -516,8 +515,7 @@ class Conversation : ConversationHistory { aggregateHistory.add(interaction) rawHistory[interaction.timestamp] = interaction } - lastEvent?.let { lastEventSubject.onNext(it) } - mDirty = false + sortHistory() } @Synchronized @@ -649,7 +647,7 @@ class Conversation : ConversationHistory { setLastMessageRead(id) } if (interaction.type != Interaction.InteractionType.INVALID) - lastEventSubject.onNext(interaction) + lastEvent = interaction } if (!added) { Log.e(TAG, "Can't attach interaction $id with parent ${interaction.parentId}") diff --git a/jami-android/libjamiclient/src/main/kotlin/net/jami/smartlist/ConversationItemViewModel.kt b/jami-android/libjamiclient/src/main/kotlin/net/jami/smartlist/ConversationItemViewModel.kt index df45847ec60a926f6e54db7ed5de257183f68348..23279f39941d762e2757244fc3dc73b97e15cd87 100644 --- a/jami-android/libjamiclient/src/main/kotlin/net/jami/smartlist/ConversationItemViewModel.kt +++ b/jami-android/libjamiclient/src/main/kotlin/net/jami/smartlist/ConversationItemViewModel.kt @@ -35,7 +35,6 @@ class ConversationItemViewModel( var isChecked = false var selected: Observable<Boolean>? = conversation.getVisible() private set - val lastEvent: Interaction? = conversation.lastEvent val request: TrustRequest? = conversation.request enum class Title { @@ -81,7 +80,6 @@ class ConversationItemViewModel( return contacts === other.contacts && title == other.title && isOnline == other.isOnline - && lastEvent === other.lastEvent } companion object {