feat: add unread message count

This commit is contained in:
andrekir 2024-06-15 12:18:26 -03:00 committed by Andre K
parent e4f5d9b89c
commit d7013e1386
7 changed files with 89 additions and 4 deletions

View file

@ -1,6 +1,7 @@
package com.geeksville.mesh.ui
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.*
@ -98,6 +99,21 @@ class MessagesFragment : Fragment(), Logging {
if (itemCount > 0) layoutManager.scrollToPosition(itemCount - 1)
}
fun scrollToFirstUnreadMessage() {
val position = messages.indexOfFirst { !it.read }
if (position > 0) {
val rect = Rect()
binding.toolbar.getGlobalVisibleRect(rect)
val toolbarOffset = rect.bottom
val offset = binding.messageListView.height - toolbarOffset
layoutManager.scrollToPositionWithOffset(position, offset)
messages[position].apply { model.clearUnreadCount(contact_key, received_time) }
} else {
scrollToBottom()
}
}
override fun getItemCount(): Int = messages.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
@ -226,13 +242,14 @@ class MessagesFragment : Fragment(), Logging {
/// Called when our node DB changes
fun onMessagesChanged(messages: List<Packet>) {
val lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition()
val shouldScrollToBottom =
lastVisibleItemPosition <= 0 || lastVisibleItemPosition == itemCount - 1
val shouldScrollToUnread = lastVisibleItemPosition <= 0
val shouldScrollToBottom = lastVisibleItemPosition == itemCount - 1
this.messages = messages
notifyDataSetChanged() // FIXME, this is super expensive and redraws all messages
if (shouldScrollToBottom) scrollToBottom()
if (shouldScrollToUnread) scrollToFirstUnreadMessage()
}
}
@ -283,6 +300,25 @@ class MessagesFragment : Fragment(), Logging {
layoutManager.stackFromEnd = true // We want the last rows to always be shown
binding.messageListView.layoutManager = layoutManager
binding.messageListView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val firstUnreadItem = messagesAdapter.messages.firstOrNull { !it.read }
if (firstUnreadItem != null && dy > 0) {
val lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition()
if (lastVisibleItemPosition != RecyclerView.NO_POSITION) {
val lastVisibleItem = messagesAdapter.messages[lastVisibleItemPosition]
val timestamp = lastVisibleItem.received_time
if (timestamp > firstUnreadItem.received_time) {
model.clearUnreadCount(contactKey, timestamp)
}
}
}
}
})
model.getMessagesFrom(contactKey).asLiveData().observe(viewLifecycleOwner) {
debug("New messages received: ${it.size}")
messagesAdapter.onMessagesChanged(it)