From e5c222386723a20c20c00f16a1af3b2161edd0b3 Mon Sep 17 00:00:00 2001 From: martinmitrevski Date: Wed, 6 Dec 2023 13:24:58 +0100 Subject: [PATCH] Working implementation --- .../ChatChannel/ChatChannelViewModel.swift | 11 +++++++++-- .../ChatChannel/MessageList/MessageListView.swift | 9 ++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift b/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift index 35cb811a..3adeb0ca 100644 --- a/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift +++ b/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift @@ -191,10 +191,11 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource { if channelController.channel?.unreadCount.messages ?? 0 > 0 { if channelController.firstUnreadMessageId != nil { firstUnreadMessageId = channelController.firstUnreadMessageId - } else { + canMarkRead = false + } else if channelController.lastReadMessageId != nil { lastReadMessageId = channelController.lastReadMessageId + canMarkRead = false } - canMarkRead = false } } @@ -239,6 +240,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource { //TODO: change to data source. if let firstUnread = self?.channelController.firstUnreadMessageId, let message = self?.channelController.dataStore.message(id: firstUnread) { + self?.firstUnreadMessageId = message.messageId DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { self?.scrolledId = message.messageId } @@ -400,6 +402,11 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource { if !showScrollToLatestButton && scrolledId == nil && !loadingNextMessages { updateScrolledIdToNewestMessage() } + + if lastMessageRead != nil && firstUnreadMessageId == nil { + //TODO: data source + self.firstUnreadMessageId = channelController.firstUnreadMessageId + } } func dataSource( diff --git a/Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListView.swift b/Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListView.swift index 4d6d99e3..d8c62f45 100644 --- a/Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListView.swift +++ b/Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListView.swift @@ -35,6 +35,7 @@ public struct MessageListView: View, KeyboardReadable { @State private var keyboardShown = false @State private var pendingKeyboardUpdate: Bool? @State private var scrollDirection = ScrollDirection.up + @State private var unreadMessagesBannerShown = false private var messageRenderingUtil = MessageRenderingUtil.shared private var skipRenderingMessageIds = [String]() @@ -176,6 +177,12 @@ public struct MessageListView: View, KeyboardReadable { newMessagesStartId: $firstUnreadMessageId, count: newMessagesCount(for: index, message: message) ) + .onAppear { + unreadMessagesBannerShown = true + } + .onDisappear { + unreadMessagesBannerShown = false + } : nil showsLastInGroupInfo ? @@ -282,7 +289,7 @@ public struct MessageListView: View, KeyboardReadable { } }) .overlay( - channel.unreadCount.messages > 0 ? VStack { + (channel.unreadCount.messages > 0 && !unreadMessagesBannerShown) ? VStack { JumpToUnreadButton(unreadCount: channel.unreadCount.messages) { _ = onJumpToMessage?(firstUnreadMessageId ?? "unknown") } onClose: {