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
import SwiftUI
#if DEBUG
import UIKit
#endif
struct PostMedia: View {
var imageURLs: [URL] = []
var imageThumbnailURLs: [URL: URL] = [:]
var pendingImages: [ImageAttachment] = []
var gifURLs: [URL] = []
var inlineGIFVideoURLs: [URL] = []
var videoURLs: [URL] = []
var descriptions: [URL: String] = [:]
var isContext = false
var decision: ModerationPolicy.Decision?
var accessibilityID = ""
@State private var mediaOverride: Bool?
private var hasMedia: Bool {
!imageURLs.isEmpty || !pendingImages.isEmpty || !gifURLs.isEmpty || !inlineGIFVideoURLs.isEmpty
|| !videoURLs.isEmpty
}
var body: some View {
Group {
if hasMedia {
let media = decision?.contentMedia
let hidden =
(media?.noOverride == true && media?.blur == true) || (mediaOverride ?? (media?.blur == true))
if hidden {
if media?.noOverride == true {
mediaButtonLabel("Media warning", systemImage: "exclamationmark.triangle")
} else {
Button {
mediaOverride = false
} label: {
mediaButtonLabel("Show media", systemImage: "exclamationmark.triangle")
}
.buttonStyle(.plain)
.accessibilityIdentifier("showMedia-\(accessibilityID)")
.accessibilityHint("Media warning")
}
} else {
mediaContent
if decision?.hasAdultMediaLabel == true {
Button {
mediaOverride = true
} label: {
mediaButtonLabel("Hide media", systemImage: "eye.slash")
}
.buttonStyle(.plain)
.accessibilityIdentifier("hideMedia-\(accessibilityID)")
}
}
}
}
.onChange(of: decision) { _, _ in mediaOverride = nil }
.onChange(of: accessibilityID) { _, _ in mediaOverride = nil }
}
private func mediaButtonLabel(_ title: String, systemImage: String) -> some View {
Label(title, systemImage: systemImage)
.appFont(.subheadline, weight: .medium)
.foregroundStyle(.primary)
.frame(maxWidth: .infinity, minHeight: 48)
.background {
RoundedRectangle(cornerRadius: 12, style: .continuous)
.fill(Color(uiColor: .secondarySystemGroupedBackground))
.overlay {
RoundedRectangle(cornerRadius: 12, style: .continuous)
.fill(AppPalette.mediaWarningBackground)
}
}
.contentShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
}
@ViewBuilder private var mediaContent: some View {
if imageURLs.isEmpty && !pendingImages.isEmpty {
PendingPostImages(images: pendingImages)
} else if !imageURLs.isEmpty {
InlineImages(
urls: imageURLs, thumbnailURLs: imageThumbnailURLs, descriptions: descriptions,
isReplyContext: isContext, requiresGIFReveal: isContext)
}
if !gifURLs.isEmpty || !inlineGIFVideoURLs.isEmpty {
GIFMedia(
imageURLs: gifURLs, videoURLs: inlineGIFVideoURLs, descriptions: descriptions, isReplyContext: isContext
)
}
if !videoURLs.isEmpty {
VideoLinks(urls: videoURLs)
}
}
}
#if DEBUG
#Preview("Post media") {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 480, height: 300))
let image = renderer.image { context in
UIColor.systemTeal.setFill()
context.fill(CGRect(x: 0, y: 0, width: 480, height: 300))
UIColor.systemYellow.setFill()
context.cgContext.fillEllipse(in: CGRect(x: 320, y: 40, width: 90, height: 90))
}
let attachment = ImageAttachment(
data: image.pngData()!, altText: "A yellow sun against a teal sky", mimeType: "image/png")
return ScrollView {
VStack(alignment: .leading, spacing: 20) {
PostMedia(pendingImages: [attachment])
PostMedia(
pendingImages: [
attachment,
ImageAttachment(
data: attachment.data, altText: attachment.altText, mimeType: attachment.mimeType)
], isContext: true)
PostMedia(videoURLs: [URL(fileURLWithPath: "/preview/video.mp4")])
}
.padding()
}
}
#endif