Skip to content

Commit c170366

Browse files
committed
add preView for MultimodalAttachment
1 parent fd91a2c commit c170366

File tree

2 files changed

+33
-43
lines changed

2 files changed

+33
-43
lines changed

firebaseai/FirebaseAIExample/Features/Multimodal/Models/MultimodalAttachment.swift

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,24 @@ public struct MultimodalAttachment: Attachment, Equatable {
7272
}
7373
}
7474

75+
extension MultimodalAttachment {
76+
@ViewBuilder
77+
public func previewView() -> any View {
78+
AttachmentPreviewCard(attachment: self)
79+
}
80+
}
81+
7582
// validate file type & mime type
7683
extension MultimodalAttachment {
7784
public static let supportedFileExtensions: Set<String> = [
78-
// Documents / text
79-
"pdf", "txt", "text",
8085
// Images
81-
"jpg", "jpeg", "png", "webp",
86+
"png", "jpeg", "webp",
8287
// Video
83-
"flv", "mov", "qt", "mpeg", "mpg", "ps", "mp4", "webm", "wmv", "3gp", "3gpp",
88+
"flv", "mov", "mpeg", "mpegps", "mpg", "mp4", "webm", "wmv", "3gpp",
8489
// Audio
85-
"aac", "flac", "mp3", "m4a", "mpga", "mp4a", "opus", "pcm", "raw", "wav", "weba",
90+
"aac", "flac", "mp3", "mpa", "mpeg", "mpga", "mp4", "opus", "pcm", "wav", "webm",
91+
// Documents
92+
"pdf", "txt",
8693
]
8794

8895
public static func validateFileType(url: URL) throws {
@@ -209,38 +216,32 @@ extension MultimodalAttachment {
209216
let fileExtension = url.pathExtension.lowercased()
210217

211218
switch fileExtension {
212-
// Documents / text
213-
case "pdf":
214-
return "application/pdf"
215-
case "txt", "text":
216-
return "text/plain"
217-
218219
// Images
219-
case "jpg", "jpeg":
220-
return "image/jpeg"
221220
case "png":
222221
return "image/png"
222+
case "jpeg":
223+
return "image/jpeg"
223224
case "webp":
224225
return "image/webp"
225226

226227
// Video
227228
case "flv":
228229
return "video/x-flv"
229-
case "mov", "qt":
230+
case "mov":
230231
return "video/quicktime"
231232
case "mpeg":
232233
return "video/mpeg"
234+
case "mpegps":
235+
return "video/mpegps"
233236
case "mpg":
234237
return "video/mpg"
235-
case "ps":
236-
return "video/mpegps"
237238
case "mp4":
238239
return "video/mp4"
239240
case "webm":
240241
return "video/webm"
241242
case "wmv":
242243
return "video/wmv"
243-
case "3gp", "3gpp":
244+
case "3gpp":
244245
return "video/3gpp"
245246

246247
// Audio
@@ -249,22 +250,28 @@ extension MultimodalAttachment {
249250
case "flac":
250251
return "audio/flac"
251252
case "mp3":
252-
return "audio/mpeg"
253-
case "m4a":
253+
return "audio/mp3"
254+
case "mpa":
254255
return "audio/m4a"
256+
case "mpeg":
257+
return "audio/mpeg"
255258
case "mpga":
256259
return "audio/mpga"
257-
case "mp4a":
260+
case "mp4":
258261
return "audio/mp4"
259262
case "opus":
260263
return "audio/opus"
261-
case "pcm", "raw":
262-
return "audio/pcm"
263264
case "wav":
264265
return "audio/wav"
265-
case "weba":
266+
case "webm":
266267
return "audio/webm"
267268

269+
// Documents / text
270+
case "pdf":
271+
return "application/pdf"
272+
case "txt":
273+
return "text/plain"
274+
268275
default:
269276
return "application/octet-stream"
270277
}

firebaseai/FirebaseAIExample/Features/Multimodal/Views/AttachmentPreviewCard.swift

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ private enum AttachmentType: String {
5959

6060
struct AttachmentPreviewCard: View {
6161
let attachment: MultimodalAttachment
62-
let onRemove: (() -> Void)?
6362

6463
private var attachmentType: AttachmentType {
6564
AttachmentType(mimeType: attachment.mimeType)
@@ -93,16 +92,8 @@ struct AttachmentPreviewCard: View {
9392
Spacer()
9493
}
9594
}
96-
97-
if let onRemove = onRemove {
98-
Button(action: onRemove) {
99-
Image(systemName: "xmark.circle.fill")
100-
.font(.system(size: 16))
101-
.foregroundColor(.gray)
102-
}
103-
.buttonStyle(PlainButtonStyle())
104-
}
10595
}
96+
.frame(width: 180)
10697
.padding(12)
10798
.background(Color(.systemGray6))
10899
.clipShape(RoundedRectangle(cornerRadius: 12))
@@ -127,23 +118,19 @@ struct AttachmentPreviewCard: View {
127118

128119
struct AttachmentPreviewScrollView: View {
129120
let attachments: [MultimodalAttachment]
130-
var onAttachmentRemove: ((MultimodalAttachment) -> Void)? = nil
131121

132122
var body: some View {
133123
if !attachments.isEmpty {
134124
ScrollView(.horizontal, showsIndicators: false) {
135-
LazyHStack(spacing: 8) {
125+
HStack {
136126
ForEach(attachments) { attachment in
137127
AttachmentPreviewCard(
138128
attachment: attachment,
139-
onRemove: onAttachmentRemove == nil ? nil : { onAttachmentRemove?(attachment) }
140129
)
141-
.frame(width: 180)
142130
}
143131
}
144-
.padding(.horizontal, 16)
132+
.padding(.horizontal, 8)
145133
}
146-
.frame(height: 80)
147134
} else {
148135
EmptyView()
149136
}
@@ -157,31 +144,27 @@ struct AttachmentPreviewScrollView: View {
157144
mimeType: "image/jpeg",
158145
data: Data()
159146
),
160-
onRemove: { print("Image removed") }
161147
)
162148

163149
AttachmentPreviewCard(
164150
attachment: MultimodalAttachment(
165151
mimeType: "application/pdf",
166152
data: Data()
167153
),
168-
onRemove: { print("PDF removed") }
169154
)
170155

171156
AttachmentPreviewCard(
172157
attachment: MultimodalAttachment(
173158
mimeType: "video/mp4",
174159
data: Data()
175160
),
176-
onRemove: { print("Video removed") }
177161
)
178162

179163
AttachmentPreviewCard(
180164
attachment: MultimodalAttachment(
181165
mimeType: "audio/mpeg",
182166
data: Data()
183167
),
184-
onRemove: { print("Audio removed") }
185168
)
186169
}
187170
.padding()

0 commit comments

Comments
 (0)