Skip to content

Commit eb24259

Browse files
authored
Add jumping to pinned message when tapping a message in the pinned messages view (#884)
* Add jumping to pinned message when tapping a message in the pinned messages view * Update CHANGELOG.md * Remove unnecessary onItemTap * Changes default jump to message position to center instead of bottom * Update CHANGELOG.md
1 parent 745bf5a commit eb24259

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
77
- Add support for customising the `MessageAvatarView` placeholder [#878](https://github.com/GetStream/stream-chat-swiftui/pull/878)
88
- Add `ViewFactory.makeVideoPlayerFooterView` to customize video player footer [#879](https://github.com/GetStream/stream-chat-swiftui/pull/879)
99
- Add `utils.channelListConfig.channelItemMutedLayoutStyle` [#881](https://github.com/GetStream/stream-chat-swiftui/pull/881)
10+
- Add jumping to pinned message when tapping a message in the pinned messages view [#884](https://github.com/GetStream/stream-chat-swiftui/pull/884)
11+
12+
### 🔄 Changed
13+
- From now on, jumping to a message will centre it in the middle instead of at the top [#884](https://github.com/GetStream/stream-chat-swiftui/pull/884)
1014

1115
# [4.81.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.81.0)
1216
_July 03, 2025_

Sources/StreamChatSwiftUI/ChatChannel/ChannelInfo/PinnedMessagesView.swift

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import SwiftUI
88
/// View displaying pinned messages in the chat info screen.
99
public struct PinnedMessagesView<Factory: ViewFactory>: View {
1010
@Injected(\.images) private var images
11+
@Injected(\.utils) private var utils
12+
@Injected(\.chatClient) private var chatClient
1113

1214
@StateObject private var viewModel: PinnedMessagesViewModel
1315

1416
let factory: Factory
17+
private let channel: ChatChannel
1518

1619
public init(
1720
factory: Factory = DefaultViewFactory.shared,
@@ -24,6 +27,7 @@ public struct PinnedMessagesView<Factory: ViewFactory>: View {
2427
channelController: channelController
2528
)
2629
)
30+
self.channel = channel
2731
self.factory = factory
2832
}
2933

@@ -33,7 +37,27 @@ public struct PinnedMessagesView<Factory: ViewFactory>: View {
3337
ScrollView {
3438
LazyVStack(spacing: 0) {
3539
ForEach(viewModel.pinnedMessages) { message in
36-
PinnedMessageView(factory: factory, message: message, channel: viewModel.channel)
40+
ZStack {
41+
PinnedMessageView(factory: factory, message: message, channel: viewModel.channel)
42+
.contentShape(Rectangle())
43+
.onTapGesture {
44+
viewModel.selectedMessage = message
45+
}
46+
NavigationLink(
47+
tag: message,
48+
selection: $viewModel.selectedMessage
49+
) {
50+
LazyView(
51+
makeMessageDestination(message: message)
52+
.modifier(HideTabBarModifier(
53+
handleTabBarVisibility: utils.messageListConfig.handleTabBarVisibility
54+
))
55+
)
56+
} label: {
57+
EmptyView()
58+
}
59+
.opacity(0) // Fixes showing accessibility button shape
60+
}
3761
Divider()
3862
}
3963
}
@@ -49,6 +73,26 @@ public struct PinnedMessagesView<Factory: ViewFactory>: View {
4973
}
5074
.navigationTitle(L10n.ChatInfo.PinnedMessages.title)
5175
}
76+
77+
private func makeMessageDestination(message: ChatMessage) -> ChatChannelView<Factory> {
78+
let channelController = utils.channelControllerFactory
79+
.makeChannelController(for: channel.cid)
80+
81+
var messageController: ChatMessageController?
82+
if let parentMessageId = message.parentMessageId {
83+
messageController = chatClient.messageController(
84+
cid: channel.cid,
85+
messageId: parentMessageId
86+
)
87+
}
88+
89+
return ChatChannelView(
90+
viewFactory: factory,
91+
channelController: channelController,
92+
messageController: messageController,
93+
scrollToMessage: message
94+
)
95+
}
5296
}
5397

5498
struct PinnedMessageView<Factory: ViewFactory>: View {

Sources/StreamChatSwiftUI/ChatChannel/ChannelInfo/PinnedMessagesViewModel.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public class PinnedMessagesViewModel: ObservableObject {
1111
let channel: ChatChannel
1212

1313
@Published var pinnedMessages: [ChatMessage]
14-
14+
@Published var selectedMessage: ChatMessage?
15+
1516
private var channelController: ChatChannelController?
1617

1718
public init(channel: ChatChannel, channelController: ChatChannelController? = nil) {

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListConfig.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public struct MessageListConfig {
2424
maxTimeIntervalBetweenMessagesInGroup: TimeInterval = 60,
2525
cacheSizeOnChatDismiss: Int = 1024 * 1024 * 100,
2626
iPadSplitViewEnabled: Bool = true,
27-
scrollingAnchor: UnitPoint = .bottom,
27+
scrollingAnchor: UnitPoint = .center,
2828
showNewMessagesSeparator: Bool = true,
2929
handleTabBarVisibility: Bool = true,
3030
messageListAlignment: MessageListAlignment = .standard,

0 commit comments

Comments
 (0)