Skip to content

Commit 62d831f

Browse files
Add init method with view model for the message list (#1046)
1 parent 57261bb commit 62d831f

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListView.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,49 @@ public struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
114114
)
115115
}
116116
}
117+
118+
public init(
119+
factory: Factory = DefaultViewFactory.shared,
120+
channel: ChatChannel,
121+
viewModel: ChatChannelViewModel,
122+
onLongPress: @escaping (MessageDisplayInfo) -> Void = { _ in }
123+
) {
124+
self.init(
125+
factory: factory,
126+
channel: channel,
127+
messages: viewModel.messages,
128+
messagesGroupingInfo: viewModel.messagesGroupingInfo,
129+
scrolledId: Binding(
130+
get: { viewModel.scrolledId },
131+
set: { viewModel.scrolledId = $0 }
132+
),
133+
showScrollToLatestButton: Binding(
134+
get: { viewModel.showScrollToLatestButton },
135+
set: { viewModel.showScrollToLatestButton = $0 }
136+
),
137+
quotedMessage: Binding(
138+
get: { viewModel.quotedMessage },
139+
set: { viewModel.quotedMessage = $0 }
140+
),
141+
currentDateString: viewModel.currentDateString,
142+
listId: viewModel.listId,
143+
isMessageThread: viewModel.isMessageThread,
144+
shouldShowTypingIndicator: viewModel.shouldShowTypingIndicator,
145+
scrollPosition: Binding(
146+
get: { viewModel.scrollPosition },
147+
set: { viewModel.scrollPosition = $0 }
148+
),
149+
loadingNextMessages: viewModel.loadingNextMessages,
150+
firstUnreadMessageId: Binding(
151+
get: { viewModel.firstUnreadMessageId },
152+
set: { viewModel.firstUnreadMessageId = $0 }
153+
),
154+
onMessageAppear: viewModel.handleMessageAppear(index:scrollDirection:),
155+
onScrollToBottom: viewModel.scrollToLastMessage,
156+
onLongPress: onLongPress,
157+
onJumpToMessage: viewModel.jumpToMessage(messageId:)
158+
)
159+
}
117160

118161
public var body: some View {
119162
ZStack {

StreamChatSwiftUITests/Tests/ChatChannel/MessageListView_Tests.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,40 @@ class MessageListView_Tests: StreamChatTestCase {
9898
assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
9999
}
100100

101+
// MARK: - Init with ChatChannelViewModel snapshot tests
102+
103+
func test_messageListView_viewModelInit_withReactions() {
104+
// Given
105+
let channel = ChatChannel.mockDMChannel(config: .init(reactionsEnabled: true))
106+
let view = makeMessageListViewWithViewModel(channel: channel, messages: [.mock(id: .unique, cid: channel.cid, text: "Hello", author: .mock(id: .unique), reactionScores: [MessageReactionType(rawValue: "like"): 1])])
107+
.applyDefaultSize()
108+
109+
// Then
110+
assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
111+
}
112+
113+
func test_messageListView_viewModelInit_noReactions() {
114+
// Given
115+
let channel = ChatChannel.mockDMChannel(config: .init(reactionsEnabled: false))
116+
let view = makeMessageListViewWithViewModel(channel: channel, messages: [.mock(id: .unique, cid: channel.cid, text: "Hello", author: .mock(id: .unique))])
117+
.applyDefaultSize()
118+
119+
// Then
120+
assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
121+
}
122+
123+
func test_messageListView_viewModelInit_unreadIndicator() {
124+
// Given
125+
var channel = ChatChannel.mockDMChannel()
126+
// Set unread count on the channel
127+
channel = ChatChannel.mockDMChannel(unreadCount: .mock(messages: 2))
128+
let view = makeMessageListViewWithViewModel(channel: channel, messages: [.mock(id: .unique, cid: channel.cid, text: "Unread 1", author: .mock(id: .unique))])
129+
.applyDefaultSize()
130+
131+
// Then
132+
assertSnapshot(matching: view, as: .image(perceptualPrecision: precision))
133+
}
134+
101135
// MARK: - private
102136

103137
func makeMessageListView(
@@ -138,4 +172,26 @@ class MessageListView_Tests: StreamChatTestCase {
138172

139173
return messageListView
140174
}
175+
176+
private func makeMessageListViewWithViewModel(
177+
channel: ChatChannel,
178+
messages: [ChatMessage]
179+
) -> MessageListView<DefaultViewFactory> {
180+
// Create a mock channel controller seeded with channel and messages
181+
let controller = ChatChannelTestHelpers.makeChannelController(
182+
chatClient: chatClient,
183+
chatChannel: channel,
184+
messages: messages
185+
)
186+
187+
// Build the view model
188+
let viewModel = ChatChannelViewModel(channelController: controller)
189+
190+
// Return the view using the new initializer
191+
return MessageListView(
192+
factory: DefaultViewFactory.shared,
193+
channel: channel,
194+
viewModel: viewModel
195+
)
196+
}
141197
}
68.6 KB
Loading
Loading
Loading

0 commit comments

Comments
 (0)