Skip to content

Commit

Permalink
Final solution: Create a Lazy view that waits for the parent message …
Browse files Browse the repository at this point in the history
…to be fetched

This solution requires the least amount of changes and there is also no breaking change
  • Loading branch information
nuno-vieira committed Dec 5, 2024
1 parent 9ac0aef commit ec13e93
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
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,46 @@ 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 need 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 {

@Injected(\.chatClient) private var chatClient
@Injected(\.fonts) private var fonts
@Injected(\.colors) private var colors

@ObservedObject private var parentMessageObserver: ChatMessageController.ObservableObject
var factory: Factory
var channel: ChatChannel
var message: ChatMessage

init(
factory: Factory,
channel: ChatChannel,
message: ChatMessage,
parentMessageController: ChatMessageController
) {
parentMessageObserver = parentMessageController.observableObject
self.factory = factory
self.channel = channel
self.message = message
parentMessageObserver.controller.synchronize()
}

var body: some View {
Group {
if let parentMessage = parentMessageObserver.message {
factory.makeMessageRepliesShownInChannelView(
channel: channel,
message: message,
parentMessage: parentMessage,
replyCount: parentMessage.replyCount
)
} else {
EmptyView()
}
}
}
}

0 comments on commit ec13e93

Please sign in to comment.