diff --git a/Session/Conversations/Message Cells/Content Views/MediaView.swift b/Session/Conversations/Message Cells/Content Views/MediaView.swift index 0af198314..cee5451b4 100644 --- a/Session/Conversations/Message Cells/Content Views/MediaView.swift +++ b/Session/Conversations/Message Cells/Content Views/MediaView.swift @@ -470,3 +470,43 @@ public class MediaView: UIView { unloadBlock?() } } + +// MARK: - SwiftUI + +import SwiftUI + +struct MediaView_SwiftUI: UIViewRepresentable { + public typealias UIViewType = MediaView + + private let mediaCache: NSCache? + public let attachment: Attachment + private let isOutgoing: Bool + private let cornerRadius: CGFloat + + public init( + mediaCache: NSCache? = nil, + attachment: Attachment, + isOutgoing: Bool, + cornerRadius: CGFloat + ) { + self.mediaCache = mediaCache + self.attachment = attachment + self.isOutgoing = isOutgoing + self.cornerRadius = cornerRadius + } + + func makeUIView(context: Context) -> MediaView { + let mediaView = MediaView( + mediaCache: mediaCache, + attachment: attachment, + isOutgoing: isOutgoing, + cornerRadius: cornerRadius + ) + + return mediaView + } + + func updateUIView(_ mediaView: MediaView, context: Context) { + mediaView.loadMedia() + } +} diff --git a/Session/Media Viewing & Editing/MessageInfoView.swift b/Session/Media Viewing & Editing/MessageInfoView.swift index 52379fcc4..0660ab70a 100644 --- a/Session/Media Viewing & Editing/MessageInfoView.swift +++ b/Session/Media Viewing & Editing/MessageInfoView.swift @@ -101,14 +101,37 @@ struct MessageInfoView: View { ZStack(alignment: .bottomTrailing) { if attachments.count > 1 { // Attachment carousel view - SessionCarouselView_SwiftUI(index: $index, contentInfos: attachments) - .frame( - maxWidth: .infinity, - maxHeight: .infinity, - alignment: .topLeading - ) + SessionCarouselView_SwiftUI( + index: $index, + isOutgoing: (messageViewModel.variant == .standardOutgoing), + contentInfos: attachments + ) + .frame( + maxWidth: .infinity, + maxHeight: .infinity, + alignment: .topLeading + ) } else { - // TODO: one attachment + MediaView_SwiftUI( + attachment: attachments[0], + isOutgoing: (messageViewModel.variant == .standardOutgoing), + cornerRadius: 0 + ) + .frame( + maxWidth: .infinity, + maxHeight: .infinity, + alignment: .topLeading + ) + .aspectRatio(1, contentMode: .fit) + .clipShape(RoundedRectangle(cornerRadius: 15)) + .padding( + EdgeInsets( + top: 0, + leading: 30, + bottom: 0, + trailing: 30 + ) + ) } Button { @@ -142,7 +165,7 @@ struct MessageInfoView: View { ) // Attachment Info - let attachment: Attachment = attachments[index - 1] + let attachment: Attachment = attachments[(index - 1 + attachments.count) % attachments.count] ZStack { RoundedRectangle(cornerRadius: 17) .fill(themeColor: .backgroundSecondary) diff --git a/Session/Shared/SessionCarouselView+SwiftUI.swift b/Session/Shared/SessionCarouselView+SwiftUI.swift index 114f84fd4..53cc1668d 100644 --- a/Session/Shared/SessionCarouselView+SwiftUI.swift +++ b/Session/Shared/SessionCarouselView+SwiftUI.swift @@ -4,11 +4,13 @@ import SwiftUI public struct SessionCarouselView_SwiftUI: View { @Binding var index: Int + let isOutgoing: Bool var contentInfos: [Attachment] let numberOfPages: Int - public init(index: Binding, contentInfos: [Attachment]) { + public init(index: Binding, isOutgoing: Bool, contentInfos: [Attachment]) { self._index = index + self.isOutgoing = isOutgoing self.contentInfos = contentInfos self.numberOfPages = contentInfos.count @@ -24,9 +26,12 @@ public struct SessionCarouselView_SwiftUI: View { .zIndex(1) PageView(index: $index, numberOfPages: self.numberOfPages) { - ForEach(self.contentInfos, id: \.self) { color in - Rectangle() - .foregroundColor(color) + ForEach(self.contentInfos) { attachment in + MediaView_SwiftUI( + attachment: attachment, + isOutgoing: self.isOutgoing, + cornerRadius: 0 + ) } } .aspectRatio(1, contentMode: .fit) @@ -66,7 +71,7 @@ struct ArrowView: View { } label: { Image(systemName: imageName) .font(.system(size: 20)) - .foregroundColor(.white) + .foregroundColor(themeColor: .textPrimary) .frame(width: 30, height: 30) } } @@ -199,7 +204,7 @@ struct SessionCarouselView_SwiftUI_Previews: PreviewProvider { Color.black } - SessionCarouselView_SwiftUI(index: $index, contentInfos: [.red, .orange, .blue]) + SessionCarouselView_SwiftUI(index: $index, isOutgoing: true, contentInfos: []) } } }