Skip to content

Conversation

@YoungHypo
Copy link
Contributor

@YoungHypo YoungHypo commented Nov 17, 2025

This PR refactors the Attachment protocol to create a simpler and more idi-omatic SwiftUI API for rendering attachment previews.

The previous design required types conforming to Attachment to implement a previewView() function. The new approach removes this function and instead requires Attachment to conform directly to SwiftUI.View.

This change simplifies the implementation of custom attachments and makes the code in views that render them cleaner and more intuitive.

Key Changes

  • Protocol Update: The Attachment protocol no longer has a previewView() requirement and now inherits from View.
  • Simplified Views: Views like AttachmentPreviewCard and MessageComposerView now have a simpler generic constraint (<AttachmentType: Attachment & View>) and can render the attachment directly (e.g., attachment) instead
    of calling a function (attachment.previewView()).
  • Concrete Types: ImageAttachment and EmptyAttachment have been updated to conform to View by implementing the required body property.

Before

// Protocol
public protocol Attachment: Identifiable, Hashable {
  @ViewBuilder
 func previewView() -> any View
}

// Usage in a view
struct AttachmentPreviewCard<AttachmentType: Attachment>: View {
  var attachment: AttachmentType
  // ...
  var body: some View {
    attachment.previewView()
  }
}

After

// Protocol
public protocol Attachment: Identifiable, Hashable, View {
}

// Usage in a view                                                                                                                                                                                                       
struct AttachmentPreviewCard<AttachmentType: Attachment>: View {                                                                                                                                                         
  var attachment: AttachmentType                                                                                                                                                                                         
  // ...                                                                                                                                                                                                                 
  var body: some View {                                                                                                                                                                                                  
    attachment                                                                                                                                                                                                           
  }                                                                                                                                                                                                                      
}

@gemini-code-assist
Copy link

Summary of Changes

Hello @YoungHypo, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the attachment preview rendering mechanism by introducing a previewView() method to the Attachment protocol. This change centralizes the view rendering logic within each attachment type, thereby simplifying the AttachmentPreviewCard which now directly displays the attachment's self-rendered preview. This approach enhances modularity and extensibility, making it easier to add new attachment types with custom preview displays.

Highlights

  • Protocol Extension: The Attachment protocol has been extended to include a new previewView() method, allowing each attachment type to define its own preview rendering logic.
  • Attachment Implementations: EmptyAttachment and ImageAttachment now implement the previewView() method, providing specific SwiftUI views for their respective previews.
  • Simplified Preview Card: The AttachmentPreviewCard has been significantly simplified. It no longer contains conditional rendering logic for different attachment types but instead directly calls attachment.previewView(), delegating the preview rendering responsibility to the attachment itself.
  • Visibility Change: The ConcentricClipShapeModifier struct has been made public.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a previewView() method to the Attachment protocol, allowing each attachment type to define its own SwiftUI preview. This is a good architectural improvement that simplifies AttachmentPreviewCard by removing conditional logic. The implementations for ImageAttachment and EmptyAttachment are included. My review includes a couple of suggestions to improve the design's flexibility and adhere to SwiftUI best practices. Specifically, I've noted that hardcoding the frame and style within ImageAttachment.previewView could limit reusability, and I've pointed out a redundant use of AnyView.

@YoungHypo
Copy link
Contributor Author

YoungHypo commented Nov 18, 2025

@peterfriese da4bf21 moved EmptyAttachment and ImageAttachment away from inheriting View directly in the protocol. Instead, they now adopt View through extensions.
AttachmentPreviewCard still requires its generic type to conform to both Attachment and View, so the UI behaves exactly the same. The ConversationKitDemo app complied successfully and shown in the picture

simulator_screenshot_D111F402-6657-43F5-A5A9-9F9414C16317

@peterfriese peterfriese changed the title Add previewView() to Attachment ♻️ Simplify attachment previews by conforming Attachment to View Nov 18, 2025
@peterfriese peterfriese self-assigned this Nov 18, 2025
@peterfriese peterfriese added the enhancement New feature or request label Nov 18, 2025
@peterfriese peterfriese self-requested a review November 18, 2025 12:01
Copy link
Owner

@peterfriese peterfriese left a comment

Choose a reason for hiding this comment

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

LGTM, approved!

@peterfriese peterfriese merged commit f35e9a5 into peterfriese:main Nov 18, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants