Fix unread decorator position when read follow unread.

This commit is contained in:
Cody Henthorne
2024-01-05 15:15:27 -05:00
committed by Alex Hart
parent 78b714e019
commit 0b4bbd5db2
5 changed files with 46 additions and 24 deletions

View File

@@ -125,8 +125,8 @@ class ConversationItemDecorations(hasWallpaper: Boolean = false, private val sch
val state: UnreadState = unreadState
if (state is UnreadState.InitialUnreadState) {
val firstUnread = items[(state.unreadCount - 1).coerceIn(items.indices)]
val timestamp = (firstUnread as? ConversationMessageElement)?.timestamp()
val firstUnread: ConversationMessageElement? = findFirstUnreadStartingAt(items, (state.unreadCount - 1).coerceIn(items.indices))
val timestamp = firstUnread?.timestamp()
if (timestamp != null) {
unreadState = UnreadState.CompleteUnreadState(unreadCount = state.unreadCount, firstUnreadTimestamp = timestamp)
}
@@ -149,6 +149,17 @@ class ConversationItemDecorations(hasWallpaper: Boolean = false, private val sch
}
}
private fun findFirstUnreadStartingAt(items: List<ConversationElement?>, startingIndex: Int): ConversationMessageElement? {
val endingIndex = (startingIndex + 20).coerceAtMost(items.lastIndex)
for (index in startingIndex..endingIndex) {
val item = items[index] as? ConversationMessageElement
if ((item?.conversationMessage?.messageRecord as? MmsMessageRecord)?.isRead == false) {
return item
}
}
return items[startingIndex] as? ConversationMessageElement
}
private fun isFirstUnread(bindingAdapterPosition: Int): Boolean {
val state = unreadState