Skip to content
Snippets Groups Projects
Commit 38e6f43d authored by Pierre Nicolas's avatar Pierre Nicolas :joy: Committed by Adrien Béraud
Browse files

account_spinner: restore unread counter

Sum of unread conversations and pending conversations.
GitLab: #1525

Change-Id: I4b5cd611d8544cf56e7d1440f5e9c03efb31e79c
parent c3de76aa
No related branches found
No related tags found
No related merge requests found
......@@ -22,11 +22,11 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.RelativeLayout
import cx.ring.R
import cx.ring.databinding.ItemToolbarSpinnerBinding
import cx.ring.utils.DeviceUtils
import cx.ring.views.AvatarDrawable
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.CompositeDisposable
import net.jami.model.Account
import net.jami.model.Profile
......@@ -75,10 +75,24 @@ class AccountAdapter(
if (type == TYPE_ACCOUNT) {
val account = getItem(position)!!
// For type=TYPE_CREATE_ACCOUNT, item title is center vertical. So remove this rule.
val params = holder.binding.title.layoutParams as RelativeLayout.LayoutParams
params.removeRule(RelativeLayout.CENTER_VERTICAL)
holder.binding.title.layoutParams = params
// Update the unread counter (sum of unread conversations and pending conversations)
holder.loader.add(
mAccountService.getObservableAccountProfile(account.accountId).switchMap {
Observable.combineLatest(
account.unreadConversations,
account.getPendingSubject()
) { unreadConversationCounter, pendingConversationList ->
unreadConversationCounter + pendingConversationList.size
}
}
.observeOn(DeviceUtils.uiScheduler)
.subscribe {
if (it > 0) {
holder.binding.invitationBadge.visibility = View.VISIBLE
holder.binding.invitationBadge.text = it.toString()
} else holder.binding.invitationBadge.visibility = View.GONE
}
)
// Subscribe to account profile changes to update:
// - avatar
......@@ -105,18 +119,13 @@ class AccountAdapter(
}
}) { e: Throwable -> Log.e(TAG, "Error loading avatar", e) })
} else {
holder.binding.invitationBadge.visibility = View.GONE
holder.binding.title.setText(
if (type == TYPE_CREATE_ACCOUNT) R.string.add_ring_account_title
else R.string.add_sip_account_title
)
holder.binding.logo.setImageResource(R.drawable.baseline_add_24)
holder.binding.subtitle.visibility = View.GONE
// Center vertical the title.
val params = holder.binding.title.layoutParams as RelativeLayout.LayoutParams
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE)
holder.binding.title.layoutParams = params
}
return view
}
......
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/conversation_secondary_background" />
<corners android:radius="@dimen/account_unread_radius" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
......@@ -9,44 +11,57 @@
android:id="@+id/logo"
android:layout_width="@dimen/list_medium_icon_size"
android:layout_height="@dimen/list_medium_icon_size"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:contentDescription="@null"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/baseline_account_circle_white_36"
tools:tint="@color/black" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_toEndOf="@+id/logo"
android:layout_marginHorizontal="16dp"
android:ellipsize="end"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/colorOnSurface"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@id/subtitle"
app:layout_constraintEnd_toStartOf="@id/invitation_badge"
app:layout_constraintStart_toEndOf="@id/logo"
app:layout_constraintTop_toTopOf="parent"
tools:text="Account Name" />
<TextView
android:id="@+id/subtitle"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_marginStart="16dp"
android:layout_toEndOf="@+id/logo"
android:layout_marginHorizontal="16dp"
android:ellipsize="end"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/grey_700"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/invitation_badge"
app:layout_constraintStart_toEndOf="@id/logo"
app:layout_constraintTop_toBottomOf="@id/title"
tools:text="accounturi" />
<View
android:id="@+id/badge_anchor"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignTop="@id/title"
android:layout_alignEnd="@id/title" />
<TextView
android:id="@+id/invitation_badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_unread_counter"
android:backgroundTint="@color/colorSecondary"
android:paddingHorizontal="10dp"
android:paddingVertical="5dp"
android:textColor="@color/invitation_card_on_badge_text_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="4" />
</RelativeLayout>
\ No newline at end of file
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -157,5 +157,6 @@ along with this program; if not, write to the Free Software
<dimen name="custom_message_bubble_default_text_size">14sp</dimen>
<dimen name="custom_message_bubble_emoji_only_text_size">32sp</dimen>
<!-- Account List -->
<dimen name="account_unread_radius">10dp</dimen>
</resources>
\ No newline at end of file
......@@ -66,6 +66,8 @@ class Account(
private val mLocationSubject: Subject<Map<Contact, Observable<ContactLocation>>> = BehaviorSubject.createDefault(contactLocations)
private val mLocationStartedSubject: Subject<ContactLocationEntry> = PublishSubject.create()
private val registrationStateSubject = BehaviorSubject.createDefault(AccountConfig.RegistrationState.valueOf(mVolatileDetails[ConfigKey.ACCOUNT_REGISTRATION_STATUS]))
private val unreadConversationsSubject: Subject<Int> = BehaviorSubject.create()
val unreadConversations: Observable<Int> = unreadConversationsSubject.distinctUntilChanged()
var historyLoader: Single<Account>? = null
var loadedProfile: Single<Profile>? = null
......@@ -231,6 +233,7 @@ class Account(
private fun conversationRefreshed(conversation: Conversation) {
if (historyLoaded) {
conversationSubject.onNext(conversation)
updateUnreadConversations()
}
}
......@@ -239,6 +242,7 @@ class Account(
conversationsChanged = true
if (historyLoaded) {
conversationsSubject.onNext(ArrayList(getSortedConversations()))
updateUnreadConversations()
}
}
}
......@@ -255,6 +259,7 @@ class Account(
// TODO: remove next line when profile is updated through dedicated signal
conversationSubject.onNext(conversation)
conversationsSubject.onNext(ArrayList(sortedConversations))
updateUnreadConversations()
}
}
......@@ -894,6 +899,15 @@ class Account(
fun setActiveCalls(conversationId: String, activeCalls: List<Map<String, String>>) =
getSwarm(conversationId)?.setActiveCalls(activeCalls.map { Conversation.ActiveCall(it) })
private fun updateUnreadConversations() {
var unread = 0
for (model in sortedConversations) {
val last = model.lastEvent
if (last != null && !last.isRead) unread++
}
unreadConversationsSubject.onNext(unread)
}
private class ConversationComparator : Comparator<Conversation> {
override fun compare(a: Conversation, b: Conversation): Int =
Interaction.compare(b.lastEvent, a.lastEvent)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment