Skip to content

Fix rendering link attachment preview with other attachment types #659

New issue

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

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

Already on GitHub? # to your account

Conversation

nuno-vieira
Copy link
Member

@nuno-vieira nuno-vieira commented Nov 20, 2024

🔗 Issue Link

Fixes IOS-545

🎯 Goal

Fix rendering link attachment preview with other attachment types

🛠 Implementation

I've added a new condition, "hasOnlyLinks", to render the link attachment. This works, but I feel like the best solution would be for this to be the responsibility of MessageTypeResolving. The problem is that the name of the method is hasLinkAttachment(), so if I added this boolean inside, the name loses its true meaning because we are checking instead "onlyHasLinkAttachment()`.

So it feels like this MessageTypeResolving would probably be better for the names to be something like: shouldRenderLinkAttachment(). This way, additional logic could be added to these methods, and it would be easier for customers to provide their own logic whether they want an attachment to be rendered or not. Maybe we can deprecate the old names, and introduce the "shouldRender" prefix instead, but probably out of scope for this PR.

🧪 Testing

Add an image and a link in the same message. It should only render the image preview, not the link preview.

☑️ Checklist

  • I have signed the Stream CLA (required)
  • Changelog is updated with client-facing changes
  • New code is covered by unit tests
  • Affected documentation updated (docusaurus, tutorial, CMS (task created)

@Stream-SDK-Bot
Copy link
Collaborator

Stream-SDK-Bot commented Nov 20, 2024

SDK Size

title develop branch diff status
StreamChatSwiftUI 7.99 MB 7.99 MB 0 KB 🟢

@@ -44,7 +44,8 @@ public struct MessageView<Factory: ViewFactory>: View {
} else if let poll = message.poll {
factory.makePollView(message: message, poll: poll, isFirst: isFirst)
} else if !message.attachmentCounts.isEmpty {
if messageTypeResolver.hasLinkAttachment(message: message) {
let hasOnlyLinks = message.attachmentCounts.keys.allSatisfy { $0 == .linkPreview }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you stated in the PR description - probably good to move this in the resolver directly. It's a small breaking change in functionality, but it's actually a bug fix. We can deal with the inaccurate method name in a major release.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that we are doing this kinda logic with video attachment as well, where we only render the video attachment view if there is not image attachment as well:

messageTypeResolver.hasVideoAttachment(message: message)
                    && !messageTypeResolver.hasImageAttachment(message: message)

That is why opted for this approach as well 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Maybe we can just put the check directly after the &&, so it's executed only when hasLinkAttachment is true?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes makes sense 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done ✅

@nuno-vieira nuno-vieira enabled auto-merge (squash) November 21, 2024 12:36
@nuno-vieira nuno-vieira merged commit b75eb97 into develop Nov 21, 2024
11 checks passed
@nuno-vieira nuno-vieira deleted the fix/only-render-link-attachment-preview-if-attachment-is-only-link branch November 21, 2024 13:48
@Stream-SDK-Bot Stream-SDK-Bot mentioned this pull request Nov 25, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants