mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
refactor: unread count from RecyclerView scroll listener
This commit is contained in:
parent
e5396524fb
commit
0774124603
1 changed files with 25 additions and 13 deletions
|
|
@ -101,19 +101,32 @@ class MessagesFragment : Fragment(), Logging {
|
|||
|
||||
fun scrollToFirstUnreadMessage() {
|
||||
val position = messages.indexOfFirst { !it.read }
|
||||
if (position > 0) {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
fun clearUnreadCount() {
|
||||
val firstUnreadItem = messages.firstOrNull { !it.read } ?: return
|
||||
val lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition()
|
||||
if (lastVisibleItemPosition != RecyclerView.NO_POSITION) {
|
||||
val lastVisibleItem = messages[lastVisibleItemPosition]
|
||||
val contactKey = lastVisibleItem.contact_key
|
||||
val timestamp = lastVisibleItem.received_time
|
||||
|
||||
if (timestamp >= firstUnreadItem.received_time) {
|
||||
model.clearUnreadCount(contactKey, timestamp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = messages.size
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
|
|
@ -301,20 +314,19 @@ class MessagesFragment : Fragment(), Logging {
|
|||
binding.messageListView.layoutManager = layoutManager
|
||||
|
||||
binding.messageListView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
|
||||
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
|
||||
super.onScrollStateChanged(recyclerView, newState)
|
||||
|
||||
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
||||
messagesAdapter.clearUnreadCount()
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
if (dy == 0) {
|
||||
messagesAdapter.clearUnreadCount()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue