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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import SwiftUI
import UIKit
struct RemoteInlineImage: View {
@Environment(AppModel.self) private var model
@Environment(\.accessibilityReduceMotion) private var reduceMotion
let url: URL
let height: CGFloat
let accessibilityLabel: String
var accessibilityDescription: String?
let tileIdentifier: String
let onTap: (() -> Void)?
var limitsGIF = false
var isGIFRevealed = false
var requiresGIFReveal = false
@AppStorage("showGIFsInline") private var showGIFsInline = true
@State private var imageURL: URL?
@State private var revealedGIF = false
@State private var imageEntry: InlineImageStore.Entry?
@State private var isLoading = true
@State private var showsLoadingIndicator = false
@State private var retryCount = 0
@State private var requestID = UUID()
private var hidesGIF: Bool {
(requiresGIFReveal || !showGIFsInline) && !revealedGIF && !isGIFRevealed
}
var body: some View {
let cached = model.inlineImages.cachedImage(for: url)
let local = imageURL == url ? imageEntry : nil
let displayed = local?.image == nil ? cached : local
let displayedImage = displayed?.image
let hasGIF = local?.gifData != nil || cached?.gifData != nil
let isGIFHidden = hasGIF && hidesGIF
let shape = RoundedRectangle(cornerRadius: 12, style: .continuous)
Group {
if isGIFHidden {
Button("Show GIF", systemImage: "play.rectangle") { revealedGIF = true }
.accessibilityIdentifier(requiresGIFReveal ? "showReplyGIFButton" : "showGIFButton")
} else if let displayedImage, let onTap {
Button(action: onTap) {
imageContent(displayedImage)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityElement(children: .ignore)
.accessibilityLabel(accessibilityLabel)
.accessibilityValue(accessibilityDescription?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "")
.accessibilityHint("Opens image viewer")
.accessibilityIdentifier(tileIdentifier)
.transition(.opacity)
} else if let displayedImage {
imageContent(displayedImage)
.accessibilityLabel(accessibilityLabel)
.transition(.opacity)
} else if isLoading {
Color(uiColor: .systemGray5)
.overlay {
if showsLoadingIndicator {
ProgressView()
.accessibilityLabel("Loading image")
.accessibilityIdentifier("inlineImageLoading")
}
}
.accessibilityElement(children: .combine)
.accessibilityLabel("Loading image")
} else {
Button {
retryCount += 1
} label: {
Label("Image could not be loaded. Try again", systemImage: "photo")
.foregroundStyle(.secondary)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.buttonStyle(.plain)
}
}
.frame(maxWidth: .infinity)
.frame(height: height)
.clipped()
.clipShape(shape)
.overlay {
if let displayed, displayed.image != nil, !isGIFHidden {
ImageBorder(shape: shape, colors: displayed.borderColors)
}
}
.accessibilityIdentifier(
isGIFHidden ? (requiresGIFReveal ? "showReplyGIFButton" : "showGIFButton") : tileIdentifier
)
.onChange(of: showGIFsInline) { _, enabled in
if !enabled { revealedGIF = false }
}
.onChange(of: url) { _, _ in
revealedGIF = false
}
.task(
id:
"\(url.absoluteString)-\(retryCount)-\(revealedGIF)-\(showGIFsInline)-"
+ "\(isGIFRevealed)-\(requiresGIFReveal)"
) {
let token = UUID()
requestID = token
if imageURL != url {
imageEntry = nil
imageURL = url
}
if imageEntry?.image == nil, let cached = model.inlineImages.cachedImage(for: url) {
imageEntry = cached
}
if imageEntry?.image != nil || (imageEntry?.gifData != nil && hidesGIF) {
isLoading = false
showsLoadingIndicator = false
return
}
isLoading = true
showsLoadingIndicator = false
let indicatorTask = Task { @MainActor in
try? await Task.sleep(for: .milliseconds(200))
guard !Task.isCancelled, requestID == token, isLoading else { return }
showsLoadingIndicator = true
}
defer { indicatorTask.cancel() }
let loaded = await model.inlineImages.load(url, decodeGIF: !hidesGIF)
guard !Task.isCancelled, requestID == token else { return }
withAnimation(!reduceMotion && loaded?.image != nil ? .easeInOut(duration: 0.25) : nil) {
imageEntry = loaded
isLoading = false
}
}
}
@ViewBuilder
private func imageContent(_ image: UIImage) -> some View {
if image.images != nil {
AnimatedImageView(image: image, accessibilityLabel: accessibilityLabel)
} else {
// Let the tile set its size. The image only fills that space.
Color.clear
.overlay {
Image(uiImage: image)
.resizable()
.scaledToFill()
.accessibilityLabel(accessibilityLabel)
}
}
}
}
#if DEBUG
#Preview("Image: local file unavailable") {
PreviewHost { _ in
RemoteInlineImage(
url: MediaPreviewContent.imageURL,
height: 260,
accessibilityLabel: "Attached image",
tileIdentifier: "previewImage",
onTap: {}
)
.padding()
}
}
#Preview("Image renderer") {
AnimatedImageView(image: MediaPreviewContent.image, accessibilityLabel: "A yellow sun over a teal landscape")
.frame(height: 260)
.clipShape(RoundedRectangle(cornerRadius: 12))
.padding()
}
#endif
private struct AnimatedImageView: UIViewRepresentable {
let image: UIImage
let accessibilityLabel: String
func makeUIView(context: Context) -> UIImageView {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.isAccessibilityElement = true
imageView.accessibilityLabel = accessibilityLabel
imageView.setContentHuggingPriority(.defaultLow, for: .horizontal)
imageView.setContentHuggingPriority(.defaultLow, for: .vertical)
imageView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
imageView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
return imageView
}
func updateUIView(_ imageView: UIImageView, context: Context) {
imageView.accessibilityLabel = accessibilityLabel
if imageView.image !== image {
imageView.image = image
imageView.startAnimating()
}
}
}