import AVKit import SwiftUI import UIKit struct GIFMedia: View { let imageURLs: [URL] let videoURLs: [URL] let descriptions: [URL: String] var isReplyContext = false @AppStorage("showGIFsInline") private var showGIFsInline = true @State private var revealed: Set = [] @State private var selectedImage: ImageViewerRequest? var body: some View { ForEach( Array((imageURLs + videoURLs.filter { !imageURLs.contains($0) }).enumerated()), id: \.offset ) { _, url in if (!isReplyContext && showGIFsInline) || revealed.contains(url) { if imageURLs.contains(url) { RemoteInlineImage( url: url, height: 260, accessibilityLabel: descriptions[url] ?? "GIF", tileIdentifier: "inlineGIFImage", onTap: { selectedImage = ImageViewerRequest( urls: [url], descriptions: descriptions, revealedGIFURLs: [url], index: 0, requiresGIFReveal: isReplyContext) }, limitsGIF: true, isGIFRevealed: true ) .accessibilityIdentifier("inlineGIF") } else { InlineLoopingVideo(url: url, description: descriptions[url] ?? "GIF") .accessibilityIdentifier("inlineGIF") } } else { Button { revealed.insert(url) } label: { Label("Show GIF", systemImage: "play.rectangle") .appFont(.subheadline, weight: .medium) .frame(maxWidth: .infinity, minHeight: 48) .modifier(MediaControlSurface()) } .buttonStyle(.plain) .accessibilityIdentifier(isReplyContext ? "showReplyGIFButton" : "showGIFButton") .accessibilityHint(descriptions[url] ?? "") } } .onChange(of: showGIFsInline) { _, enabled in if !enabled { revealed.removeAll() } } .fullScreenCover(item: $selectedImage) { request in ImageViewer(request: request) } } } #if DEBUG #Preview("GIF: reveal in reply") { PreviewHost { _ in GIFMedia(imageURLs: [MediaPreviewContent.imageURL], videoURLs: [], descriptions: [:], isReplyContext: true) .padding() } } #Preview("GIF video: local playback unavailable") { PreviewHost { _ in InlineLoopingVideo(url: MediaPreviewContent.videoURL, description: "Preview GIF") .padding() } } #endif struct InlineLoopingVideo: View { @Environment(\.scenePhase) private var scenePhase let url: URL let description: String @StateObject private var playback: InlineGIFPlayback @State private var isVisible = false init(url: URL, description: String, playback: InlineGIFPlayback? = nil) { self.url = url self.description = description _playback = StateObject(wrappedValue: playback ?? InlineGIFPlayback()) } var body: some View { let shape = RoundedRectangle(cornerRadius: 12, style: .continuous) ZStack { Color.black if playback.failed { Button("GIF could not be loaded. Try again") { playback.retry(url: url, isVisible: isVisible, isActive: scenePhase == .active) } .foregroundStyle(.white) } else { if let player = playback.player { InlinePlayerLayer(player: player) .accessibilityLabel(description) .accessibilityHidden(!playback.ready) } if !playback.ready { ProgressView().tint(.white).accessibilityLabel("Loading GIF") } } } .aspectRatio(16 / 9, contentMode: .fit) .clipShape(shape) .overlay { if playback.ready && !playback.failed { ImageBorder(shape: shape, colors: .fallback) } } .onScrollVisibilityChange(threshold: 0.25) { visible in isVisible = visible updatePlayback(isVisible: visible) } .onChange(of: scenePhase) { _, _ in updatePlayback(isVisible: isVisible) } .onChange(of: url) { _, _ in updatePlayback(isVisible: isVisible) } .onAppear { updatePlayback(isVisible: isVisible) } .onDisappear { isVisible = false updatePlayback(isVisible: false) } } private func updatePlayback(isVisible: Bool) { playback.update(url: url, isVisible: isVisible, isActive: scenePhase == .active) } } private struct InlinePlayerLayer: UIViewRepresentable { let player: AVPlayer func makeUIView(context: Context) -> InlinePlayerUIView { InlinePlayerUIView() } func updateUIView(_ view: InlinePlayerUIView, context: Context) { view.playerLayer.player = player view.playerLayer.videoGravity = .resizeAspect } static func dismantleUIView(_ view: InlinePlayerUIView, coordinator: ()) { view.playerLayer.player = nil } } private final class InlinePlayerUIView: UIView { override static var layerClass: AnyClass { AVPlayerLayer.self } var playerLayer: AVPlayerLayer { guard let playerLayer = layer as? AVPlayerLayer else { preconditionFailure("The video view requires an AVPlayerLayer.") } return playerLayer } }