Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grouping image and video attachments in the same message #525

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ struct ImageAttachmentContentView: View {
galleryShown = true
} label: {
LazyLoadingImage(
source: imageAttachment.imageURL,
source: MediaAttachment(url: imageAttachment.imageURL, type: .image),
width: itemWidth,
height: itemWidth
)
Expand Down
100 changes: 74 additions & 26 deletions Sources/StreamChatSwiftUI/ChatChannel/Gallery/GalleryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import StreamChat
import SwiftUI
import AVKit

/// View used for displaying image attachments in a gallery.
public struct GalleryView: View {
Expand All @@ -14,7 +15,7 @@ public struct GalleryView: View {
@Injected(\.fonts) private var fonts
@Injected(\.images) private var images

var imageAttachments: [ChatMessageImageAttachment]
var mediaAttachments: [MediaAttachment]
var author: ChatUser
@Binding var isShown: Bool
@State private var selected: Int
Expand All @@ -27,7 +28,34 @@ public struct GalleryView: View {
isShown: Binding<Bool>,
selected: Int
) {
self.imageAttachments = imageAttachments
let mediaAttachments = imageAttachments.map { attachment in
let url: URL
if let state = attachment.uploadingState {
url = state.localFileURL
} else {
url = attachment.imageURL
}
return MediaAttachment(
url: url,
type: .image,
uploadingState: attachment.uploadingState
)
}
self.init(
mediaAttachments: mediaAttachments,
author: author,
isShown: isShown,
selected: selected
)
}

init(
mediaAttachments: [MediaAttachment],
author: ChatUser,
isShown: Binding<Bool>,
selected: Int
) {
self.mediaAttachments = mediaAttachments
self.author = author
_isShown = isShown
_selected = State(initialValue: selected)
Expand All @@ -43,27 +71,34 @@ public struct GalleryView: View {
)

TabView(selection: $selected) {
ForEach(0..<sources.count, id: \.self) { index in
let url = sources[index]
ZoomableScrollView {
VStack {
Spacer()
LazyLoadingImage(
source: url,
width: reader.size.width,
height: reader.size.height,
resize: true,
shouldSetFrame: false,
onImageLoaded: { image in
loadedImages[index] = image
ForEach(0..<mediaAttachments.count, id: \.self) { index in
ZStack {
let source = mediaAttachments[index]
if source.type == .image {
ZoomableScrollView {
VStack {
Spacer()
LazyLoadingImage(
source: source,
width: reader.size.width,
height: reader.size.height,
resize: true,
shouldSetFrame: false,
onImageLoaded: { image in
loadedImages[index] = image
}
)
.aspectRatio(contentMode: .fit)
.frame(width: reader.size.width)
Spacer()
}
)
.aspectRatio(contentMode: .fit)
.frame(width: reader.size.width)
Spacer()
}
.tag(index)
} else {
StreamVideoPlayer(url: source.url)
.tag(index)
}
}
.tag(index)
}
}
.tabViewStyle(.page(indexDisplayMode: .never))
Expand All @@ -82,7 +117,7 @@ public struct GalleryView: View {

Spacer()

Text("\(selected + 1) of \(sources.count)")
Text("\(selected + 1) of \(mediaAttachments.count)")
.font(fonts.bodyBold)

Spacer()
Expand All @@ -101,7 +136,7 @@ public struct GalleryView: View {
}
.sheet(isPresented: $gridShown) {
GridPhotosView(
imageURLs: sources,
imageURLs: mediaAttachments.filter { $0.type == .image }.map(\.url),
isShown: $gridShown
)
}
Expand All @@ -115,10 +150,23 @@ public struct GalleryView: View {
return []
}
}
}

private var sources: [URL] {
imageAttachments.map { attachment in
attachment.imageURL
}
struct StreamVideoPlayer: View {

@State var player: AVPlayer

init(url: URL) {
let player = AVPlayer(url: url)
_player = State(wrappedValue: player)
}

var body: some View {
VideoPlayer(player: player)
.clipped()
.onAppear {
try? AVAudioSession.sharedInstance().setCategory(.playback, options: [])
player.play()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct GridPhotosView: View {
LazyVGrid(columns: columns, spacing: 2) {
ForEach(imageURLs, id: \.self) { url in
LazyLoadingImage(
source: url,
source: MediaAttachment(url: url, type: .image),
width: Self.itemWidth,
height: Self.itemWidth
)
Expand Down
Loading
Loading