diff --git a/Threaded/Components/Post/PostAttachment.swift b/Threaded/Components/Post/PostAttachment.swift index 8c58b84..b80dd00 100644 --- a/Threaded/Components/Post/PostAttachment.swift +++ b/Threaded/Components/Post/PostAttachment.swift @@ -7,7 +7,10 @@ import AVKit struct PostAttachment: View { @Environment(AppDelegate.self) private var appDelegate: AppDelegate var attachment: MediaAttachment + var isFeatured: Bool = true + var isImaging: Bool = false + @State private var player: AVPlayer? var appLayoutWidth: CGFloat = 10 @@ -24,91 +27,128 @@ struct PostAttachment: View { let mediaSize: CGSize = size(for: attachment) ?? .init(width: imageMaxHeight, height: imageMaxHeight) let newSize = imageSize(from: mediaSize) - GeometryReader { _ in - // Audio later because it's a lil harder - if attachment.supportedType == .image { - if let url = attachment.url { - AsyncImage(url: url) { image in - image - .resizable() - .aspectRatio(contentMode: .fill) - .frame(width: !isFeatured ? imageMaxHeight / 1.5 : newSize.width, height: !isFeatured ? imageMaxHeight: newSize.height) - .overlay( - RoundedRectangle(cornerRadius: 15) - .stroke(.gray.opacity(0.3), lineWidth: 1) - ) - } placeholder: { - ZStack(alignment: .center) { + if !isImaging { + GeometryReader { _ in + // Audio later because it's a lil harder + if attachment.supportedType == .image { + if let url = attachment.url { + AsyncImage(url: url) { image in + image + .resizable() + .aspectRatio(contentMode: .fill) + .frame(width: !isFeatured ? imageMaxHeight / 1.5 : newSize.width, height: !isFeatured ? imageMaxHeight: newSize.height) + .overlay( + RoundedRectangle(cornerRadius: 15) + .stroke(.gray.opacity(0.3), lineWidth: 1) + ) + } placeholder: { + ZStack(alignment: .center) { + Color.gray + + ProgressView() + .progressViewStyle(.circular) + } + } + } + } else if attachment.supportedType == .gifv { + ZStack(alignment: .center) { + if player != nil { + NoControlsPlayerViewController(player: player!) + .overlay( + RoundedRectangle(cornerRadius: 15) + .stroke(.gray.opacity(0.3), lineWidth: 1) + ) + } else { Color.gray ProgressView() .progressViewStyle(.circular) } } - } - } else if attachment.supportedType == .gifv { - ZStack(alignment: .center) { - if player != nil { - NoControlsPlayerViewController(player: player!) - .overlay( - RoundedRectangle(cornerRadius: 15) - .stroke(.gray.opacity(0.3), lineWidth: 1) - ) - } else { - Color.gray - - ProgressView() - .progressViewStyle(.circular) - } - } - .onAppear { - if let url = attachment.url { - player = AVPlayer(url: url) - player?.audiovisualBackgroundPlaybackPolicy = .pauses - player?.isMuted = true - player?.play() - - guard let player else { return } - NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) { _ in - Task { @MainActor in - player.seek(to: CMTime.zero) - player.play() + .onAppear { + if let url = attachment.url { + player = AVPlayer(url: url) + player?.audiovisualBackgroundPlaybackPolicy = .pauses + player?.isMuted = true + player?.play() + + guard let player else { return } + NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) { _ in + Task { @MainActor in + player.seek(to: CMTime.zero) + player.play() + } } } } + .onDisappear() { + guard player != nil else { return } + player?.pause() + } + + } else if attachment.supportedType == .video { + ZStack { + if player != nil { + VideoPlayer(player: player) + .overlay( + RoundedRectangle(cornerRadius: 15) + .stroke(.gray.opacity(0.3), lineWidth: 1) + ) + } else { + Color.gray + + ProgressView() + .progressViewStyle(.circular) + } + } + .onAppear { + if let url = attachment.url { + player = AVPlayer(url: url) + player?.audiovisualBackgroundPlaybackPolicy = .pauses + player?.isMuted = false + player?.play() + } + } + .onDisappear() { + guard player != nil else { return } + player?.pause() + } } - .onDisappear() { - guard player != nil else { return } - player?.pause() - } - - } else if attachment.supportedType == .video { - ZStack { - if player != nil { - VideoPlayer(player: player) - .overlay( - RoundedRectangle(cornerRadius: 15) - .stroke(.gray.opacity(0.3), lineWidth: 1) - ) - } else { + } + .frame(width: !isFeatured ? imageMaxHeight / 1.5 : newSize.width, height: !isFeatured ? imageMaxHeight: newSize.height) + .clipped() + .clipShape(.rect(cornerRadius: 15)) + .contentShape(Rectangle()) + } else { + imaging + } + } + + @ViewBuilder + var imaging: some View { + let mediaSize: CGSize = size(for: attachment) ?? .init(width: imageMaxHeight, height: imageMaxHeight) + let newSize = imageSize(from: mediaSize) + + GeometryReader { _ in + // Audio later because it's a lil harder + if let url = attachment.previewUrl { + AsyncImage(url: url) { image in + image + .resizable() + .aspectRatio(contentMode: .fill) + .frame(width: !isFeatured ? imageMaxHeight / 1.5 : newSize.width, height: !isFeatured ? imageMaxHeight: newSize.height) + .overlay( + RoundedRectangle(cornerRadius: 15) + .stroke(.gray.opacity(0.3), lineWidth: 1) + ) + } placeholder: { + ZStack(alignment: .center) { Color.gray ProgressView() .progressViewStyle(.circular) } } - .onAppear { - if let url = attachment.url { - player = AVPlayer(url: url) - player?.audiovisualBackgroundPlaybackPolicy = .pauses - player?.isMuted = false - player?.play() - } - } - .onDisappear() { - guard player != nil else { return } - player?.pause() - } } } .frame(width: !isFeatured ? imageMaxHeight / 1.5 : newSize.width, height: !isFeatured ? imageMaxHeight: newSize.height)