Skip to content

Commit a9bc868

Browse files
Grouping image and video attachments in the same message (#525)
1 parent 8ee90c0 commit a9bc868

File tree

10 files changed

+219
-73
lines changed

10 files changed

+219
-73
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1414

1515
### 🔄 Changed
1616
- Show inline alert banner when encountering a failure while interacting with polls [#504](https://github.com/GetStream/stream-chat-swiftui/pull/504)
17+
- Grouping image and video attachments in the same message [#525](https://github.com/GetStream/stream-chat-swiftui/pull/525)
1718

1819
# [4.57.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.57.0)
1920
_June 07, 2024_

Sources/StreamChatSwiftUI/ChatChannel/ChannelInfo/MediaAttachmentsView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ struct ImageAttachmentContentView: View {
115115
galleryShown = true
116116
} label: {
117117
LazyLoadingImage(
118-
source: imageAttachment.imageURL,
118+
source: MediaAttachment(url: imageAttachment.imageURL, type: .image),
119119
width: itemWidth,
120120
height: itemWidth
121121
)

Sources/StreamChatSwiftUI/ChatChannel/Gallery/GalleryView.swift

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright © 2024 Stream.io Inc. All rights reserved.
33
//
44

5+
import AVKit
56
import StreamChat
67
import 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
}

Sources/StreamChatSwiftUI/ChatChannel/Gallery/GridPhotosView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct GridPhotosView: View {
3333
LazyVGrid(columns: columns, spacing: 2) {
3434
ForEach(imageURLs, id: \.self) { url in
3535
LazyLoadingImage(
36-
source: url,
36+
source: MediaAttachment(url: url, type: .image),
3737
width: Self.itemWidth,
3838
height: Self.itemWidth
3939
)

0 commit comments

Comments
 (0)