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 all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

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

# [4.57.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.57.0)
_June 07, 2024_
Expand Down
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 @@ -2,6 +2,7 @@
// Copyright © 2024 Stream.io Inc. All rights reserved.
//

import AVKit
import StreamChat
import SwiftUI

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