22// Copyright © 2024 Stream.io Inc. All rights reserved.
33//
44
5+ import AVKit
56import StreamChat
67import SwiftUI
78
@@ -14,7 +15,7 @@ public struct GalleryView: View {
1415 @Injected ( \. fonts) private var fonts
1516 @Injected ( \. images) private var images
1617
17- var imageAttachments : [ ChatMessageImageAttachment ]
18+ var mediaAttachments : [ MediaAttachment ]
1819 var author : ChatUser
1920 @Binding var isShown : Bool
2021 @State private var selected : Int
@@ -27,7 +28,34 @@ public struct GalleryView: View {
2728 isShown: Binding < Bool > ,
2829 selected: Int
2930 ) {
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
3159 self . author = author
3260 _isShown = isShown
3361 _selected = State ( initialValue: selected)
@@ -43,27 +71,34 @@ public struct GalleryView: View {
4371 )
4472
4573 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 ( )
5994 }
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)
64100 }
65101 }
66- . tag ( index)
67102 }
68103 }
69104 . tabViewStyle ( . page( indexDisplayMode: . never) )
@@ -82,7 +117,7 @@ public struct GalleryView: View {
82117
83118 Spacer ( )
84119
85- Text ( " \( selected + 1 ) of \( sources . count) " )
120+ Text ( " \( selected + 1 ) of \( mediaAttachments . count) " )
86121 . font ( fonts. bodyBold)
87122
88123 Spacer ( )
@@ -101,7 +136,7 @@ public struct GalleryView: View {
101136 }
102137 . sheet ( isPresented: $gridShown) {
103138 GridPhotosView (
104- imageURLs: sources ,
139+ imageURLs: mediaAttachments . filter { $0 . type == . image } . map ( \ . url ) ,
105140 isShown: $gridShown
106141 )
107142 }
@@ -115,10 +150,23 @@ public struct GalleryView: View {
115150 return [ ]
116151 }
117152 }
153+ }
118154
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+ }
123171 }
124172}
0 commit comments