@@ -16,6 +16,7 @@ public struct ImageAttachmentContainer: View {
16
16
@Binding var scrolledId : String ?
17
17
18
18
@State private var galleryShown = false
19
+ @State private var selectedIndex = 0
19
20
20
21
public var body : some View {
21
22
VStack (
@@ -34,13 +35,12 @@ public struct ImageAttachmentContainer: View {
34
35
alignment: message. alignmentInBubble,
35
36
spacing: 0
36
37
) {
37
- Button {
38
- self . galleryShown = true
39
- } label: {
40
- ImageAttachmentView (
41
- message: message,
42
- width: width
43
- )
38
+ ImageAttachmentView (
39
+ message: message,
40
+ width: width
41
+ ) { index in
42
+ selectedIndex = index
43
+ galleryShown = true
44
44
}
45
45
46
46
if !message. text. isEmpty {
@@ -55,10 +55,13 @@ public struct ImageAttachmentContainer: View {
55
55
. clipped ( )
56
56
}
57
57
. messageBubble ( for: message, isFirst: isFirst)
58
- . fullScreenCover ( isPresented: $galleryShown) {
58
+ . fullScreenCover ( isPresented: $galleryShown, onDismiss: {
59
+ self . selectedIndex = 0
60
+ } ) {
59
61
GalleryView (
60
62
message: message,
61
- isShown: $galleryShown
63
+ isShown: $galleryShown,
64
+ selected: selectedIndex
62
65
)
63
66
}
64
67
}
@@ -83,6 +86,7 @@ struct ImageAttachmentView: View {
83
86
84
87
let message : ChatMessage
85
88
let width : CGFloat
89
+ var imageTapped : ( ( Int ) -> Void ) ? = nil
86
90
87
91
private let spacing : CGFloat = 2
88
92
private let maxDisplayedImages = 4
@@ -106,22 +110,28 @@ struct ImageAttachmentView: View {
106
110
if sources. count == 1 {
107
111
SingleImageView (
108
112
source: sources [ 0 ] ,
109
- width: width
113
+ width: width,
114
+ imageTapped: imageTapped,
115
+ index: 0
110
116
)
111
117
. withUploadingStateIndicator ( for: uploadState ( for: 0 ) , url: sources [ 0 ] )
112
118
} else if sources. count == 2 {
113
119
HStack ( spacing: spacing) {
114
120
MultiImageView (
115
121
source: sources [ 0 ] ,
116
122
width: width / 2 ,
117
- height: width
123
+ height: width,
124
+ imageTapped: imageTapped,
125
+ index: 0
118
126
)
119
127
. withUploadingStateIndicator ( for: uploadState ( for: 0 ) , url: sources [ 0 ] )
120
128
121
129
MultiImageView (
122
130
source: sources [ 1 ] ,
123
131
width: width / 2 ,
124
- height: width
132
+ height: width,
133
+ imageTapped: imageTapped,
134
+ index: 1
125
135
)
126
136
. withUploadingStateIndicator ( for: uploadState ( for: 1 ) , url: sources [ 1 ] )
127
137
}
@@ -131,22 +141,28 @@ struct ImageAttachmentView: View {
131
141
MultiImageView (
132
142
source: sources [ 0 ] ,
133
143
width: width / 2 ,
134
- height: width
144
+ height: width,
145
+ imageTapped: imageTapped,
146
+ index: 0
135
147
)
136
148
. withUploadingStateIndicator ( for: uploadState ( for: 0 ) , url: sources [ 0 ] )
137
149
138
150
VStack ( spacing: spacing) {
139
151
MultiImageView (
140
152
source: sources [ 1 ] ,
141
153
width: width / 2 ,
142
- height: width / 2
154
+ height: width / 2 ,
155
+ imageTapped: imageTapped,
156
+ index: 1
143
157
)
144
158
. withUploadingStateIndicator ( for: uploadState ( for: 1 ) , url: sources [ 1 ] )
145
159
146
160
MultiImageView (
147
161
source: sources [ 2 ] ,
148
162
width: width / 2 ,
149
- height: width / 2
163
+ height: width / 2 ,
164
+ imageTapped: imageTapped,
165
+ index: 2
150
166
)
151
167
. withUploadingStateIndicator ( for: uploadState ( for: 2 ) , url: sources [ 2 ] )
152
168
}
@@ -158,14 +174,18 @@ struct ImageAttachmentView: View {
158
174
MultiImageView (
159
175
source: sources [ 0 ] ,
160
176
width: width / 2 ,
161
- height: width / 2
177
+ height: width / 2 ,
178
+ imageTapped: imageTapped,
179
+ index: 0
162
180
)
163
181
. withUploadingStateIndicator ( for: uploadState ( for: 0 ) , url: sources [ 0 ] )
164
182
165
183
MultiImageView (
166
184
source: sources [ 1 ] ,
167
185
width: width / 2 ,
168
- height: width / 2
186
+ height: width / 2 ,
187
+ imageTapped: imageTapped,
188
+ index: 2
169
189
)
170
190
. withUploadingStateIndicator ( for: uploadState ( for: 1 ) , url: sources [ 1 ] )
171
191
}
@@ -174,15 +194,19 @@ struct ImageAttachmentView: View {
174
194
MultiImageView (
175
195
source: sources [ 2 ] ,
176
196
width: width / 2 ,
177
- height: width / 2
197
+ height: width / 2 ,
198
+ imageTapped: imageTapped,
199
+ index: 1
178
200
)
179
201
. withUploadingStateIndicator ( for: uploadState ( for: 2 ) , url: sources [ 2 ] )
180
202
181
203
ZStack {
182
204
MultiImageView (
183
205
source: sources [ 3 ] ,
184
206
width: width / 2 ,
185
- height: width / 2
207
+ height: width / 2 ,
208
+ imageTapped: imageTapped,
209
+ index: 3
186
210
)
187
211
. withUploadingStateIndicator ( for: uploadState ( for: 3 ) , url: sources [ 3 ] )
188
212
@@ -215,22 +239,36 @@ struct ImageAttachmentView: View {
215
239
struct SingleImageView : View {
216
240
let source : URL
217
241
let width : CGFloat
242
+ var imageTapped : ( ( Int ) -> Void ) ? = nil
243
+ var index : Int ?
218
244
219
245
var body : some View {
220
- LazyLoadingImage ( source: source, width: width)
221
- . frame ( width: width, height: 3 * width / 4 )
246
+ LazyLoadingImage (
247
+ source: source,
248
+ width: width,
249
+ imageTapped: imageTapped,
250
+ index: index
251
+ )
252
+ . frame ( width: width, height: 3 * width / 4 )
222
253
}
223
254
}
224
255
225
256
struct MultiImageView : View {
226
257
let source : URL
227
258
let width : CGFloat
228
259
let height : CGFloat
260
+ var imageTapped : ( ( Int ) -> Void ) ? = nil
261
+ var index : Int ?
229
262
230
263
var body : some View {
231
- LazyLoadingImage ( source: source, width: width)
232
- . frame ( width: width, height: height)
233
- . clipped ( )
264
+ LazyLoadingImage (
265
+ source: source,
266
+ width: width,
267
+ imageTapped: imageTapped,
268
+ index: index
269
+ )
270
+ . frame ( width: width, height: height)
271
+ . clipped ( )
234
272
}
235
273
}
236
274
@@ -244,16 +282,31 @@ struct LazyLoadingImage: View {
244
282
let width : CGFloat
245
283
246
284
var resize : Bool = true
285
+ var imageTapped : ( ( Int ) -> Void ) ? = nil
286
+ var index : Int ?
247
287
248
288
var body : some View {
249
289
ZStack {
250
290
if let image = image {
251
- Image ( uiImage: image)
252
- . resizable ( )
253
- . scaledToFill ( )
254
- . aspectRatio ( contentMode: . fill)
255
- . clipped ( )
256
- . allowsHitTesting ( false )
291
+ if let imageTapped = imageTapped {
292
+ Button {
293
+ imageTapped ( index ?? 0 )
294
+ } label: {
295
+ Image ( uiImage: image)
296
+ . resizable ( )
297
+ . scaledToFill ( )
298
+ . aspectRatio ( contentMode: . fill)
299
+ . clipped ( )
300
+ . allowsHitTesting ( false )
301
+ }
302
+ } else {
303
+ Image ( uiImage: image)
304
+ . resizable ( )
305
+ . scaledToFill ( )
306
+ . aspectRatio ( contentMode: . fill)
307
+ . clipped ( )
308
+ . allowsHitTesting ( false )
309
+ }
257
310
} else if error != nil {
258
311
Color ( . secondarySystemBackground)
259
312
} else {
0 commit comments