diff --git a/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift b/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift index 7fb37638..e4569c7f 100644 --- a/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift +++ b/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift @@ -412,6 +412,11 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource { return } + // Set unread state before updating messages for ensuring the state is up to date before `handleMessageAppear` is called + if lastReadMessageId != nil && firstUnreadMessageId == nil { + firstUnreadMessageId = channelDataSource.firstUnreadMessageId + } + if shouldAnimate(changes: changes) { withAnimation { self.messages = messages @@ -425,10 +430,6 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource { if !showScrollToLatestButton && scrolledId == nil && !loadingNextMessages { updateScrolledIdToNewestMessage() } - - if lastReadMessageId != nil && firstUnreadMessageId == nil { - firstUnreadMessageId = channelDataSource.firstUnreadMessageId - } } func dataSource(