Skip to content

Commit 7daaa08

Browse files
authored
[FirebaseAI] Multimodal Attachment Preview Fix (#1814)
* add preView for MultimodalAttachment * add extension View for MultimodalAttachment
1 parent fd91a2c commit 7daaa08

File tree

2 files changed

+32
-43
lines changed

2 files changed

+32
-43
lines changed

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

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

75+
extension MultimodalAttachment: View {
76+
public var body: some View {
77+
AttachmentPreviewCard(attachment: self)
78+
}
79+
}
80+
7581
// validate file type & mime type
7682
extension MultimodalAttachment {
7783
public static let supportedFileExtensions: Set<String> = [
78-
// Documents / text
79-
"pdf", "txt", "text",
8084
// Images
81-
"jpg", "jpeg", "png", "webp",
85+
"png", "jpeg", "webp",
8286
// Video
83-
"flv", "mov", "qt", "mpeg", "mpg", "ps", "mp4", "webm", "wmv", "3gp", "3gpp",
87+
"flv", "mov", "mpeg", "mpegps", "mpg", "mp4", "webm", "wmv", "3gpp",
8488
// Audio
85-
"aac", "flac", "mp3", "m4a", "mpga", "mp4a", "opus", "pcm", "raw", "wav", "weba",
89+
"aac", "flac", "mp3", "mpa", "mpeg", "mpga", "mp4", "opus", "pcm", "wav", "webm",
90+
// Documents
91+
"pdf", "txt",
8692
]
8793

8894
public static func validateFileType(url: URL) throws {
@@ -209,38 +215,32 @@ extension MultimodalAttachment {
209215
let fileExtension = url.pathExtension.lowercased()
210216

211217
switch fileExtension {
212-
// Documents / text
213-
case "pdf":
214-
return "application/pdf"
215-
case "txt", "text":
216-
return "text/plain"
217-
218218
// Images
219-
case "jpg", "jpeg":
220-
return "image/jpeg"
221219
case "png":
222220
return "image/png"
221+
case "jpeg":
222+
return "image/jpeg"
223223
case "webp":
224224
return "image/webp"
225225

226226
// Video
227227
case "flv":
228228
return "video/x-flv"
229-
case "mov", "qt":
229+
case "mov":
230230
return "video/quicktime"
231231
case "mpeg":
232232
return "video/mpeg"
233+
case "mpegps":
234+
return "video/mpegps"
233235
case "mpg":
234236
return "video/mpg"
235-
case "ps":
236-
return "video/mpegps"
237237
case "mp4":
238238
return "video/mp4"
239239
case "webm":
240240
return "video/webm"
241241
case "wmv":
242242
return "video/wmv"
243-
case "3gp", "3gpp":
243+
case "3gpp":
244244
return "video/3gpp"
245245

246246
// Audio
@@ -249,22 +249,28 @@ extension MultimodalAttachment {
249249
case "flac":
250250
return "audio/flac"
251251
case "mp3":
252-
return "audio/mpeg"
253-
case "m4a":
252+
return "audio/mp3"
253+
case "mpa":
254254
return "audio/m4a"
255+
case "mpeg":
256+
return "audio/mpeg"
255257
case "mpga":
256258
return "audio/mpga"
257-
case "mp4a":
259+
case "mp4":
258260
return "audio/mp4"
259261
case "opus":
260262
return "audio/opus"
261-
case "pcm", "raw":
262-
return "audio/pcm"
263263
case "wav":
264264
return "audio/wav"
265-
case "weba":
265+
case "webm":
266266
return "audio/webm"
267267

268+
// Documents / text
269+
case "pdf":
270+
return "application/pdf"
271+
case "txt":
272+
return "text/plain"
273+
268274
default:
269275
return "application/octet-stream"
270276
}

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)