2
2
// Copyright © 2024 Stream.io Inc. All rights reserved.
3
3
//
4
4
5
+ import AVKit
5
6
import StreamChat
6
7
import SwiftUI
7
8
@@ -14,7 +15,7 @@ public struct GalleryView: View {
14
15
@Injected ( \. fonts) private var fonts
15
16
@Injected ( \. images) private var images
16
17
17
- var imageAttachments : [ ChatMessageImageAttachment ]
18
+ var mediaAttachments : [ MediaAttachment ]
18
19
var author : ChatUser
19
20
@Binding var isShown : Bool
20
21
@State private var selected : Int
@@ -27,7 +28,34 @@ public struct GalleryView: View {
27
28
isShown: Binding < Bool > ,
28
29
selected: Int
29
30
) {
30
- self . imageAttachments = imageAttachments
31
+ let mediaAttachments = imageAttachments. map { attachment in
32
+ let url : URL
33
+ if let state = attachment. uploadingState {
34
+ url = state. localFileURL
35
+ } else {
36
+ url = attachment. imageURL
37
+ }
38
+ return MediaAttachment (
39
+ url: url,
40
+ type: . image,
41
+ uploadingState: attachment. uploadingState
42
+ )
43
+ }
44
+ self . init (
45
+ mediaAttachments: mediaAttachments,
46
+ author: author,
47
+ isShown: isShown,
48
+ selected: selected
49
+ )
50
+ }
51
+
52
+ init (
53
+ mediaAttachments: [ MediaAttachment ] ,
54
+ author: ChatUser ,
55
+ isShown: Binding < Bool > ,
56
+ selected: Int
57
+ ) {
58
+ self . mediaAttachments = mediaAttachments
31
59
self . author = author
32
60
_isShown = isShown
33
61
_selected = State ( initialValue: selected)
@@ -43,27 +71,34 @@ public struct GalleryView: View {
43
71
)
44
72
45
73
TabView ( selection: $selected) {
46
- ForEach ( 0 ..< sources. count, id: \. self) { index in
47
- let url = sources [ index]
48
- ZoomableScrollView {
49
- VStack {
50
- Spacer ( )
51
- LazyLoadingImage (
52
- source: url,
53
- width: reader. size. width,
54
- height: reader. size. height,
55
- resize: true ,
56
- shouldSetFrame: false ,
57
- onImageLoaded: { image in
58
- loadedImages [ index] = image
74
+ ForEach ( 0 ..< mediaAttachments. count, id: \. self) { index in
75
+ ZStack {
76
+ let source = mediaAttachments [ index]
77
+ if source. type == . image {
78
+ ZoomableScrollView {
79
+ VStack {
80
+ Spacer ( )
81
+ LazyLoadingImage (
82
+ source: source,
83
+ width: reader. size. width,
84
+ height: reader. size. height,
85
+ resize: true ,
86
+ shouldSetFrame: false ,
87
+ onImageLoaded: { image in
88
+ loadedImages [ index] = image
89
+ }
90
+ )
91
+ . aspectRatio ( contentMode: . fit)
92
+ . frame ( width: reader. size. width)
93
+ Spacer ( )
59
94
}
60
- )
61
- . aspectRatio ( contentMode: . fit)
62
- . frame ( width: reader. size. width)
63
- Spacer ( )
95
+ }
96
+ . tag ( index)
97
+ } else {
98
+ StreamVideoPlayer ( url: source. url)
99
+ . tag ( index)
64
100
}
65
101
}
66
- . tag ( index)
67
102
}
68
103
}
69
104
. tabViewStyle ( . page( indexDisplayMode: . never) )
@@ -82,7 +117,7 @@ public struct GalleryView: View {
82
117
83
118
Spacer ( )
84
119
85
- Text ( " \( selected + 1 ) of \( sources . count) " )
120
+ Text ( " \( selected + 1 ) of \( mediaAttachments . count) " )
86
121
. font ( fonts. bodyBold)
87
122
88
123
Spacer ( )
@@ -101,7 +136,7 @@ public struct GalleryView: View {
101
136
}
102
137
. sheet ( isPresented: $gridShown) {
103
138
GridPhotosView (
104
- imageURLs: sources ,
139
+ imageURLs: mediaAttachments . filter { $0 . type == . image } . map ( \ . url ) ,
105
140
isShown: $gridShown
106
141
)
107
142
}
@@ -115,10 +150,23 @@ public struct GalleryView: View {
115
150
return [ ]
116
151
}
117
152
}
153
+ }
118
154
119
- private var sources : [ URL ] {
120
- imageAttachments. map { attachment in
121
- attachment. imageURL
122
- }
155
+ struct StreamVideoPlayer : View {
156
+
157
+ @State var player : AVPlayer
158
+
159
+ init ( url: URL ) {
160
+ let player = AVPlayer ( url: url)
161
+ _player = State ( wrappedValue: player)
162
+ }
163
+
164
+ var body : some View {
165
+ VideoPlayer ( player: player)
166
+ . clipped ( )
167
+ . onAppear {
168
+ try ? AVAudioSession . sharedInstance ( ) . setCategory ( . playback, options: [ ] )
169
+ player. play ( )
170
+ }
123
171
}
124
172
}
0 commit comments