diff --git a/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelDataSource.swift b/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelDataSource.swift index 2d0ac707..699be412 100644 --- a/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelDataSource.swift +++ b/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelDataSource.swift @@ -70,6 +70,8 @@ protocol ChannelDataSource: AnyObject { _ messageId: MessageId, completion: ((Error?) -> Void)? ) + + func loadFirstPage(_ completion: ((_ error: Error?) -> Void)?) } /// Implementation of `ChannelDataSource`. Loads the messages of the channel. @@ -133,6 +135,10 @@ class ChatChannelDataSource: ChannelDataSource, ChatChannelControllerDelegate { ) { controller.loadPageAroundMessageId(messageId, completion: completion) } + + func loadFirstPage(_ completion: ((_ error: Error?) -> Void)?) { + controller.loadFirstPage(completion) + } } /// Implementation of the `ChannelDataSource`. Loads the messages in a reply thread. @@ -209,4 +215,8 @@ class MessageThreadDataSource: ChannelDataSource, ChatMessageControllerDelegate ) { messageController.loadPageAroundReplyId(messageId, completion: completion) } + + func loadFirstPage(_ completion: ((_ error: Error?) -> Void)?) { + messageController.loadFirstPage(completion) + } } diff --git a/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift b/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift index 81ec9d74..a114f624 100644 --- a/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift +++ b/Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift @@ -150,7 +150,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource { } else if messageController != nil, let jumpToReplyId = self?.messageCachingUtils.jumpToReplyId { self?.scrolledId = jumpToReplyId self?.messageCachingUtils.jumpToReplyId = nil - } else { + } else if messageController == nil { self?.scrolledId = scrollToMessage?.messageId } } @@ -203,10 +203,13 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource { } public func scrollToLastMessage() { - if scrolledId != nil { - scrolledId = nil + if channelDataSource.hasLoadedAllNextMessages { + updateScrolledIdToNewestMessage() + } else { + channelDataSource.loadFirstPage { [weak self] error in + self?.scrolledId = self?.messages.first?.messageId + } } - scrolledId = messages.first?.messageId } public func jumpToMessage(messageId: String) -> Bool { @@ -351,7 +354,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource { maybeRefreshMessageList() if !showScrollToLatestButton && scrolledId == nil && !loadingNextMessages { - scrollToLastMessage() + updateScrolledIdToNewestMessage() } } @@ -616,6 +619,13 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource { } } + private func updateScrolledIdToNewestMessage() { + if scrolledId != nil { + scrolledId = nil + } + scrolledId = messages.first?.messageId + } + deinit { messageCachingUtils.clearCache() if messageController == nil {