Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message thread reply footnote view not shown if parent message not in cache #681

Merged
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

# Upcoming

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ public struct MessageContainerView<Factory: ViewFactory>: View {
)
.accessibilityElement(children: .contain)
.accessibility(identifier: "MessageRepliesView")
} else if message.showReplyInChannel, let parentId = message.parentMessageId {
/// In case the parent message is not available in the local cache, we need to fetch it from the remote server.
/// The lazy view uses the `factory.makeMessageRepliesShownInChannelView` internally once the parent message is fetched.
LazyMessageRepliesView(
factory: factory,
channel: channel,
message: message,
parentMessageController: chatClient.messageController(
nuno-vieira marked this conversation as resolved.
Show resolved Hide resolved
cid: channel.cid,
messageId: parentId
)
)
.accessibilityElement(children: .contain)
.accessibility(identifier: "MessageRepliesView")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,45 @@ public struct MessageRepliesView<Factory: ViewFactory>: View {
}
}
}

/// Lazy view that uses the message controller to fetch the parent message before creating message replies view.
/// This is needed when the parent message is not available in the local cache.
/// Changing the `parentMessage` to `nil` in the `MessageRepliesView` would case multiple changes including breaking changes.
struct LazyMessageRepliesView<Factory: ViewFactory>: View {
@StateObject private var parentMessageObserver: ChatMessageController.ObservableObject

var factory: Factory
var channel: ChatChannel
var message: ChatMessage

init(
factory: Factory,
channel: ChatChannel,
message: ChatMessage,
parentMessageController: ChatMessageController
) {
_parentMessageObserver = StateObject(wrappedValue: parentMessageController.observableObject)
self.factory = factory
self.channel = channel
self.message = message
}

var body: some View {
VStack {
if let parentMessage = parentMessageObserver.message {
factory.makeMessageRepliesShownInChannelView(
channel: channel,
message: message,
parentMessage: parentMessage,
replyCount: parentMessage.replyCount
)
} else {
EmptyView()
}
}.onAppear {
if parentMessageObserver.message == nil {
parentMessageObserver.controller.synchronize()
}
}
}
}
Loading