1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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<URL> = []
@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
}
}