Skip to content

Commit

Permalink
Small updates
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmitrevski committed Nov 26, 2023
1 parent b2a68d9 commit ab926e6
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

# Upcoming

### ✅ Added
- Jump to a message that is not on the first page
- Jump to a message in a thread
- Bi-directional scrolling of the message list

### 🐞 Fixed
- Some links not being rendered correctly

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ protocol ChannelDataSource: AnyObject {
completion: ((Error?) -> Void)?
)

/// Loads the first page of the channel.
/// - Parameter completion: called when the initial page is loaded.
func loadFirstPage(_ completion: ((_ error: Error?) -> Void)?)
}

Expand All @@ -79,9 +81,11 @@ class ChatChannelDataSource: ChannelDataSource, ChatChannelControllerDelegate {

let controller: ChatChannelController
weak var delegate: MessagesDataSource?

var messages: LazyCachedMapCollection<ChatMessage> {
controller.messages
}

var hasLoadedAllNextMessages: Bool {
controller.hasLoadedAllNextMessages
}
Expand Down Expand Up @@ -146,10 +150,13 @@ class MessageThreadDataSource: ChannelDataSource, ChatMessageControllerDelegate

let channelController: ChatChannelController
let messageController: ChatMessageController

weak var delegate: MessagesDataSource?

var messages: LazyCachedMapCollection<ChatMessage> {
messageController.replies
}

var hasLoadedAllNextMessages: Bool {
messageController.hasLoadedAllNextReplies
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/StreamChatSwiftUI/ChatChannel/ChatChannelView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public struct ChatChannelView<Factory: ViewFactory>: View, KeyboardReadable {
currentDateString: viewModel.currentDateString,
listId: viewModel.listId,
isMessageThread: viewModel.isMessageThread,
shouldShowTypingIndicator: viewModel.shouldShowTypingIndicator,
scrollPosition: $viewModel.scrollPosition,
shouldShowTypingIndicator: viewModel.shouldShowTypingIndicator,
scrollPosition: $viewModel.scrollPosition,
loadingNextMessages: viewModel.loadingNextMessages,
onMessageAppear: viewModel.handleMessageAppear(index:scrollDirection:),
onScrollToBottom: viewModel.scrollToLastMessage,
Expand Down
13 changes: 6 additions & 7 deletions Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
return 5
}
}()

private var timer: Timer?
private var currentDate: Date? {
didSet {
Expand Down Expand Up @@ -106,7 +107,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {

@Published public var shouldShowTypingIndicator = false
@Published public var scrollPosition: String?
@Published public private(set)var loadingNextMessages: Bool = false
@Published public private(set) var loadingNextMessages: Bool = false

public var channel: ChatChannel? {
channelController.channel
Expand Down Expand Up @@ -140,9 +141,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
messages = channelDataSource.messages

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
if let scrollToMessage,
let parentMessageId = scrollToMessage.parentMessageId,
messageController == nil {
if let scrollToMessage, let parentMessageId = scrollToMessage.parentMessageId, messageController == nil {
let message = channelController.dataStore.message(id: parentMessageId)
self?.threadMessage = message
self?.threadMessageShown = true
Expand Down Expand Up @@ -206,7 +205,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
if channelDataSource.hasLoadedAllNextMessages {
updateScrolledIdToNewestMessage()
} else {
channelDataSource.loadFirstPage { [weak self] error in
channelDataSource.loadFirstPage { [weak self] _ in
self?.scrolledId = self?.messages.first?.messageId
}
}
Expand Down Expand Up @@ -424,8 +423,8 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
}
loadingNextMessages = true

if scrollPosition != self.messages.first?.messageId {
scrollPosition = self.messages[index].messageId
if scrollPosition != messages.first?.messageId {
scrollPosition = messages[index].messageId
}

channelDataSource.loadNextMessages(limit: newerMessagesLimit) { [weak self] _ in
Expand Down
1 change: 1 addition & 0 deletions Sources/StreamChatSwiftUI/Utils/MessageCachingUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MessageCachingUtils {
}
}
}

var jumpToReplyId: String?

func authorId(for message: ChatMessage) -> String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,37 @@ class ChatChannelDataSource_Tests: StreamChatTestCase {
XCTAssert(channelCall == true)
XCTAssert(noChannelCall == false)
}

func test_channelDataSource_hasLoadedAllNextMessages() {
// Given
let expected: [ChatMessage] = [message]
let channelDataSource = makeChannelDataSource(messages: expected)

// When
let messages = channelDataSource.messages
channelDataSource.loadFirstPage(nil)

// Then
XCTAssert(messages[0] == expected[0])
XCTAssert(messages.count == expected.count)
XCTAssert(channelDataSource.hasLoadedAllNextMessages == true)
}

func test_channelDataSource_loadPageAroundMessageId() {
// Given
let handler = MockMessagesDataSourceHandler()
let expected: [ChatMessage] = [message]
let controller = makeChannelController(messages: expected)
let channelDataSource = ChatChannelDataSource(controller: controller)
channelDataSource.delegate = handler

// When
channelDataSource.loadPageAroundMessageId(.unique, completion: nil)
let loadPageCall = controller.loadPageAroundMessageIdCallCount

// Then
XCTAssert(loadPageCall == 1)
}

func test_messageThreadDataSource_messages() {
// Given
Expand Down

0 comments on commit ab926e6

Please sign in to comment.