-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathFileAttachmentView.swift
More file actions
226 lines (196 loc) Β· 7.14 KB
/
Copy pathFileAttachmentView.swift
File metadata and controls
226 lines (196 loc) Β· 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// Copyright Β© 2026 Stream.io Inc. All rights reserved.
//
import StreamChat
import SwiftUI
public struct FileAttachmentsContainer<Factory: ViewFactory>: View {
@Injected(\.colors) var colors
@Injected(\.tokens) var tokens
var factory: Factory
var message: ChatMessage
var width: CGFloat
var isFirst: Bool
@Binding var scrolledId: String?
public init(
factory: Factory,
message: ChatMessage,
width: CGFloat,
isFirst: Bool,
scrolledId: Binding<String?>
) {
self.factory = factory
self.message = message
self.width = width
self.isFirst = isFirst
_scrolledId = scrolledId
}
public var body: some View {
VStack(spacing: 4) {
ForEach(message.fileAttachments, id: \.self) { attachment in
FileAttachmentView(
attachment: attachment,
width: width,
isFirst: isFirst
)
.background(backgroundColor)
.roundWithBorder(cornerRadius: tokens.messageBubbleRadiusAttachment)
}
}
.accessibilityIdentifier("FileAttachmentsContainer")
}
private var backgroundColor: Color {
let attachmentCounts = message.attachmentCounts
if message.text.isEmpty, attachmentCounts.count == 1, attachmentCounts[.file] == 1 {
return .clear
}
return Color(message.isSentByCurrentUser ? colors.chatBackgroundAttachmentOutgoing : colors.chatBackgroundAttachmentIncoming)
}
}
public struct FileAttachmentView: View {
@Injected(\.utils) private var utils
@Injected(\.images) private var images
@Injected(\.fonts) private var fonts
@Injected(\.colors) private var colors
@Injected(\.tokens) private var tokens
@Injected(\.chatClient) private var chatClient
@State private var fullScreenShown = false
var attachment: ChatMessageFileAttachment
var width: CGFloat
var isFirst: Bool
public init(attachment: ChatMessageFileAttachment, width: CGFloat, isFirst: Bool) {
self.attachment = attachment
self.width = width
self.isFirst = isFirst
}
public var body: some View {
HStack {
FileAttachmentDisplayView(
url: attachment.assetURL,
title: attachment.title ?? "",
sizeString: attachment.file.sizeString
)
.onTapGesture {
fullScreenShown = true
}
.accessibilityAction {
fullScreenShown = true
}
Spacer()
if utils.messageListConfig.downloadFileAttachmentsEnabled {
DownloadShareAttachmentView(attachment: attachment)
}
}
.padding(.all, tokens.spacingSm)
.frame(width: width)
.withUploadingStateIndicator(for: attachment.uploadingState, url: attachment.assetURL)
.withDownloadingStateIndicator(for: attachment.downloadingState, url: attachment.assetURL)
.sheet(isPresented: $fullScreenShown) {
FileAttachmentPreview(title: attachment.title, url: previewURL)
}
.accessibilityIdentifier("FileAttachmentView")
}
private var previewURL: URL {
if attachment.downloadingState?.state == .downloaded,
let localFileURL = attachment.downloadingState?.localFileURL {
return localFileURL
}
return attachment.assetURL
}
}
public struct FileAttachmentDisplayView: View {
@Injected(\.images) private var images
@Injected(\.fonts) private var fonts
@Injected(\.colors) private var colors
@Injected(\.tokens) private var tokens
var url: URL
var title: String
var sizeString: String
public init(url: URL, title: String, sizeString: String) {
self.url = url
self.title = title
self.sizeString = sizeString
}
public var body: some View {
HStack(spacing: tokens.spacingSm) {
Image(uiImage: previewImage)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 34, height: 40)
.accessibilityHidden(true)
VStack(alignment: .leading, spacing: tokens.spacingXxs) {
Text(title)
.font(fonts.bodyBold)
.lineLimit(1)
.foregroundColor(Color(colors.text))
Text(sizeString)
.font(fonts.footnote)
.lineLimit(1)
.foregroundColor(Color(colors.textLowEmphasis))
}
Spacer()
}
.accessibilityElement(children: .combine)
}
private var previewImage: UIImage {
let iconName = url.pathExtension
return images.documentPreviews[iconName] ?? images.fileFallback
}
}
struct DownloadShareAttachmentView<Payload: DownloadableAttachmentPayload>: View {
@Injected(\.colors) var colors
@Injected(\.images) var images
@Injected(\.chatClient) var chatClient
@State private var shareSheetShown = false
var attachment: ChatMessageAttachment<Payload>
var body: some View {
Group {
if shouldShowDownloadButton {
downloadButton
} else if shouldShowShareButton {
shareButton
}
}
.sheet(isPresented: $shareSheetShown) {
if let shareURL = attachment.downloadingState?.localFileURL {
ShareSheet(activityItems: [shareURL])
}
}
}
private var shouldShowShareButton: Bool {
attachment.downloadingState?.state == .downloaded
}
private var shouldShowDownloadButton: Bool {
(attachment.uploadingState == nil || attachment.uploadingState?.state == .uploaded) && attachment.downloadingState == nil
}
private var downloadButton: some View {
Button(action: { downloadAttachment() }) {
Image(uiImage: images.download)
.renderingMode(.template)
.foregroundColor(Color(colors.accentPrimary))
.frame(width: 24, height: 24)
}
.accessibilityLabel("Download")
}
private var shareButton: some View {
Button(action: { shareSheetShown = true }) {
Image(uiImage: images.share)
.renderingMode(.template)
.foregroundColor(Color(colors.accentPrimary))
.frame(width: 24, height: 24)
}
.accessibilityLabel("Share")
}
private func downloadAttachment() {
let messageId = attachment.id.messageId
let cid = attachment.id.cid
let messageController = chatClient.messageController(cid: cid, messageId: messageId)
messageController.downloadAttachment(attachment) { _ in }
}
}
struct ShareSheet: UIViewControllerRepresentable {
let activityItems: [Any]
func makeUIViewController(context: Context) -> UIActivityViewController {
UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {}
}