Skip to content

Commit

Permalink
Fix message thread reply footnote view not shown if parent message no…
Browse files Browse the repository at this point in the history
…t 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....
  • Loading branch information
nuno-vieira authored Dec 6, 2024
1 parent 6525d6b commit 2350e93
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
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(
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()
}
}
}
}

0 comments on commit 2350e93

Please sign in to comment.