Skip to content

Commit 1f00e52

Browse files
Fixed a bug with threads with custom attachments
1 parent 476edb9 commit 1f00e52

File tree

5 files changed

+81
-7
lines changed

5 files changed

+81
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
99
### 🐞 Fixed
1010
- Fixed a bug with channel list refreshing after deeplinking
1111
- Navigation bar iPad resizing issue
12+
- Fixed a bug with thread with custom attachments dismissed
1213

1314
### 🔄 Changed
1415
- Docs restructuring

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelView.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ public struct ChatChannelView<Factory: ViewFactory>: View, KeyboardReadable {
9797
utils.messageListConfig.messagePopoverEnabled && messageDisplayInfo != nil && !viewModel
9898
.reactionsShown
9999
) ? 0 : 1)
100+
101+
NavigationLink(
102+
isActive: $viewModel.threadMessageShown
103+
) {
104+
if let message = viewModel.threadMessage {
105+
let threadDestination = factory.makeMessageThreadDestination()
106+
threadDestination(channel, message)
107+
} else {
108+
EmptyView()
109+
}
110+
} label: {
111+
EmptyView()
112+
}
100113
}
101114
.accentColor(colors.tintColor)
102115
.overlay(

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
8282

8383
@Published public var editedMessage: ChatMessage?
8484
@Published public var channelHeaderType: ChannelHeaderType = .regular
85+
@Published public var threadMessage: ChatMessage?
86+
@Published public var threadMessageShown = false {
87+
didSet {
88+
if threadMessageShown == false {
89+
threadMessage = nil
90+
}
91+
}
92+
}
8593

8694
public var channel: ChatChannel? {
8795
channelController.channel
@@ -125,10 +133,27 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
125133
object: nil
126134
)
127135

136+
if messageController == nil {
137+
NotificationCenter.default.addObserver(
138+
self,
139+
selector: #selector(selectedMessageThread(notification:)),
140+
name: NSNotification.Name(MessageRepliesConstants.selectedMessageThread),
141+
object: nil
142+
)
143+
}
144+
128145
channelName = channel?.name ?? ""
129146
checkHeaderType()
130147
}
131148

149+
@objc
150+
private func selectedMessageThread(notification: Notification) {
151+
if let message = notification.userInfo?[MessageRepliesConstants.selectedMessage] as? ChatMessage {
152+
threadMessage = message
153+
threadMessageShown = true
154+
}
155+
}
156+
132157
@objc
133158
private func didReceiveMemoryWarning() {
134159
Nuke.ImageCache.shared.removeAll()

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageRepliesView.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
import StreamChat
66
import SwiftUI
77

8+
enum MessageRepliesConstants {
9+
static let selectedMessageThread = "selectedMessageThread"
10+
static let selectedMessage = "selectedMessage"
11+
}
12+
813
/// View shown below a message, when there are replies to it.
914
struct MessageRepliesView<Factory: ViewFactory>: View {
1015

@@ -16,14 +21,15 @@ struct MessageRepliesView<Factory: ViewFactory>: View {
1621
var message: ChatMessage
1722
var replyCount: Int
1823

19-
var threadDestination: Factory.MessageThreadDestination {
20-
let threadDestination = factory.makeMessageThreadDestination()
21-
return threadDestination(channel, message)
22-
}
23-
2424
var body: some View {
25-
NavigationLink {
26-
LazyView(threadDestination)
25+
Button {
26+
// NOTE: this is used to avoid breaking changes.
27+
// Will be updated in a major release.
28+
NotificationCenter.default.post(
29+
name: NSNotification.Name(MessageRepliesConstants.selectedMessageThread),
30+
object: nil,
31+
userInfo: [MessageRepliesConstants.selectedMessage: message]
32+
)
2733
} label: {
2834
HStack {
2935
if !message.isSentByCurrentUser {

StreamChatSwiftUITests/Tests/ChatChannel/ChatChannelViewModel_Tests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,35 @@ class ChatChannelViewModel_Tests: StreamChatTestCase {
356356
XCTAssert(readsString != newChannelReadsString)
357357
}
358358

359+
func test_chatChannelVM_threadMessage() {
360+
// Given
361+
let channelController = makeChannelController()
362+
let viewModel = ChatChannelViewModel(channelController: channelController)
363+
let message = ChatMessage.mock(
364+
id: .unique,
365+
cid: .unique,
366+
text: "Some text",
367+
author: .mock(id: .unique)
368+
)
369+
370+
// When
371+
NotificationCenter.default.post(
372+
name: NSNotification.Name(MessageRepliesConstants.selectedMessageThread),
373+
object: nil,
374+
userInfo: [MessageRepliesConstants.selectedMessage: message]
375+
)
376+
377+
// Then
378+
XCTAssert(viewModel.threadMessage == message)
379+
XCTAssert(viewModel.threadMessageShown == true)
380+
381+
// When
382+
viewModel.threadMessageShown = false
383+
384+
// Then
385+
XCTAssert(viewModel.threadMessage == nil)
386+
}
387+
359388
// MARK: - private
360389

361390
private func makeChannelController(

0 commit comments

Comments
 (0)