Added unread messages count indicator to bottom nav bar

This commit is contained in:
Sylvain Berfini 2023-10-11 16:07:27 +02:00
parent 0d43006a14
commit 9c6533bceb
3 changed files with 63 additions and 0 deletions

View file

@ -26,6 +26,8 @@ import androidx.lifecycle.ViewModel
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.core.Call
import org.linphone.core.ChatMessage
import org.linphone.core.ChatRoom
import org.linphone.core.Core
import org.linphone.core.CoreListenerStub
import org.linphone.core.tools.Log
@ -50,6 +52,8 @@ class BottomNavBarViewModel @UiThread constructor() : ViewModel() {
val missedCallsCount = MutableLiveData<Int>()
val unreadMessages = MutableLiveData<Int>()
private val coreListener = object : CoreListenerStub() {
@WorkerThread
override fun onCallStateChanged(
@ -62,12 +66,27 @@ class BottomNavBarViewModel @UiThread constructor() : ViewModel() {
updateMissedCallsCount()
}
}
@WorkerThread
override fun onMessagesReceived(
core: Core,
chatRoom: ChatRoom,
messages: Array<out ChatMessage>
) {
updateUnreadMessagesCount()
}
@WorkerThread
override fun onChatRoomRead(core: Core, chatRoom: ChatRoom) {
updateUnreadMessagesCount()
}
}
init {
coreContext.postOnCoreThread { core ->
core.addListener(coreListener)
updateMissedCallsCount()
updateUnreadMessagesCount()
}
updateAvailableMenus()
@ -93,6 +112,17 @@ class BottomNavBarViewModel @UiThread constructor() : ViewModel() {
missedCallsCount.postValue(count)
}
@WorkerThread
fun updateUnreadMessagesCount() {
val account = LinphoneUtils.getDefaultAccount()
val count = account?.unreadChatMessageCount ?: coreContext.core.unreadChatMessageCount
val moreThanOne = count > 1
Log.i(
"$TAG There ${if (moreThanOne) "are" else "is"} [$count] unread ${if (moreThanOne) "messages" else "message"}"
)
unreadMessages.postValue(count)
}
@UiThread
fun resetMissedCallsCount() {
coreContext.postOnCoreThread { core ->

View file

@ -92,6 +92,21 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/calls" />
<androidx.appcompat.widget.AppCompatTextView
style="@style/default_text_style"
android:id="@+id/unread_messages"
android:layout_width="24dp"
android:layout_height="24dp"
android:gravity="center"
android:background="@drawable/shape_red_round"
android:text="@{String.valueOf(viewModel.unreadMessages), default=`1`}"
android:textColor="@color/white"
android:textSize="13sp"
android:paddingBottom="2dp"
android:visibility="@{viewModel.unreadMessages > 0 ? View.VISIBLE : View.GONE}"
app:layout_constraintTop_toTopOf="@id/conversations"
app:layout_constraintEnd_toEndOf="@id/conversations"/>
<androidx.appcompat.widget.AppCompatTextView
style="@style/bottom_nav_bar_label_style"
android:onClick="@{onMeetingsClicked}"

View file

@ -104,6 +104,24 @@
app:layout_constraintStart_toEndOf="@id/calls"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
style="@style/default_text_style"
android:id="@+id/unread_messages"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="50dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:background="@drawable/shape_red_round"
android:text="@{String.valueOf(viewModel.unreadMessages), default=`1`}"
android:textColor="@color/white"
android:textSize="13sp"
android:paddingBottom="2dp"
android:visibility="@{viewModel.unreadMessages > 0 ? View.VISIBLE : View.GONE}"
app:layout_constraintTop_toTopOf="@id/conversations"
app:layout_constraintStart_toStartOf="@id/conversations"
app:layout_constraintEnd_toEndOf="@id/conversations"/>
<androidx.appcompat.widget.AppCompatTextView
style="@style/bottom_nav_bar_label_style"
android:onClick="@{onMeetingsClicked}"