Skip to content

Commit 2350e93

Browse files
authored
Fix message thread reply footnote view not shown if parent message not in cache (#681)
* First solution: Provide the reply in the parentMessage parameter and do workaround * Revert "First solution: Provide the reply in the parentMessage parameter and do workaround" This reverts commit cfb4b2e. * Final solution: Create a Lazy view that waits for the parent message to be fetched This solution requires the least amount of changes and there is also no breaking change * Update CHANGELOG.md * Remove unused properties * PR feedback * Fix Xcode 15 * Fix Xcode 15 * Use on appear instead * Please....
1 parent 6525d6b commit 2350e93

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
33

44
# Upcoming
55

6+
### 🐞 Fixed
7+
- Fix message thread reply footnote view not shown if parent message not in cache [#681](https://github.com/GetStream/stream-chat-swiftui/pull/681)
68
### ⚡ Performance
79
- Improve message search performance [#680](https://github.com/GetStream/stream-chat-swiftui/pull/680)
810

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageContainerView.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,20 @@ public struct MessageContainerView<Factory: ViewFactory>: View {
185185
)
186186
.accessibilityElement(children: .contain)
187187
.accessibility(identifier: "MessageRepliesView")
188+
} else if message.showReplyInChannel, let parentId = message.parentMessageId {
189+
/// In case the parent message is not available in the local cache, we need to fetch it from the remote server.
190+
/// The lazy view uses the `factory.makeMessageRepliesShownInChannelView` internally once the parent message is fetched.
191+
LazyMessageRepliesView(
192+
factory: factory,
193+
channel: channel,
194+
message: message,
195+
parentMessageController: chatClient.messageController(
196+
cid: channel.cid,
197+
messageId: parentId
198+
)
199+
)
200+
.accessibilityElement(children: .contain)
201+
.accessibility(identifier: "MessageRepliesView")
188202
}
189203
}
190204

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageRepliesView.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,45 @@ public struct MessageRepliesView<Factory: ViewFactory>: View {
116116
}
117117
}
118118
}
119+
120+
/// Lazy view that uses the message controller to fetch the parent message before creating message replies view.
121+
/// This is needed when the parent message is not available in the local cache.
122+
/// Changing the `parentMessage` to `nil` in the `MessageRepliesView` would case multiple changes including breaking changes.
123+
struct LazyMessageRepliesView<Factory: ViewFactory>: View {
124+
@StateObject private var parentMessageObserver: ChatMessageController.ObservableObject
125+
126+
var factory: Factory
127+
var channel: ChatChannel
128+
var message: ChatMessage
129+
130+
init(
131+
factory: Factory,
132+
channel: ChatChannel,
133+
message: ChatMessage,
134+
parentMessageController: ChatMessageController
135+
) {
136+
_parentMessageObserver = StateObject(wrappedValue: parentMessageController.observableObject)
137+
self.factory = factory
138+
self.channel = channel
139+
self.message = message
140+
}
141+
142+
var body: some View {
143+
VStack {
144+
if let parentMessage = parentMessageObserver.message {
145+
factory.makeMessageRepliesShownInChannelView(
146+
channel: channel,
147+
message: message,
148+
parentMessage: parentMessage,
149+
replyCount: parentMessage.replyCount
150+
)
151+
} else {
152+
EmptyView()
153+
}
154+
}.onAppear {
155+
if parentMessageObserver.message == nil {
156+
parentMessageObserver.controller.synchronize()
157+
}
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)